forked from lloydmeta/enumeratum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Circe KeyEncoder/KeyDecoder for Enum (lloydmeta#219)
- Loading branch information
Showing
4 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
enumeratum-circe/src/main/scala/enumeratum/CirceKeyEnum.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package enumeratum | ||
import io.circe.{KeyEncoder, KeyDecoder} | ||
|
||
/** | ||
* Helper trait that adds implicit Circe KeyEncoder/KeyDecoder for an [[Enum]]'s members. | ||
*/ | ||
trait CirceKeyEnum[A <: EnumEntry] { this: Enum[A] => | ||
implicit val circeKeyEncoder: KeyEncoder[A] = Circe.keyEncoder(this) | ||
implicit val circeKeyDecoder: KeyDecoder[A] = Circe.keyDecoder(this) | ||
} |
28 changes: 28 additions & 0 deletions
28
enumeratum-circe/src/test/scala/enumeratum/CirceKeySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package enumeratum | ||
|
||
import org.scalatest.{FunSpec, Matchers} | ||
import cats.syntax.either._ | ||
import io.circe.Json | ||
import io.circe.syntax._ | ||
|
||
class CirceKeySpec extends FunSpec with Matchers { | ||
describe("to JSON") { | ||
it("should work") { | ||
Map(ShirtSize.Small -> 5, ShirtSize.Large -> 10).asJson shouldBe Json.obj( | ||
"Small" -> 5.asJson, | ||
"Large" -> 10.asJson | ||
) | ||
} | ||
} | ||
|
||
describe("from JSON") { | ||
it("should work") { | ||
Json | ||
.obj( | ||
"Medium" -> 100.asJson, | ||
"Large" -> 15.asJson | ||
) | ||
.as[Map[ShirtSize, Int]] shouldBe Map(ShirtSize.Medium -> 100, ShirtSize.Large -> 15).asRight | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters