|
| 1 | +package scalapb_circe |
| 2 | + |
| 3 | +import io.circe.syntax._ |
| 4 | +import io.circe.{Decoder, Encoder, Json} |
| 5 | +import jsontest.anytests.AnyTest |
| 6 | +import jsontest.custom_collection.{Guitar, Studio} |
| 7 | +import jsontest.issue315.{Bar, Foo, Msg} |
| 8 | +import jsontest.test.MyEnum |
| 9 | +import jsontest.test3.MyTest3.MyEnum3 |
| 10 | +import org.scalatest.Assertion |
| 11 | +import org.scalatest.freespec.AnyFreeSpec |
| 12 | +import org.scalatest.matchers.must.Matchers |
| 13 | +import scalapb.{GeneratedEnum, GeneratedEnumCompanion, GeneratedMessage, GeneratedMessageCompanion} |
| 14 | +import scalapb_circe.codec._ |
| 15 | + |
| 16 | +class CodecSpec extends AnyFreeSpec with Matchers { |
| 17 | + |
| 18 | + "GeneratedMessage" - { |
| 19 | + "encode to same value via codec and JsonFormat" in { |
| 20 | + def check[M <: GeneratedMessage](m: M): Assertion = { |
| 21 | + val encodedWithJsonFormat = JsonFormat.toJson(m) |
| 22 | + val encodedImplicitly = m.asJson |
| 23 | + encodedImplicitly mustBe encodedWithJsonFormat |
| 24 | + } |
| 25 | + |
| 26 | + check(AnyTest("foo")) |
| 27 | + check(Guitar(99)) |
| 28 | + check(Studio(Set(Guitar(1), Guitar(2), Guitar(3)))) |
| 29 | + check( |
| 30 | + Msg( |
| 31 | + baz = "bazzz", |
| 32 | + someUnion = Msg.SomeUnion.Foo(Foo("fooooo")) |
| 33 | + ) |
| 34 | + ) |
| 35 | + check( |
| 36 | + Msg( |
| 37 | + baz = "bazzz", |
| 38 | + someUnion = Msg.SomeUnion.Bar(Bar("fooooo")) |
| 39 | + ) |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + "decode to same value via codec and JsonFormat" in { |
| 44 | + def check[M <: GeneratedMessage: GeneratedMessageCompanion](m: M): Assertion = { |
| 45 | + val decodedWithJsonFormat = JsonFormat.fromJson(JsonFormat.printer.toJson(m)) |
| 46 | + val decodedImplicitly = m.asJson.as[M] |
| 47 | + decodedImplicitly mustBe Right(decodedWithJsonFormat) |
| 48 | + } |
| 49 | + |
| 50 | + check(AnyTest("foo")) |
| 51 | + check(Guitar(99)) |
| 52 | + check(Studio(Set(Guitar(1), Guitar(2), Guitar(3)))) |
| 53 | + check( |
| 54 | + Msg( |
| 55 | + baz = "bazzz", |
| 56 | + someUnion = Msg.SomeUnion.Foo(Foo("fooooo")) |
| 57 | + ) |
| 58 | + ) |
| 59 | + check( |
| 60 | + Msg( |
| 61 | + baz = "bazzz", |
| 62 | + someUnion = Msg.SomeUnion.Bar(Bar("fooooo")) |
| 63 | + ) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + "encode using an implicit printer w/ non-standard settings" in { |
| 68 | + implicit val printer: Printer = new Printer(includingDefaultValueFields = true) |
| 69 | + |
| 70 | + // Create a proto with a default value (0). |
| 71 | + val g = Guitar(0) |
| 72 | + |
| 73 | + // Using regular JsonFormat yields an empty object because 0 is the default value. |
| 74 | + JsonFormat.toJson(g) mustBe Json.obj() |
| 75 | + |
| 76 | + // Using asJson with an implicit printer includes the default value. |
| 77 | + g.asJson mustBe Json.obj("numberOfStrings" -> Json.fromInt(0)) |
| 78 | + } |
| 79 | + |
| 80 | + "decode using an implicit parser w/ non-standard settings" in { |
| 81 | + implicit val parser: Parser = new Parser(preservingProtoFieldNames = true) |
| 82 | + |
| 83 | + // Use the snake-case naming to define a Guitar Json object. |
| 84 | + val j = Json.obj("number_of_strings" -> Json.fromInt(42)) |
| 85 | + |
| 86 | + // Using the regular JsonFormat parser decodes to the defaultInstance. |
| 87 | + JsonFormat.fromJson[Guitar](j) mustBe Guitar.defaultInstance |
| 88 | + |
| 89 | + // Using as[T] with an implicit parser decodes back to the original value (42). |
| 90 | + j.as[Guitar] mustBe Right(Guitar(42)) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + "GeneratedEnum" - { |
| 95 | + "encode to same value via codec and JsonFormat" in { |
| 96 | + def check[E <: GeneratedEnum: GeneratedEnumCompanion](e: E): Assertion = { |
| 97 | + val encodedWithJsonFormat = JsonFormat.printer.serializeEnum(e.scalaValueDescriptor) |
| 98 | + val encodeImplicitly = e.asJson |
| 99 | + encodeImplicitly mustBe encodedWithJsonFormat |
| 100 | + } |
| 101 | + MyEnum.values.foreach(check(_: MyEnum)) |
| 102 | + MyEnum3.values.foreach(check(_: MyEnum3)) |
| 103 | + } |
| 104 | + |
| 105 | + "decode to same value via codec and JsonFormat" in { |
| 106 | + def check[E <: GeneratedEnum: GeneratedEnumCompanion](e: E): Assertion = { |
| 107 | + val cmp = implicitly[GeneratedEnumCompanion[E]] |
| 108 | + val decodedWithJsonFormat = cmp.fromValue( |
| 109 | + JsonFormat.parser |
| 110 | + .defaultEnumParser(cmp.scalaDescriptor, JsonFormat.printer.serializeEnum(e.scalaValueDescriptor)) |
| 111 | + .index |
| 112 | + ) |
| 113 | + val decodedImplicitly = e.asJson.as[E] |
| 114 | + decodedImplicitly mustBe Right(decodedWithJsonFormat) |
| 115 | + } |
| 116 | + MyEnum.values.foreach(check(_: MyEnum)) |
| 117 | + MyEnum3.values.foreach(check(_: MyEnum3)) |
| 118 | + } |
| 119 | + |
| 120 | + "encode using an implicit printer w/ non-standard settings" in { |
| 121 | + implicit val printer = new Printer(formattingEnumsAsNumber = true) |
| 122 | + def check[E <: GeneratedEnum: GeneratedEnumCompanion](e: E): Assertion = { |
| 123 | + e.asJson mustBe Json.fromInt(e.value) |
| 124 | + } |
| 125 | + MyEnum.values.foreach(check(_: MyEnum)) |
| 126 | + MyEnum3.values.foreach(check(_: MyEnum3)) |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + "Case class with GeneratedMessage and GeneratedEnum" - { |
| 131 | + "derive and use a semi-auto codec" in { |
| 132 | + |
| 133 | + import io.circe.generic.semiauto._ |
| 134 | + import io.circe.syntax._ |
| 135 | + |
| 136 | + case class Band(version: MyEnum, guitars: Seq[Guitar]) |
| 137 | + |
| 138 | + object Band { |
| 139 | + implicit val dec: Decoder[Band] = deriveDecoder[Band] |
| 140 | + implicit val enc: Encoder[Band] = deriveEncoder[Band] |
| 141 | + } |
| 142 | + |
| 143 | + val band = Band(MyEnum.V1, Seq(Guitar(4), Guitar(5))) |
| 144 | + val json = Json.obj( |
| 145 | + "version" -> Json.fromString("V1"), |
| 146 | + "guitars" -> Json.arr( |
| 147 | + Json.obj("numberOfStrings" -> Json.fromInt(4)), |
| 148 | + Json.obj("numberOfStrings" -> Json.fromInt(5)) |
| 149 | + ) |
| 150 | + ) |
| 151 | + band.asJson mustBe json |
| 152 | + json.as[Band] mustBe Right(band) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments