Skip to content

Commit 1986595

Browse files
committed
Rename recently added Scala 2 macros and add tests for them
1 parent 12211f6 commit 1986595

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

jsoniter-scala-macros/shared/src/main/scala-2/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,6 @@ object JsonCodecMaker {
527527
*/
528528
def makeCirceLike[A]: JsonValueCodec[A] = macro Impl.makeCirceLike[A]
529529

530-
/**
531-
* Replacements for the `make` call preconfigured to behave as expected by openapi specifications.
532-
* */
533-
def makeOpenapiLike[A]: JsonValueCodec[A] = macro Impl.makeOpenapiLike[A]
534-
def makeOpenapiEnumLike[A]: JsonValueCodec[A] = macro Impl.makeOpenapiEnumLike[A]
535-
def makeOpenapiADTLikeDefaultMapping[A](discriminator: String): JsonValueCodec[A] = macro Impl.makeOpenapiADTLikeDefaultMapping[A]
536-
def makeOpenapiADTLike[A](discriminator: String, mapping: PartialFunction[String, String]): JsonValueCodec[A] = macro Impl.makeOpenapiADTLike[A]
537-
538530
/**
539531
* A replacement for the `make` call with the
540532
* `CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withTransientNone(false).withDiscriminatorFieldName(None).withAdtLeafClassNameMapper(x => enforce_snake_case(simpleClassName(x))).withFieldNameMapper(enforce_snake_case).withJavaEnumValueNameMapper(enforce_snake_case)`
@@ -545,6 +537,49 @@ object JsonCodecMaker {
545537
*/
546538
def makeCirceLikeSnakeCased[A]: JsonValueCodec[A] = macro Impl.makeCirceLikeSnakeCased[A]
547539

540+
/**
541+
* Replacements for the `make` call preconfigured to behave as expected by openapi specifications:
542+
* `CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withRequireCollectionFields(true).withAllowRecursiveTypes(true)`
543+
*
544+
* @tparam A a type that should be encoded and decoded by the derived codec
545+
* @return an instance of the derived codec
546+
*/
547+
def makeOpenapiLike[A]: JsonValueCodec[A] = macro Impl.makeOpenapiLike1[A]
548+
549+
/**
550+
* Replacements for the `make` call preconfigured to behave as expected by openapi specifications:
551+
* `CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withRequireCollectionFields(true).withAllowRecursiveTypes(true)`
552+
* with a privided discriminator field name.
553+
*
554+
* @tparam A a type that should be encoded and decoded by the derived codec
555+
* @param discriminatorFieldName a name of discriminator field
556+
* @return an instance of the derived codec
557+
*/
558+
def makeOpenapiLike[A](discriminatorFieldName: String): JsonValueCodec[A] = macro Impl.makeOpenapiLike2[A]
559+
560+
/**
561+
* Replacements for the `make` call preconfigured to behave as expected by openapi specifications:
562+
* `CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withRequireCollectionFields(true).withAllowRecursiveTypes(true)`
563+
* with a privided discriminator field name and an ADT leaf-class name mapper with an applied function
564+
* that truncates to simple class name
565+
*
566+
* @tparam A a type that should be encoded and decoded by the derived codec
567+
* @param discriminatorFieldName a name of discriminator field
568+
* @param adtLeafClassNameMapper the function of mapping from string of case class/object full name to string value of
569+
* discriminator field
570+
* @return an instance of the derived codec
571+
*/
572+
def makeOpenapiLike[A](discriminatorFieldName: String, adtLeafClassNameMapper: PartialFunction[String, String]): JsonValueCodec[A] = macro Impl.makeOpenapiLike3[A]
573+
574+
/**
575+
* Replacements for the `make` call preconfigured to behave as expected by openapi specifications:
576+
* `CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withRequireCollectionFields(true).withAllowRecursiveTypes(true).withDiscriminatorFieldName(scala.None))`
577+
*
578+
* @tparam A a type that should be encoded and decoded by the derived codec
579+
* @return an instance of the derived codec
580+
*/
581+
def makeOpenapiLikeWithoutDiscriminator[A]: JsonValueCodec[A] = macro Impl.makeOpenapiLikeWithoutDiscriminator[A]
582+
548583
/**
549584
* Derives a codec for JSON values for the specified type `A` and a provided derivation configuration.
550585
*
@@ -578,23 +613,25 @@ object JsonCodecMaker {
578613
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withTransientNone(false)
579614
.withDiscriminatorFieldName(None).withCirceLikeObjectEncoding(true))
580615

581-
def makeOpenapiLike[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
616+
def makeOpenapiLike1[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
582617
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
583618
.withRequireCollectionFields(true).withAllowRecursiveTypes(true))
584619

585-
def makeOpenapiEnumLike[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
620+
def makeOpenapiLike2[A: c.WeakTypeTag](c: blackbox.Context)(discriminatorFieldName: c.Expr[String]): c.Expr[JsonValueCodec[A]] =
586621
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
587-
.withRequireCollectionFields(true).withAllowRecursiveTypes(true).withDiscriminatorFieldName(scala.None))
622+
.withRequireCollectionFields(true).withAllowRecursiveTypes(true)
623+
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminatorFieldName.tree.duplicate))))))
588624

589-
def makeOpenapiADTLikeDefaultMapping[A: c.WeakTypeTag](c: blackbox.Context)(discriminator: c.Expr[String]): c.Expr[JsonValueCodec[A]] =
625+
def makeOpenapiLike3[A: c.WeakTypeTag](c: blackbox.Context)(discriminatorFieldName: c.Expr[String],
626+
adtLeafClassNameMapper: c.Expr[PartialFunction[String, String]]): c.Expr[JsonValueCodec[A]] =
590627
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
591628
.withRequireCollectionFields(true).withAllowRecursiveTypes(true)
592-
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminator.tree.duplicate))))))
629+
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminatorFieldName.tree.duplicate)))))
630+
.withAdtLeafClassNameMapper(x => c.eval(c.Expr[PartialFunction[String, String]](c.untypecheck(adtLeafClassNameMapper.tree.duplicate))).apply(simpleClassName(x))))
593631

594-
def makeOpenapiADTLike[A: c.WeakTypeTag](c: blackbox.Context)(discriminator: c.Expr[String], mapping: c.Expr[PartialFunction[String, String]]): c.Expr[JsonValueCodec[A]] =
632+
def makeOpenapiLikeWithoutDiscriminator[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
595633
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false)
596-
.withRequireCollectionFields(true).withAllowRecursiveTypes(true)
597-
.withRequireDiscriminatorFirst(false).withDiscriminatorFieldName(Some(c.eval(c.Expr[String](c.untypecheck(discriminator.tree.duplicate))))).withAdtLeafClassNameMapper(x => c.eval(c.Expr[PartialFunction[String, String]](c.untypecheck(mapping.tree.duplicate))).apply(com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker.simpleClassName(x))))
634+
.withRequireCollectionFields(true).withAllowRecursiveTypes(true).withDiscriminatorFieldName(scala.None))
598635

599636
def makeCirceLikeSnakeCased[A: c.WeakTypeTag](c: blackbox.Context): c.Expr[JsonValueCodec[A]] =
600637
make(c)(CodecMakerConfig.withTransientEmpty(false).withTransientDefault(false).withTransientNone(false)

jsoniter-scala-macros/shared/src/test/scala-2/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerCompileTimeEvalSpec.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
package com.github.plokhotnyuk.jsoniter_scala.macros
22

3+
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker._
34
import org.scalatest.exceptions.TestFailedException
45

56
class JsonCodecMakerCompileTimeEvalSpec extends VerifyingSpec {
67
"JsonCodecMaker.make" should {
8+
"serialize and deserialize ADTs with openapi-like formatting" in {
9+
sealed trait Enum
10+
11+
object EnumValue1 extends Enum
12+
13+
object EnumValue2 extends Enum
14+
15+
verifySerDeser(makeOpenapiLike[Enum], EnumValue1, """{"type":"EnumValue1"}""")
16+
verifySerDeser(makeOpenapiLike[Enum]("hint"), EnumValue2, """{"hint":"EnumValue2"}""")
17+
verifySerDeser(makeOpenapiLike[Enum]("hint", enforce_snake_case), EnumValue1, """{"hint":"enum_value_1"}""")
18+
verifySerDeser(makeOpenapiLikeWithoutDiscriminator[Enum], EnumValue2, """"EnumValue2"""")
19+
}
720
"don't generate codecs when a parameter of the 'make' call depends on not yet compiled code" in {
821
assert(intercept[TestFailedException](assertCompiles {
922
"""object A {

version.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ThisBuild / version := "2.35.4-SNAPSHOT"
1+
ThisBuild / version := "2.36.0-SNAPSHOT"

0 commit comments

Comments
 (0)