diff --git a/src/FsCodec.Box/Compression.fs b/src/FsCodec.Box/Compression.fs index eb021e0..aaa7cf9 100644 --- a/src/FsCodec.Box/Compression.fs +++ b/src/FsCodec.Box/Compression.fs @@ -87,7 +87,7 @@ type Compression private () = static member EncodeTryCompress<'Event, 'Context>(native: IEventCodec<'Event, ReadOnlyMemory, 'Context>, [] ?options) : IEventCodec<'Event, EncodedBody, 'Context> = let opts = defaultArg options CompressionOptions.Default - FsCodec.Core.EventCodec.Map(native, (fun x -> Compression.Utf8ToEncodedTryCompress(opts, x)), Func<_, _> Compression.EncodedToUtf8) + FsCodec.Core.EventCodec.Map(native, (fun d -> Compression.Utf8ToEncodedTryCompress(opts, d)), Func<_, _> Compression.EncodedToUtf8) /// Adapts an IEventCodec rendering to ReadOnlyMemory<byte> Event Bodies to encode as per EncodeTryCompress, but without attempting compression. [] diff --git a/src/FsCodec.Box/FsCodec.Box.fsproj b/src/FsCodec.Box/FsCodec.Box.fsproj index 3c8ff02..93ebe76 100644 --- a/src/FsCodec.Box/FsCodec.Box.fsproj +++ b/src/FsCodec.Box/FsCodec.Box.fsproj @@ -24,6 +24,7 @@ + diff --git a/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj b/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj index 755312d..ee35d82 100644 --- a/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj +++ b/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj @@ -30,8 +30,10 @@ - - + + + + diff --git a/src/FsCodec.SystemTextJson/Interop.fs b/src/FsCodec.SystemTextJson/Interop.fs index 414db2e..5e6bb1e 100644 --- a/src/FsCodec.SystemTextJson/Interop.fs +++ b/src/FsCodec.SystemTextJson/Interop.fs @@ -21,7 +21,6 @@ type InteropHelpers private () = [] static member ToUtf8Codec<'Event, 'Context>(native: FsCodec.IEventCodec<'Event, JsonElement, 'Context>) : FsCodec.IEventCodec<'Event, ReadOnlyMemory, 'Context> = - FsCodec.Core.EventCodec.Map(native, Func<_, _> InteropHelpers.JsonElementToUtf8, Func<_, _> InteropHelpers.Utf8ToJsonElement) /// Adapts an IEventCodec that's rendering to ReadOnlyMemory<byte> Event Bodies to handle JsonElement bodies instead.
@@ -29,5 +28,4 @@ type InteropHelpers private () = [] static member ToJsonElementCodec<'Event, 'Context>(native: FsCodec.IEventCodec<'Event, ReadOnlyMemory, 'Context>) : FsCodec.IEventCodec<'Event, JsonElement, 'Context> = - FsCodec.Core.EventCodec.Map(native, Func<_, _> InteropHelpers.Utf8ToJsonElement, Func<_, _> InteropHelpers.JsonElementToUtf8) diff --git a/src/FsCodec/FsCodec.fs b/src/FsCodec/FsCodec.fs index 3f8e56e..ff203ed 100755 --- a/src/FsCodec/FsCodec.fs +++ b/src/FsCodec/FsCodec.fs @@ -41,6 +41,7 @@ namespace FsCodec.Core open FsCodec open System +open System.ComponentModel /// An Event about to be written, see IEventData for further information. [] @@ -61,8 +62,8 @@ type EventData<'Format>(eventType, data, meta, eventId, correlationId, causation member _.CausationId = causationId member _.Timestamp = timestamp - static member MapEx<'Mapped>(f: Func, 'Format, 'Mapped>) - (x: IEventData<'Format>): IEventData<'Mapped> = + static member MapBodies<'Mapped>(f: Func, 'Format, 'Mapped>): Func, IEventData<'Mapped>> = + Func<_, _>(fun x -> { new IEventData<'Mapped> with member _.EventType = x.EventType member _.Data = f.Invoke(x, x.Data) @@ -70,11 +71,22 @@ type EventData<'Format>(eventType, data, meta, eventId, correlationId, causation member _.EventId = x.EventId member _.CorrelationId = x.CorrelationId member _.CausationId = x.CausationId - member _.Timestamp = x.Timestamp } + member _.Timestamp = x.Timestamp }) - static member Map<'Mapped>(f: Func<'Format, 'Mapped>) - (x: IEventData<'Format>): IEventData<'Mapped> = - EventData.MapEx(Func<_, _, _>(fun _x -> f.Invoke)) x + // Original ugly signature + [] + static member Map<'Mapped>(f: Func<'Format, 'Mapped>) (x: IEventData<'Format>): IEventData<'Mapped> = + EventData.MapBodies(Func<_, _, _>(fun _x -> f.Invoke)).Invoke(x) + +/// F#-specific wrappers; for C#, use EventData.MapBodies directly +// These helper modules may move up to the FsCodec namespace in V4, along with breaking changes moving IsUnfold and Context from ITimelineEvent to IEventData +// If you have helpers that should be in the box alongside these, raise an Issue please +module EventData = + + let mapBodies_<'Format, 'Mapped> (f: IEventData<'Format> -> 'Format -> 'Mapped) = + EventData.MapBodies(Func, 'Format, 'Mapped> f).Invoke + let mapBodies<'Format, 'Mapped> (f: 'Format -> 'Mapped) = + EventData.MapBodies(Func, 'Format, 'Mapped>(fun _ -> f)).Invoke /// An Event or Unfold that's been read from a Store and hence has a defined Index on the Event Timeline. [] @@ -108,41 +120,66 @@ type TimelineEvent<'Format>(index, eventType, data, meta, eventId, correlationId member _.CausationId = causationId member _.Timestamp = timestamp - static member Map<'Mapped>(f: Func<'Format, 'Mapped>) - (x: ITimelineEvent<'Format>): ITimelineEvent<'Mapped> = + static member MapBodies<'Mapped>(f: Func, 'Format, 'Mapped>): Func, ITimelineEvent<'Mapped>> = + Func<_, _>(fun x -> { new ITimelineEvent<'Mapped> with member _.Index = x.Index member _.IsUnfold = x.IsUnfold member _.Context = x.Context member _.Size = x.Size member _.EventType = x.EventType - member _.Data = f.Invoke x.Data - member _.Meta = f.Invoke x.Meta + member _.Data = f.Invoke(x, x.Data) + member _.Meta = f.Invoke(x, x.Meta) member _.EventId = x.EventId member _.CorrelationId = x.CorrelationId member _.CausationId = x.CausationId - member _.Timestamp = x.Timestamp } + member _.Timestamp = x.Timestamp }) + // Original ugly signature + [] + static member Map<'Mapped>(f: Func<'Format, 'Mapped>) (x: ITimelineEvent<'Format>): ITimelineEvent<'Mapped> = + TimelineEvent.MapBodies(Func<_, _, _>(fun _x -> f.Invoke)).Invoke(x) + +/// F#-specific wrappers; for C#, use TimelineEvent.MapBodies directly +module TimelineEvent = + + let mapBodies_<'Format, 'Mapped> (f: ITimelineEvent<'Format> -> 'Format -> 'Mapped) = + TimelineEvent.MapBodies(Func, 'Format, 'Mapped> f).Invoke + let mapBodies<'Format, 'Mapped> (f: 'Format -> 'Mapped) = + TimelineEvent.MapBodies(Func, 'Format, 'Mapped>(fun _ -> f)).Invoke [] type EventCodec<'Event, 'Format, 'Context> private () = - static member MapEx<'TargetFormat>(native: IEventCodec<'Event, 'Format, 'Context>, up: Func, 'Format,'TargetFormat>, down: Func<'TargetFormat, 'Format>) + static member MapBodies<'TargetFormat>( + native: IEventCodec<'Event, 'Format, 'Context>, + up: Func, 'Format, 'TargetFormat>, + down: Func<'TargetFormat, 'Format>) : IEventCodec<'Event, 'TargetFormat, 'Context> = - let upConvert = EventData.MapEx up - let downConvert = TimelineEvent.Map down + let upConvert = EventData.MapBodies up + let downConvert = TimelineEvent.MapBodies(fun _ x -> down.Invoke x) { new IEventCodec<'Event, 'TargetFormat, 'Context> with member _.Encode(context, event) = let encoded = native.Encode(context, event) - upConvert encoded + upConvert.Invoke encoded member _.Decode target = - let encoded = downConvert target + let encoded = downConvert.Invoke target native.Decode encoded } - static member Map<'TargetFormat>(native: IEventCodec<'Event, 'Format, 'Context>, up: Func<'Format,'TargetFormat>, down: Func<'TargetFormat, 'Format>) + // NOTE To be be replaced by MapBodies/EventCodec.mapBodies for symmetry with TimelineEvent and EventData + // TO BE be Obsoleted and whenever FsCodec.Box is next released + [] + static member Map<'TargetFormat>(native: IEventCodec<'Event, 'Format, 'Context>, up: Func<'Format, 'TargetFormat>, down: Func<'TargetFormat, 'Format>) : IEventCodec<'Event, 'TargetFormat, 'Context> = + EventCodec.MapBodies(native, Func<_, _, _>(fun _x -> up.Invoke), down) + +/// F#-specific wrappers; for C#, use EventCodec.MapBodies directly +module EventCodec = - EventCodec.MapEx(native, Func<_, _, _>(fun _x -> up.Invoke), down) + let mapBodies_ (up: IEventData<'Format> -> 'Format -> 'TargetFormat) (down: 'TargetFormat -> 'Format) x = + EventCodec<'Event, 'Format, 'Context>.MapBodies<'TargetFormat>(x, up, down) + let mapBodies (up: 'Format -> 'TargetFormat) (down: 'TargetFormat -> 'Format) x = + EventCodec<'Event, 'Format, 'Context>.MapBodies<'TargetFormat>(x, Func<_, _, _>(fun _ -> up), down)