From 7ff1f85cbd2d48a15db9f4721c027a6610416b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=A6=E5=A2=83=E8=BF=B7=E7=A6=BB?= Date: Fri, 29 Jul 2022 15:33:19 +0800 Subject: [PATCH 1/2] add TransformableSyntax --- .../bitlap/common/TransformableSyntax.scala | 37 ++++++++++++++ .../scala/org/bitlap/common/package.scala | 5 ++ .../org/bitlap/common/TransformableTest.scala | 49 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala diff --git a/smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala b/smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala new file mode 100644 index 00000000..58f8220c --- /dev/null +++ b/smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 bitlap + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +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 TransformWith: Transformable[From, To] = macro TransformerMacro.applyImpl[From, To] + + implicit val _self: TransformableSyntax[From, To] = self + +} diff --git a/smt-common/src/main/scala/org/bitlap/common/package.scala b/smt-common/src/main/scala/org/bitlap/common/package.scala index 86685e3a..04272566 100644 --- a/smt-common/src/main/scala/org/bitlap/common/package.scala +++ b/smt-common/src/main/scala/org/bitlap/common/package.scala @@ -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 transformBySyntax[T <: Product](implicit transformableSyntax: TransformableSyntax[F, T]): T = + transformableSyntax.transformer.transform(from) + } } diff --git a/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala b/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala index 4d30603f..31dca63b 100644 --- a/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala +++ b/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala @@ -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.transformBySyntax[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.transformBySyntax[A2].toString shouldEqual "A2(Some(hello a),Some(option),Some(1),List(list),List())" + + } } From ef545b9da935cfb39c69e1af0754874e281e5322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=A6=E5=A2=83=E8=BF=B7=E7=A6=BB?= Date: Fri, 29 Jul 2022 15:36:38 +0800 Subject: [PATCH 2/2] add TransformableSyntax --- .../bitlap/common/TransformableSyntax.scala | 23 +------------------ .../scala/org/bitlap/common/package.scala | 2 +- .../org/bitlap/common/TransformableTest.scala | 4 ++-- 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala b/smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala index 58f8220c..8d4b3d8a 100644 --- a/smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala +++ b/smt-common/src/main/scala/org/bitlap/common/TransformableSyntax.scala @@ -1,24 +1,3 @@ -/* - * Copyright (c) 2022 bitlap - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - package org.bitlap.common import org.bitlap.common.internal.TransformerMacro @@ -30,7 +9,7 @@ trait TransformableSyntax[From <: Product, To <: Product] { self => val transformer: Transformer[From, To] - def TransformWith: Transformable[From, To] = macro TransformerMacro.applyImpl[From, To] + def Syntax: Transformable[From, To] = macro TransformerMacro.applyImpl[From, To] implicit val _self: TransformableSyntax[From, To] = self diff --git a/smt-common/src/main/scala/org/bitlap/common/package.scala b/smt-common/src/main/scala/org/bitlap/common/package.scala index 04272566..8b20fc15 100644 --- a/smt-common/src/main/scala/org/bitlap/common/package.scala +++ b/smt-common/src/main/scala/org/bitlap/common/package.scala @@ -74,7 +74,7 @@ package object common { } implicit final class TransformableSyntaxOps[F <: Product](private val from: F) extends AnyVal { - final def transformBySyntax[T <: Product](implicit transformableSyntax: TransformableSyntax[F, T]): T = + final def transformCaseClass[T <: Product](implicit transformableSyntax: TransformableSyntax[F, T]): T = transformableSyntax.transformer.transform(from) } } diff --git a/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala b/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala index 31dca63b..cdcfebd9 100644 --- a/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala +++ b/smt-common/src/test/scala/org/bitlap/common/TransformableTest.scala @@ -529,7 +529,7 @@ class TransformableTest extends AnyFlatSpec with Matchers { ) val a = A1(Some("hello a"), false) - a.transformBySyntax[A2].toString shouldEqual "A2(Some(hello a),false,Some(option))" + a.transformCaseClass[A2].toString shouldEqual "A2(Some(hello a),false,Some(option))" } "TransformableTest extends TransformableOps" should "ok" in { @@ -554,7 +554,7 @@ class TransformableTest extends AnyFlatSpec with Matchers { val a = A1(Some("hello a")) - a.transformBySyntax[A2].toString shouldEqual "A2(Some(hello a),Some(option),Some(1),List(list),List())" + a.transformCaseClass[A2].toString shouldEqual "A2(Some(hello a),Some(option),Some(1),List(list),List())" } }