Skip to content

Commit

Permalink
Migrate EventBody to FsCodec 3 conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed May 12, 2022
1 parent 1dbb49c commit fb83a76
Show file tree
Hide file tree
Showing 59 changed files with 255 additions and 259 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The `Unreleased` section name is replaced by the expected version of next releas
- `CosmosStore`: Only log `bytes` when log level is `Debug` [#305](https://github.com/jet/equinox/pull/305)
- `EventStore`: Target `EventStore.Client` v `22.0.0-preview`; rename `Connector` -> `EventStoreConnector` [#317](https://github.com/jet/equinox/pull/317)
- Update all non-Client dependencies except `FSharp.Core`, `FSharp.Control.AsyncSeq` [#310](https://github.com/jet/equinox/pull/310)
- Update all Stores to use `FsCodec` v `3.0.0`, with [`EventBody` types switching from `byte[]` to `ReadOnlyMemory<byte>`, see FsCodec#75](https://github.com/jet/FsCodec/pull/75) [#323](https://github.com/jet/equinox/pull/323)

### Removed

Expand Down
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<PackageLicense>Apache-2.0</PackageLicense>
<Copyright>Copyright © 2016-22</Copyright>

<WarningLevel>5</WarningLevel>

<ThisDirAbsolute>$([System.IO.Path]::GetFullPath("$(MSBuildThisFileDirectory)"))</ThisDirAbsolute>

<!-- SourceLink related properties https://github.com/dotnet/SourceLink#using-sourcelink -->
Expand Down
4 changes: 1 addition & 3 deletions samples/Infrastructure/Infrastructure.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<WarningLevel>5</WarningLevel>
<IsTestProject>false</IsTestProject>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<DisableImplicitSystemValueTupleReference>true</DisableImplicitSystemValueTupleReference>
</PropertyGroup>
Expand All @@ -27,7 +25,7 @@
<ItemGroup>
<PackageReference Include="Argu" Version="6.1.1" />
<PackageReference Include="Destructurama.FSharp" Version="1.2.0" />
<PackageReference Include="FSharp.Core" Version="4.3.4" />
<PackageReference Include="FSharp.Core" Version="4.5.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" />
Expand Down
4 changes: 2 additions & 2 deletions samples/Infrastructure/Services.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Samples.Infrastructure.Services

open Domain
open FsCodec.SystemTextJson
open FsCodec.SystemTextJson.Interop // use ToJsonElementCodec because we are doing an overkill example
open Microsoft.Extensions.DependencyInjection
open System

type StreamResolver(storage) =
member _.Resolve
( codec : FsCodec.IEventCodec<'event,byte[],_>,
( codec : FsCodec.IEventCodec<'event, ReadOnlyMemory<byte>, _>,
fold: 'state -> 'event seq -> 'state,
initial : 'state,
snapshot : ('event -> bool) * ('state -> 'event)) =
Expand Down
2 changes: 1 addition & 1 deletion samples/Infrastructure/Storage.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ open System
[<RequireQualifiedAccess; NoEquality; NoComparison>]
type StorageConfig =
// For MemoryStore, we keep the events as UTF8 arrays - we could use FsCodec.Codec.Box to remove the JSON encoding, which would improve perf but can conceal problems
| Memory of Equinox.MemoryStore.VolatileStore<byte[]>
| Memory of Equinox.MemoryStore.VolatileStore<ReadOnlyMemory<byte>>
| Cosmos of Equinox.CosmosStore.CosmosStoreContext * Equinox.CosmosStore.CachingStrategy * unfolds: bool
| Es of Equinox.EventStore.EventStoreContext * Equinox.EventStore.CachingStrategy option * unfolds: bool
| Sql of Equinox.SqlStreamStore.SqlStreamStoreContext * Equinox.SqlStreamStore.CachingStrategy option * unfolds: bool
Expand Down
2 changes: 0 additions & 2 deletions samples/Store/Domain.Tests/Domain.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<WarningLevel>5</WarningLevel>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions samples/Store/Domain/Cart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module Events =
| ItemQuantityChanged of ItemQuantityChangedInfo
| ItemPropertiesChanged of ItemPropertiesChangedInfo
interface TypeShape.UnionContract.IUnionContract
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>()
let codecStj = FsCodec.SystemTextJson.Codec.Create<Event>()
let codec = EventCodec.create<Event>()
let codecStj = EventCodec.createJson<Event>()

module Fold =

Expand Down
4 changes: 2 additions & 2 deletions samples/Store/Domain/ContactPreferences.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module Events =
type Event =
| [<System.Runtime.Serialization.DataMember(Name = "contactPreferencesChanged")>]Updated of Value
interface TypeShape.UnionContract.IUnionContract
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>()
let codecStj = FsCodec.SystemTextJson.Codec.Create<Event>()
let codec = EventCodec.create<Event>()
let codecStj = EventCodec.createJson<Event>()

module Fold =

Expand Down
8 changes: 3 additions & 5 deletions samples/Store/Domain/Domain.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<WarningLevel>5</WarningLevel>
<IsTestProject>false</IsTestProject>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<DisableImplicitSystemValueTupleReference>true</DisableImplicitSystemValueTupleReference>
</PropertyGroup>
Expand All @@ -19,10 +17,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.3.4" />
<PackageReference Include="FSharp.Core" Version="4.5.4" />

<PackageReference Include="FsCodec.NewtonsoftJson" Version="2.3.2" />
<PackageReference Include="FsCodec.SystemTextJson" Version="2.3.2" />
<PackageReference Include="FsCodec.NewtonsoftJson" Version="3.0.0-rc.2" />
<PackageReference Include="FsCodec.SystemTextJson" Version="3.0.0-rc.2" />

<ProjectReference Include="..\..\..\src\Equinox\Equinox.fsproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions samples/Store/Domain/Favorites.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module Events =
| Favorited of Favorited
| Unfavorited of Unfavorited
interface TypeShape.UnionContract.IUnionContract
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>()
let codecStj = FsCodec.SystemTextJson.Codec.Create<Event>()
let codec = EventCodec.create<Event>()
let codecStj = EventCodec.createJson<Event>()

module Fold =

Expand Down
13 changes: 11 additions & 2 deletions samples/Store/Domain/Infrastructure.fs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[<AutoOpen>]
module Domain.Infrastructure

open FsCodec.NewtonsoftJson
open FSharp.UMX
open Newtonsoft.Json
open System
Expand Down Expand Up @@ -40,7 +39,7 @@ type SkuId private (id : string) =
[<Obsolete>] new() = SkuId(Guid.NewGuid())
/// Represent as a Guid.ToString("N") output externally
and private SkuIdJsonConverter() =
inherit JsonIsomorphism<SkuId, string>()
inherit FsCodec.NewtonsoftJson.JsonIsomorphism<SkuId, string>()
/// Renders as per `Guid.ToString("N")`, i.e. no dashes
override _.Pickle value = string value
/// Input must be a `Guid.Parse`able value
Expand Down Expand Up @@ -75,3 +74,13 @@ module ClientId = let toString (value : ClientId) : string = Guid.toStringN %val
type InventoryItemId = Guid<inventoryItemId>
and [<Measure>] inventoryItemId
module InventoryItemId = let toString (value : InventoryItemId) : string = Guid.toStringN %value

module EventCodec =

/// For CosmosStore - we encode to JsonElement as that's what the store talks
let createJson<'t when 't :> TypeShape.UnionContract.IUnionContract> () =
FsCodec.SystemTextJson.CodecJsonElement.Create<'t>()

/// For stores other than CosmosStore, we encode to UTF-8 and have the store do the right thing
let create<'t when 't :> TypeShape.UnionContract.IUnionContract> () =
FsCodec.NewtonsoftJson.Codec.Create<'t>()
2 changes: 1 addition & 1 deletion samples/Store/Domain/SavedForLater.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Events =
/// Addition of a collection of skus to the list
| Added of Added
interface TypeShape.UnionContract.IUnionContract
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>()
let codec = EventCodec.create<Event>()

module Fold =
open Events
Expand Down
3 changes: 2 additions & 1 deletion samples/Store/Integration/CartIntegration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ open Domain
open Equinox
open Equinox.CosmosStore.Integration.CosmosFixtures
open Swensen.Unquote
open System

let fold, initial = Cart.Fold.fold, Cart.Fold.initial
let snapshot = Cart.Fold.isOrigin, Cart.Fold.snapshot

let createMemoryStore () = MemoryStore.VolatileStore<byte[]>()
let createMemoryStore () = MemoryStore.VolatileStore<ReadOnlyMemory<byte>>()
let createServiceMemory log store =
Cart.create log (MemoryStore.MemoryStoreCategory(store, Cart.Events.codec, fold, initial).Resolve)

Expand Down
7 changes: 4 additions & 3 deletions samples/Store/Integration/CodecIntegration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ let codec = FsCodec.NewtonsoftJson.Codec.Create()

[<AutoData(MaxTest=100)>]
let ``Can roundtrip, rendering correctly`` (x: SimpleDu) =
let serialized = codec.Encode(None,x)
render x =! if serialized.Data = null then null else System.Text.Encoding.UTF8.GetString(serialized.Data)
let adapted = FsCodec.Core.TimelineEvent.Create(-1L, serialized.EventType, serialized.Data)
let serialized = codec.Encode(None, x)
let d = serialized.Data
render x =! if d.IsEmpty then null else System.Text.Encoding.UTF8.GetString(d.Span)
let adapted = FsCodec.Core.TimelineEvent.Create(-1L, serialized.EventType, d)
let deserialized = codec.TryDecode adapted |> Option.get
deserialized =! x
3 changes: 0 additions & 3 deletions samples/Store/Integration/Integration.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<WarningLevel>5</WarningLevel>
<DisableImplicitSystemValueTupleReference>true</DisableImplicitSystemValueTupleReference>
</PropertyGroup>

Expand All @@ -29,7 +27,6 @@

<ItemGroup>
<PackageReference Include="FsCheck.xUnit" Version="2.16.4" />
<PackageReference Include="FsCodec.Box" Version="2.3.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Serilog.Sinks.Seq" Version="5.1.1" />
<PackageReference Include="unquote" Version="6.1.0" />
Expand Down
2 changes: 1 addition & 1 deletion samples/TodoBackend/Todo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Events =
| Cleared
| Snapshotted of Snapshotted
interface TypeShape.UnionContract.IUnionContract
let codec = FsCodec.NewtonsoftJson.Codec.Create<Event>()
let codec = EventCodec.create<Event> ()

module Fold =
type State = { items : Events.Todo list; nextId : int }
Expand Down
4 changes: 1 addition & 3 deletions samples/TodoBackend/TodoBackend.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<WarningLevel>5</WarningLevel>
<IsTestProject>false</IsTestProject>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
<DisableImplicitSystemValueTupleReference>true</DisableImplicitSystemValueTupleReference>
</PropertyGroup>
Expand All @@ -13,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.3.4" />
<PackageReference Include="FSharp.Core" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 11 additions & 9 deletions samples/Tutorial/AsAt.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// - the same general point applies to over-using querying of streams for read purposes as we do here;
// applying CQRS principles can often lead to a better model regardless of raw necessity

#if !LOCAL
#if LOCAL
// Compile Tutorial.fsproj by either a) right-clicking or b) typing
// dotnet build samples/Tutorial before attempting to send this to FSI with Alt-Enter
#if VISUALSTUDIO
Expand All @@ -20,10 +20,12 @@
#I "bin/Debug/net6.0/"
#r "Serilog.dll"
#r "Serilog.Sinks.Console.dll"
#r "Serilog.Sinks.Seq.dll"
#r "System.Configuration.ConfigurationManager.dll"
#r "Equinox.Core.dll"
#r "Newtonsoft.Json.dll"
#r "FSharp.UMX.dll"
#r "FsCodec.dll"
#r "Equinox.Core.dll"
#r "Equinox.dll"
#r "TypeShape.dll"
#r "FsCodec.SystemTextJson.dll"
Expand All @@ -38,9 +40,9 @@
#else
#r "nuget:Serilog.Sinks.Console"
#r "nuget:Serilog.Sinks.Seq"
#r "nuget:Equinox.CosmosStore"
#r "nuget:Equinox.EventStore"
#r "nuget:FsCodec.SystemTextJson"
#r "nuget:Equinox.CosmosStore, *-*"
#r "nuget:Equinox.EventStore, *-*"
#r "nuget:FsCodec.SystemTextJson, *-*"
#endif
open System

Expand All @@ -67,6 +69,7 @@ module Events =
e,None,None

// unlike most normal codecs, we have a mapping to supply as we want the Index to be added to each event so we can track it in the State as we fold
let codecJe = FsCodec.SystemTextJson.CodecJsonElement.Create(up,down)
let codec = FsCodec.SystemTextJson.Codec.Create(up,down)

module Fold =
Expand Down Expand Up @@ -122,7 +125,7 @@ type Service internal (resolve : string -> Equinox.Decider<Events.Event, Fold.St
member _.Add(clientId, count) = execute clientId (Add count)
member _.Remove(clientId, count) = execute clientId (Remove count)
member _.Read(clientId) = query clientId Fold.State.balance
member _.AsAt(clientId,index) = query clientId (fun state -> state.[index])
member _.AsAt(clientId,index) = query clientId (fun state -> state[index])

module Log =
open Serilog
Expand Down Expand Up @@ -159,8 +162,7 @@ module EventStore =
let cacheStrategy = CachingStrategy.SlidingWindow (cache, TimeSpan.FromMinutes 20.) // OR CachingStrategy.NoCaching
// rig snapshots to be injected as events into the stream every `snapshotWindow` events
let accessStrategy = AccessStrategy.RollingSnapshots (Fold.isValid,Fold.snapshot)
open FsCodec.SystemTextJson
let cat = EventStoreCategory(context, Events.codec.ToByteArrayCodec(), Fold.fold, Fold.initial, cacheStrategy, accessStrategy)
let cat = EventStoreCategory(context, Events.codec, Fold.fold, Fold.initial, cacheStrategy, accessStrategy)
let resolve id = Equinox.Decider(Log.log, cat.Resolve(streamName id), maxAttempts = 3)

module Cosmos =
Expand All @@ -174,7 +176,7 @@ module Cosmos =
let context = CosmosStoreContext(storeClient, tipMaxEvents = 10)
let cacheStrategy = CachingStrategy.SlidingWindow (cache, TimeSpan.FromMinutes 20.) // OR CachingStrategy.NoCaching
let accessStrategy = AccessStrategy.Snapshot (Fold.isValid,Fold.snapshot)
let category = CosmosStoreCategory(context, Events.codec, Fold.fold, Fold.initial, cacheStrategy, accessStrategy)
let category = CosmosStoreCategory(context, Events.codecJe, Fold.fold, Fold.initial, cacheStrategy, accessStrategy)
let resolve id = Equinox.Decider(Log.log, category.Resolve(streamName id), maxAttempts = 3)

//let serviceES = Service(EventStore.resolve)
Expand Down
5 changes: 3 additions & 2 deletions samples/Tutorial/Cosmos.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#else
#r "nuget:Serilog.Sinks.Console"
#r "nuget:Serilog.Sinks.Seq"
#r "nuget:Equinox.CosmosStore"
#r "nuget:Equinox.CosmosStore, *-*"
#r "nuget:FsCodec.SystemTextJson, *-*"
#endif

module Log =
Expand Down Expand Up @@ -51,7 +52,7 @@ module Favorites =
| Added of Item
| Removed of Item
interface TypeShape.UnionContract.IUnionContract
let codec = FsCodec.SystemTextJson.Codec.Create<Event>() // Coming soon, replace Newtonsoft with SystemTextJson and works same
let codec = FsCodec.SystemTextJson.CodecJsonElement.Create<Event>()

module Fold =

Expand Down
15 changes: 8 additions & 7 deletions samples/Tutorial/FulfilmentCenter.fsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#if LOCAL
#I "bin/Debug/netstandard2.1/"
#I "bin/Debug/net6.0/"
#r "Serilog.dll"
#r "Serilog.Sinks.Console.dll"
#r "Newtonsoft.Json.dll"
#r "TypeShape.dll"
#r "Equinox.dll"
#r "Equinox.Core.dll"
#r "FSharp.UMX.dll"
#r "FSCodec.dll"
#r "FsCodec.dll"
#r "TypeShape.dll"
#r "FsCodec.NewtonsoftJson.dll"
#r "FsCodec.SystemTextJson.dll"
#r "Microsoft.Azure.Cosmos.Client.dll"
#r "Microsoft.Azure.Cosmos.Direct.dll"
#r "System.Net.Http"
#r "Serilog.Sinks.Seq.dll"
#r "Equinox.CosmosStore.dll"
#else
#r "nuget:Equinox.MemoryStore"
#r "nuget:Equinox.CosmosStore"
#r "nuget:FsCodec.NewtonsoftJson"
#r "nuget:Equinox.MemoryStore, *-*"
#r "nuget:Equinox.CosmosStore, *-*"
#r "nuget:FsCodec.NewtonsoftJson, *-*"
#r "nuget:FsCodec.SystemTextJson, *-*"
#r "nuget:Serilog.Sinks.Console"
#r "nuget:Serilog.Sinks.Seq"
#endif
Expand Down
2 changes: 1 addition & 1 deletion samples/Tutorial/Gapless.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Events =
| Released of Item
| Snapshotted of Snapshotted
interface TypeShape.UnionContract.IUnionContract
let codec = FsCodec.SystemTextJson.Codec.Create<Event>()
let codec = FsCodec.SystemTextJson.CodecJsonElement.Create<Event>()

module Fold =

Expand Down
2 changes: 1 addition & 1 deletion samples/Tutorial/Index.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Events =
| Deleted of ItemIds
| Snapshotted of Items<'v>
interface TypeShape.UnionContract.IUnionContract
let codec<'v> = FsCodec.SystemTextJson.Codec.Create<Event<'v>>()
let codec<'v> = FsCodec.SystemTextJson.CodecJsonElement.Create<Event<'v>>()

module Fold =

Expand Down
12 changes: 11 additions & 1 deletion samples/Tutorial/Infrastructure.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ type IndexId = string<indexId>
and [<Measure>] indexId
module IndexId =
let parse (value : string) : IndexId = %value
let toString (value : IndexId) : string = %value
let toString (value : IndexId) : string = %value

module EventCodec =

/// For CosmosStore - we encode to JsonElement as that's what the store talks
let createJson<'t when 't :> TypeShape.UnionContract.IUnionContract> () =
FsCodec.SystemTextJson.CodecJsonElement.Create<'t>()

/// For stores other than CosmosStore, we encode to UTF-8 and have the store do the right thing
let create<'t when 't :> TypeShape.UnionContract.IUnionContract> () =
FsCodec.NewtonsoftJson.Codec.Create<'t>()
Loading

0 comments on commit fb83a76

Please sign in to comment.