-
Notifications
You must be signed in to change notification settings - Fork 7
Create caches for document encoders #91
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e1a666
Create caches for document encoders
ghostbuster91 fe98e4a
Remove unused imports
ghostbuster91 4732bbb
Remove redundant classes
ghostbuster91 dea62f1
Apply review comments
ghostbuster91 eb59f11
add client side error handling
ghostbuster91 e8b3f2c
Mark impl details package private
ghostbuster91 bb9c14b
minor simplification
kubukoz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
47 changes: 47 additions & 0 deletions
47
modules/smithy4s/src/main/scala/jsonrpclib/smithy4sinterop/CirceDecoderImpl.scala
This file contains hidden or 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,47 @@ | ||
package jsonrpclib.smithy4sinterop | ||
|
||
import io.circe.{Decoder => CirceDecoder, _} | ||
import smithy4s.codecs.PayloadPath | ||
import smithy4s.schema.CachedSchemaCompiler | ||
import smithy4s.Document | ||
import smithy4s.Document.{Encoder => _, _} | ||
import smithy4s.Schema | ||
|
||
private[smithy4sinterop] class CirceDecoderImpl extends CachedSchemaCompiler[CirceDecoder] { | ||
val decoder: CachedSchemaCompiler.DerivingImpl[Decoder] = Document.Decoder | ||
|
||
type Cache = decoder.Cache | ||
def createCache(): Cache = decoder.createCache() | ||
|
||
def fromSchema[A](schema: Schema[A], cache: Cache): CirceDecoder[A] = | ||
c => { | ||
c.as[Json] | ||
.map(fromJson(_)) | ||
.flatMap { d => | ||
decoder | ||
.fromSchema(schema, cache) | ||
.decode(d) | ||
.left | ||
.map(e => | ||
DecodingFailure(DecodingFailure.Reason.CustomReason(e.getMessage), c.history ++ toCursorOps(e.path)) | ||
) | ||
} | ||
} | ||
|
||
def fromSchema[A](schema: Schema[A]): CirceDecoder[A] = fromSchema(schema, createCache()) | ||
|
||
private def toCursorOps(path: PayloadPath): List[CursorOp] = | ||
path.segments.map { | ||
case PayloadPath.Segment.Label(name) => CursorOp.DownField(name) | ||
case PayloadPath.Segment.Index(i) => CursorOp.DownN(i) | ||
} | ||
|
||
private def fromJson(json: Json): Document = json.fold( | ||
jsonNull = DNull, | ||
jsonBoolean = DBoolean(_), | ||
jsonNumber = n => DNumber(n.toBigDecimal.get), | ||
jsonString = DString(_), | ||
jsonArray = arr => DArray(arr.map(fromJson)), | ||
jsonObject = obj => DObject(obj.toMap.view.mapValues(fromJson).toMap) | ||
) | ||
} |
28 changes: 28 additions & 0 deletions
28
modules/smithy4s/src/main/scala/jsonrpclib/smithy4sinterop/CirceEncoderImpl.scala
This file contains hidden or 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 jsonrpclib.smithy4sinterop | ||
|
||
import io.circe.{Encoder => CirceEncoder, _} | ||
import smithy4s.schema.CachedSchemaCompiler | ||
import smithy4s.Document | ||
import smithy4s.Document._ | ||
import smithy4s.Schema | ||
|
||
private[smithy4sinterop] class CirceEncoderImpl extends CachedSchemaCompiler[CirceEncoder] { | ||
val encoder: CachedSchemaCompiler.DerivingImpl[Encoder] = Document.Encoder | ||
|
||
type Cache = encoder.Cache | ||
def createCache(): Cache = encoder.createCache() | ||
|
||
def fromSchema[A](schema: Schema[A], cache: Cache): CirceEncoder[A] = | ||
a => documentToJson(encoder.fromSchema(schema, cache).encode(a)) | ||
|
||
def fromSchema[A](schema: Schema[A]): CirceEncoder[A] = fromSchema(schema, createCache()) | ||
|
||
private val documentToJson: Document => Json = { | ||
case DNull => Json.Null | ||
case DString(value) => Json.fromString(value) | ||
case DBoolean(value) => Json.fromBoolean(value) | ||
case DNumber(value) => Json.fromBigDecimal(value) | ||
case DArray(values) => Json.fromValues(values.map(documentToJson)) | ||
case DObject(entries) => Json.fromFields(entries.view.mapValues(documentToJson)) | ||
} | ||
} |
62 changes: 20 additions & 42 deletions
62
modules/smithy4s/src/main/scala/jsonrpclib/smithy4sinterop/CirceJsonCodec.scala
This file contains hidden or 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 |
---|---|---|
@@ -1,55 +1,33 @@ | ||
package jsonrpclib.smithy4sinterop | ||
|
||
import io.circe._ | ||
import smithy4s.codecs.PayloadPath | ||
import smithy4s.Document | ||
import smithy4s.Document.{Decoder => _, _} | ||
import smithy4s.schema.CachedSchemaCompiler | ||
import smithy4s.Schema | ||
|
||
object CirceJsonCodec { | ||
|
||
object Encoder extends CirceEncoderImpl | ||
object Decoder extends CirceDecoderImpl | ||
|
||
object Codec extends CachedSchemaCompiler[Codec] { | ||
type Cache = (Encoder.Cache, Decoder.Cache) | ||
def createCache(): Cache = (Encoder.createCache(), Decoder.createCache()) | ||
|
||
def fromSchema[A](schema: Schema[A]): Codec[A] = | ||
io.circe.Codec.from(Decoder.fromSchema(schema), Encoder.fromSchema(schema)) | ||
|
||
def fromSchema[A](schema: Schema[A], cache: Cache): Codec[A] = | ||
io.circe.Codec.from( | ||
Decoder.fromSchema(schema, cache._2), | ||
Encoder.fromSchema(schema, cache._1) | ||
) | ||
} | ||
|
||
/** Creates a Circe `Codec[A]` from a Smithy4s `Schema[A]`. | ||
* | ||
* This enables encoding values of type `A` to JSON and decoding JSON back into `A`, using the structure defined by | ||
* the Smithy schema. | ||
*/ | ||
def fromSchema[A](implicit schema: Schema[A]): Codec[A] = Codec.from( | ||
c => { | ||
c.as[Json] | ||
.map(fromJson) | ||
.flatMap { d => | ||
Document | ||
.decode[A](d) | ||
.left | ||
.map(e => | ||
DecodingFailure(DecodingFailure.Reason.CustomReason(e.getMessage), c.history ++ toCursorOps(e.path)) | ||
) | ||
} | ||
}, | ||
a => documentToJson(Document.encode(a)) | ||
) | ||
|
||
private def toCursorOps(path: PayloadPath): List[CursorOp] = | ||
path.segments.map { | ||
case PayloadPath.Segment.Label(name) => CursorOp.DownField(name) | ||
case PayloadPath.Segment.Index(i) => CursorOp.DownN(i) | ||
} | ||
|
||
private val documentToJson: Document => Json = { | ||
case DNull => Json.Null | ||
case DString(value) => Json.fromString(value) | ||
case DBoolean(value) => Json.fromBoolean(value) | ||
case DNumber(value) => Json.fromBigDecimal(value) | ||
case DArray(values) => Json.fromValues(values.map(documentToJson)) | ||
case DObject(entries) => Json.fromFields(entries.view.mapValues(documentToJson)) | ||
} | ||
|
||
private def fromJson(json: Json): Document = json.fold( | ||
jsonNull = DNull, | ||
jsonBoolean = DBoolean(_), | ||
jsonNumber = n => DNumber(n.toBigDecimal.get), | ||
jsonString = DString(_), | ||
jsonArray = arr => DArray(arr.map(fromJson)), | ||
jsonObject = obj => DObject(obj.toMap.view.mapValues(fromJson).toMap) | ||
) | ||
def fromSchema[A](implicit schema: Schema[A]): Codec[A] = | ||
Codec.fromSchema(schema) | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.