-
Notifications
You must be signed in to change notification settings - Fork 19
STJ: Add ToByteArrayCodec/ToJsonElementCodec shims #48
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
9 commits
Select commit
Hold shift + click to select a range
bd8fcd1
Add ToByteArray/JsonElementCodec shims
bartelink c0f8757
Handle nulls
bartelink 4e94994
Cleanup interop test suite
bartelink 8bfcff3
Add commentary re not supporting non-record fields
bartelink a21f01a
Work around init order issues for net461
bartelink 566dbfa
remove mistake
bartelink d2fd83b
Roll back programming by coincidence
bartelink 97588c0
Tidy comments
bartelink 32ff08e
Alphabetize
bartelink 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
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,58 @@ | ||
| namespace FsCodec.SystemTextJson | ||
|
|
||
| open System.Runtime.CompilerServices | ||
| open System.Text.Json | ||
|
|
||
| [<Extension>] | ||
| type InteropExtensions = | ||
| static member private Adapt<'From, 'To, 'Event, 'Context> | ||
| ( native : FsCodec.IEventCodec<'Event, 'From, 'Context>, | ||
| up : 'From -> 'To, | ||
| down : 'To -> 'From) : FsCodec.IEventCodec<'Event, 'To, 'Context> = | ||
|
|
||
| { new FsCodec.IEventCodec<'Event, 'To, 'Context> with | ||
| member __.Encode(context, event) = | ||
| let encoded = native.Encode(context, event) | ||
| { new FsCodec.IEventData<_> with | ||
| member __.EventType = encoded.EventType | ||
| member __.Data = up encoded.Data | ||
| member __.Meta = up encoded.Meta | ||
| member __.EventId = encoded.EventId | ||
| member __.CorrelationId = encoded.CorrelationId | ||
| member __.CausationId = encoded.CausationId | ||
| member __.Timestamp = encoded.Timestamp } | ||
|
|
||
| member __.TryDecode encoded = | ||
| let mapped = | ||
| { new FsCodec.ITimelineEvent<_> with | ||
| member __.Index = encoded.Index | ||
| member __.IsUnfold = encoded.IsUnfold | ||
| member __.Context = encoded.Context | ||
| member __.EventType = encoded.EventType | ||
| member __.Data = down encoded.Data | ||
| member __.Meta = down encoded.Meta | ||
| member __.EventId = encoded.EventId | ||
| member __.CorrelationId = encoded.CorrelationId | ||
| member __.CausationId = encoded.CausationId | ||
| member __.Timestamp = encoded.Timestamp } | ||
| native.TryDecode mapped } | ||
|
|
||
| static member private MapFrom(x : byte[]) : JsonElement = | ||
| if x = null then JsonElement() | ||
| else JsonSerializer.Deserialize(System.ReadOnlySpan.op_Implicit x) | ||
| static member private MapTo(x: JsonElement) : byte[] = | ||
| if x.ValueKind = JsonValueKind.Undefined then null | ||
| else JsonSerializer.SerializeToUtf8Bytes(x, InteropExtensions.NoOverEscapingOptions) | ||
| // Avoid introduction of HTML escaping for things like quotes etc (as standard Options.Create() profile does) | ||
| static member private NoOverEscapingOptions = | ||
| System.Text.Json.JsonSerializerOptions(Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping) | ||
|
|
||
| [<Extension>] | ||
| static member ToByteArrayCodec<'Event, 'Context>(native : FsCodec.IEventCodec<'Event, JsonElement, 'Context>) | ||
| : FsCodec.IEventCodec<'Event, byte[], 'Context> = | ||
| InteropExtensions.Adapt(native, InteropExtensions.MapTo, InteropExtensions.MapFrom) | ||
|
|
||
| [<Extension>] | ||
| static member ToJsonElementCodec<'Event, 'Context>(native : FsCodec.IEventCodec<'Event, byte[], 'Context>) | ||
| : FsCodec.IEventCodec<'Event, JsonElement, 'Context> = | ||
| InteropExtensions.Adapt(native, InteropExtensions.MapFrom, InteropExtensions.MapTo) | ||
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
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,48 @@ | ||
| /// Covers interop with stores that manage event bodies as byte[] | ||
| module FsCodec.SystemTextJson.Tests.InteropTests | ||
|
|
||
| open FsCheck.Xunit | ||
| open Newtonsoft.Json | ||
| open Swensen.Unquote | ||
| open System | ||
| open Xunit | ||
|
|
||
| type Batch = FsCodec.NewtonsoftJson.Tests.VerbatimUtf8ConverterTests.Batch | ||
| type Union = FsCodec.NewtonsoftJson.Tests.VerbatimUtf8ConverterTests.Union | ||
| let mkBatch = FsCodec.NewtonsoftJson.Tests.VerbatimUtf8ConverterTests.mkBatch | ||
|
|
||
| let indirectCodec = FsCodec.SystemTextJson.Codec.Create() |> FsCodec.SystemTextJson.InteropExtensions.ToByteArrayCodec | ||
| let [<Fact>] ``encodes correctly`` () = | ||
| let input = Union.A { embed = "\"" } | ||
| let encoded = indirectCodec.Encode(None, input) | ||
| let e : Batch = mkBatch encoded | ||
| let res = JsonConvert.SerializeObject(e) | ||
| test <@ res.Contains """"d":{"embed":"\""}""" @> | ||
| let des = JsonConvert.DeserializeObject<Batch>(res) | ||
| let loaded = FsCodec.Core.TimelineEvent.Create(-1L, des.e.[0].c, des.e.[0].d) | ||
| let decoded = indirectCodec.TryDecode loaded |> Option.get | ||
| input =! decoded | ||
|
|
||
| type EmbeddedString = { embed : string } | ||
| type EmbeddedDateTimeOffset = { embed : DateTimeOffset } | ||
| type U = | ||
| // | S of string // Opens up some edge cases wrt handling missing/empty/null `d` fields in stores, but possible if you have time to shave that yak! | ||
| | EDto of EmbeddedDateTimeOffset | ||
| | ES of EmbeddedString | ||
| | N | ||
| interface TypeShape.UnionContract.IUnionContract | ||
|
|
||
| let defaultSettings = FsCodec.NewtonsoftJson.Settings.CreateDefault() // Test without converters, as that's what Equinox.Cosmos will do | ||
| let defaultEventCodec = FsCodec.NewtonsoftJson.Codec.Create<U>(defaultSettings) | ||
| let indirectCodecU = FsCodec.SystemTextJson.Codec.Create<U>() |> FsCodec.SystemTextJson.InteropExtensions.ToByteArrayCodec | ||
|
|
||
| let [<Property>] ``round-trips diverse bodies correctly`` (x: U, encodeDirect, decodeDirect) = | ||
| let encoder = if encodeDirect then defaultEventCodec else indirectCodecU | ||
| let decoder = if decodeDirect then defaultEventCodec else indirectCodecU | ||
| let encoded = encoder.Encode(None,x) | ||
| let e : Batch = mkBatch encoded | ||
| let ser = JsonConvert.SerializeObject(e, defaultSettings) | ||
| let des = JsonConvert.DeserializeObject<Batch>(ser, defaultSettings) | ||
| let loaded = FsCodec.Core.TimelineEvent.Create(-1L, des.e.[0].c, des.e.[0].d) | ||
| let decoded = decoder.TryDecode loaded |> Option.get | ||
| x =! decoded |
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.