-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial sketch of optics module #78
Conversation
597bed0
to
657bc22
Compare
Current coverage is
|
Example usage: import cats.data.Xor
import io.circe._, generic.auto._, jawn._, optics._
import monocle.macros.Lenses
@Lenses case class Foo(count: Int)
@Lenses case class Qux(foo: Foo, s: String)
val toQux = JsonPath.root.x.y.at(2).as[Qux]
val incrementCount = toQux.composeLens(Qux.foo).composeLens(Foo.count).modify(_ + 1)
val Xor.Right(json) = parse("""
{ "x" : { "y" : [0, 1, { "foo" : { "count" : 3 }, "s" : "abcde" }] } }
""") And then: scala> json
res0: io.circe.Json =
{
"x" : {
"y" : [
0,
1,
{
"foo" : {
"count" : 3
},
"s" : "abcde"
}
]
}
}
scala> incrementCount(json)
res1: io.circe.Json =
{
"x" : {
"y" : [
0,
1,
{
"s" : "abcde",
"foo" : {
"count" : 4
}
}
]
}
} |
def at(i: Int): JsonPath = | ||
JsonPath(opt.composePrism(jsonArray).composeOptional(index(i))) | ||
|
||
def as[A](implicit decode: Decoder[A], encode: Encoder[A]): Optional[Json, A] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that as
is not lawful because decoder
is not injective e.g.
case class Point(x: Int, y: Int)
val decoder: Decoder[Point] = ...
decoder.decodeJson(obj("x" -> int(1), "y" -> int(2))) == decoder.decodeJson(obj("x" -> int(1), "y" -> int(2), "z" -> int(5)))
9439c35
to
2ee2da9
Compare
2ee2da9
to
800782e
Compare
800782e
to
577023f
Compare
You can upgrade monocle to 1.2.0-M2 and define a |
6dbb713
to
dab1cc3
Compare
dab1cc3
to
6f18c78
Compare
I'm going to merge this when it's green on the understanding that the |
Initial sketch of optics module
The readme brought me here ("We are likely to add an experimental optics subproject in 0.3.0"), so given this is merged I think the readme should be updated. |
Don't define charBuilder where it's unneeded
This is a partial port from Argonaut in response to #39 and this question. If we can get some docs and tests in place before Shapeless 2.3.0 is out, I'd be happy to publish this with 0.2.0, but I don't want to wait for it.