Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Scala Macro Tools
提交
5326f4ee
S
Scala Macro Tools
项目概览
梦境迷离
/
Scala Macro Tools
上一次同步 1 年多
通知
8
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Scala Macro Tools
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
5326f4ee
编写于
7月 29, 2022
作者:
梦境迷离
提交者:
GitHub
7月 29, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add TransformableSyntax (#231)
* add TransformableSyntax
上级
9c536329
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
70 addition
and
0 deletion
+70
-0
smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala
...rc/main/scala/org/bitlap/common/TransformableSyntax.scala
+16
-0
smt-common/src/main/scala/org/bitlap/common/package.scala
smt-common/src/main/scala/org/bitlap/common/package.scala
+5
-0
smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala
.../src/test/scala/org/bitlap/common/TransformableTest.scala
+49
-0
未找到文件。
smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala
0 → 100644
浏览文件 @
5326f4ee
package
org.bitlap.common
import
org.bitlap.common.internal.TransformerMacro
/** @author
* 梦境迷离
* @version 1.0,2022/7/29
*/
trait
TransformableSyntax
[
From
<:
Product
,
To
<:
Product
]
{
self
=>
val
transformer
:
Transformer
[
From
,
To
]
def
Syntax
:
Transformable
[
From
,
To
]
=
macro
TransformerMacro
.
applyImpl
[
From
,
To
]
implicit
val
_self
:
TransformableSyntax
[
From
,
To
]
=
self
}
smt-common/src/main/scala/org/bitlap/common/package.scala
浏览文件 @
5326f4ee
...
...
@@ -72,4 +72,9 @@ package object common {
implicit
final
class
TransformerOps
[
F
](
private
val
from
:
F
)
extends
AnyVal
{
final
def
transform
[
T
](
implicit
transformer
:
Transformer
[
F
,
T
])
:
T
=
transformer
.
transform
(
from
)
}
implicit
final
class
TransformableSyntaxOps
[
F
<:
Product
](
private
val
from
:
F
)
extends
AnyVal
{
final
def
transformCaseClass
[
T
<:
Product
](
implicit
transformableSyntax
:
TransformableSyntax
[
F
,
T
])
:
T
=
transformableSyntax
.
transformer
.
transform
(
from
)
}
}
smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala
浏览文件 @
5326f4ee
...
...
@@ -508,4 +508,53 @@ class TransformableTest extends AnyFlatSpec with Matchers {
| a.transform[A2](b1)
|"""
.
stripMargin
shouldNot
compile
}
"TransformableTest extends TransformableOps"
should
"compile ok"
in
{
case
class
A1
(
a
:
Option
[
String
],
b
:
Boolean
)
object
A1
extends
TransformableSyntax
[
A1
,
A2
]
{
override
val
transformer
:
Transformer
[
A1
,
A2
]
=
Syntax
.
setName
(
_
.
a
,
_
.
b
)
.
setName
(
_
.
b
,
_
.
c
)
.
setType
[
Boolean
,
String
](
_
.
b
,
_
.
toString
)
.
setDefaultValue
(
_
.
b
,
None
)
.
instance
}
case
class
A2
(
b
:
Option
[
String
],
c
:
String
,
e
:
Option
[
String
]
=
Some
(
"option"
)
)
val
a
=
A1
(
Some
(
"hello a"
),
false
)
a
.
transformCaseClass
[
A2
].
toString
shouldEqual
"A2(Some(hello a),false,Some(option))"
}
"TransformableTest extends TransformableOps"
should
"ok"
in
{
case
class
A1
(
d
:
Option
[
String
])
object
A1
extends
TransformableSyntax
[
A1
,
A2
]
{
override
val
transformer
:
Transformer
[
A1
,
A2
]
=
Syntax
.
setDefaultValue
(
_
.
f
,
Option
(
"1"
))
.
disableCollectionDefaultsToEmpty
// use default value, not None
.
disableOptionDefaultsToNone
// use default value, not Empty
.
instance
}
case
class
A2
(
d
:
Option
[
String
],
e
:
Option
[
String
]
=
Some
(
"option"
),
f
:
Option
[
String
]
=
None
,
h
:
List
[
String
]
=
List
(
"list"
),
i
:
List
[
String
]
=
List
.
empty
)
val
a
=
A1
(
Some
(
"hello a"
))
a
.
transformCaseClass
[
A2
].
toString
shouldEqual
"A2(Some(hello a),Some(option),Some(1),List(list),List())"
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录