diff --git a/CHANGELOG.md b/CHANGELOG.md index 89da0ee..f9df153 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ The `Unreleased` section name is replaced by the expected version of next releas ## [Unreleased] ### Added + +- `Core.EventData.MapEx`: Enable contextual encoding of bodies [#127](https://github.com/jet/FsCodec/pull/127) + ### Changed ### Removed ### Fixed diff --git a/src/FsCodec/FsCodec.fs b/src/FsCodec/FsCodec.fs index f811585..3f8e56e 100755 --- a/src/FsCodec/FsCodec.fs +++ b/src/FsCodec/FsCodec.fs @@ -61,17 +61,21 @@ type EventData<'Format>(eventType, data, meta, eventId, correlationId, causation member _.CausationId = causationId member _.Timestamp = timestamp - static member Map<'Mapped>(f: Func<'Format, 'Mapped>) + static member MapEx<'Mapped>(f: Func, 'Format, 'Mapped>) (x: IEventData<'Format>): IEventData<'Mapped> = { new IEventData<'Mapped> with 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 } + static member Map<'Mapped>(f: Func<'Format, 'Mapped>) + (x: IEventData<'Format>): IEventData<'Mapped> = + EventData.MapEx(Func<_, _, _>(fun _x -> f.Invoke)) x + /// An Event or Unfold that's been read from a Store and hence has a defined Index on the Event Timeline. [] type TimelineEvent<'Format>(index, eventType, data, meta, eventId, correlationId, causationId, timestamp, isUnfold, context, size) = @@ -90,7 +94,7 @@ type TimelineEvent<'Format>(index, eventType, data, meta, eventId, correlationId TimelineEvent(index, inner.EventType, inner.Data, inner.Meta, inner.EventId, inner.CorrelationId, inner.CausationId, inner.Timestamp, isUnfold, Option.toObj context, size) :> _ override _.ToString() = sprintf "%s %s @%i" (if isUnfold then "Unfold" else "Event") eventType index - + interface ITimelineEvent<'Format> with member _.Index = index member _.IsUnfold = isUnfold @@ -122,10 +126,10 @@ type TimelineEvent<'Format>(index, eventType, data, meta, eventId, correlationId [] type EventCodec<'Event, 'Format, 'Context> private () = - static member Map<'TargetFormat>(native: IEventCodec<'Event, 'Format, 'Context>, up: Func<'Format,'TargetFormat>, down: Func<'TargetFormat, 'Format>) + static member MapEx<'TargetFormat>(native: IEventCodec<'Event, 'Format, 'Context>, up: Func, 'Format,'TargetFormat>, down: Func<'TargetFormat, 'Format>) : IEventCodec<'Event, 'TargetFormat, 'Context> = - let upConvert = EventData.Map up + let upConvert = EventData.MapEx up let downConvert = TimelineEvent.Map down { new IEventCodec<'Event, 'TargetFormat, 'Context> with @@ -137,3 +141,8 @@ type EventCodec<'Event, 'Format, 'Context> private () = member _.Decode target = let encoded = downConvert target native.Decode encoded } + + static member Map<'TargetFormat>(native: IEventCodec<'Event, 'Format, 'Context>, up: Func<'Format,'TargetFormat>, down: Func<'TargetFormat, 'Format>) + : IEventCodec<'Event, 'TargetFormat, 'Context> = + + EventCodec.MapEx(native, Func<_, _, _>(fun _x -> up.Invoke), down)