Skip to content

Commit

Permalink
Use Circe Extras To Consider Default Values In JSON Codecs
Browse files Browse the repository at this point in the history
Merge pull request #8 from ChetanBhasin/default-codecs
  • Loading branch information
ChetanBhasin authored May 15, 2020
2 parents a39095a + 234106d commit f57a033
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ lazy val sharedDependencies =
circeRefined,
circeCore,
circeParser,
circeExtras,
circeGeneric,
circeDerivation,
scalaCompat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ object ScalaGenerator {
s"""
|package $packageName\n
|import io.circe._
|import io.circe.derivation._\n${refinementImports.getOrElse("")}
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._\n${refinementImports.getOrElse("")}
|$docs
|final case class $typeName${values
.map(
Expand All @@ -184,6 +185,7 @@ object ScalaGenerator {
)
.mkString("(\n\t", ",\n\t", "\n)")} \n
|object $typeName {
|\timplicit val customConfig = Configuration.default.withDefaults
|\timplicit val circeDecoder: Decoder[$typeName] = deriveDecoder[$typeName](renaming.snakeCase, true, None)
|\timplicit val circeEncoder: Encoder[$typeName] = deriveEncoder[$typeName](renaming.snakeCase, None)${refinedCode
.getOrElse("")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|/**
|* Test Summary
|**/
|final case class Person(name: String, age: Int)
|
|object Person {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[Person] = deriveDecoder[Person](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[Person] = deriveEncoder[Person](renaming.snakeCase, None)
|}
Expand All @@ -61,11 +63,13 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|final case class Person()
|
|object Person {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[Person] = deriveDecoder[Person](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[Person] = deriveEncoder[Person](renaming.snakeCase, None)
|}
Expand Down Expand Up @@ -93,11 +97,13 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|final case class Person(name: com.enfore.apis.Name, age: Int)
|
|object Person {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[Person] = deriveDecoder[Person](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[Person] = deriveEncoder[Person](renaming.snakeCase, None)
|}
Expand Down Expand Up @@ -148,11 +154,13 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|final case class Person(name: com.enfore.apis.Name = com.enfore.apis.Name.MAGIC, age: Int)
|
|object Person {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[Person] = deriveDecoder[Person](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[Person] = deriveEncoder[Person](renaming.snakeCase, None)
|}
Expand Down Expand Up @@ -184,7 +192,8 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|final case class ParamedType(optionalVal: Option[String],
| listVal: List[Int],
Expand All @@ -193,6 +202,7 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
| listOpVal: List[Option[Double]])
|
| object ParamedType {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[ParamedType] = deriveDecoder[ParamedType](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[ParamedType] = deriveEncoder[ParamedType](renaming.snakeCase, None)
| }
Expand Down Expand Up @@ -220,7 +230,8 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|import eu.timepit.refined._
|import eu.timepit.refined.api._
Expand All @@ -233,6 +244,7 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|final case class RefinedType(stringVal : String Refined AllOf[MinSize[W.`3`.T] :: MaxSize[W.`3`.T] :: HNil])
|
|object RefinedType {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[RefinedType] = deriveDecoder[RefinedType](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[RefinedType] = deriveEncoder[RefinedType](renaming.snakeCase, None)
| object RefinementConstructors {
Expand Down Expand Up @@ -316,7 +328,8 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|import eu.timepit.refined._
|import eu.timepit.refined.api._
Expand All @@ -329,6 +342,7 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|final case class RefinedType(listString : List[String] Refined AllOf[MinSize[W.`3`.T] :: MaxSize[W.`3`.T] :: HNil])
|
|object RefinedType {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[RefinedType] = deriveDecoder[RefinedType](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[RefinedType] = deriveEncoder[RefinedType](renaming.snakeCase, None)
| object RefinementConstructors {
Expand Down Expand Up @@ -363,7 +377,8 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|package com.enfore.apis
|
|import io.circe._
|import io.circe.derivation._
|import io.circe.generic.extras.Configuration
|import io.circe.generic.extras.semiauto._
|
|
|import eu.timepit.refined._
Expand All @@ -377,6 +392,7 @@ class ComponentsTypeReprSpec extends AnyFlatSpec with Matchers {
|final case class RefinedType(nested : Option[String Refined AllOf[MinSize[W.`2`.T] :: MaxSize[W.`2`.T] :: HNil]])
|
| object RefinedType {
| implicit val customConfig = Configuration.default.withDefaults
| implicit val circeDecoder: Decoder[RefinedType] = deriveDecoder[RefinedType](renaming.snakeCase, true, None)
| implicit val circeEncoder: Encoder[RefinedType] = deriveEncoder[RefinedType](renaming.snakeCase, None)
|
Expand Down
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Dependencies {
lazy val circeCore = "io.circe" %% "circe-core" % "0.13.0"
lazy val circeParser = "io.circe" %% "circe-parser" % "0.13.0"
lazy val circeGeneric = "io.circe" %% "circe-generic" % "0.13.0"
lazy val circeExtras = "io.circe" %% "circe-generic-extras" % "0.13.0"
lazy val circeYaml = "io.circe" %% "circe-yaml" % "0.13.0"
lazy val circeDerivation = "io.circe" %% "circe-derivation" % "0.13.0-M4"
lazy val scalaCompat = "org.scala-lang.modules" %% "scala-collection-compat" % "2.1.2"
Expand Down

0 comments on commit f57a033

Please sign in to comment.