< Summary

Information
Class: Intentum.Persistence.Serialization.BehaviorEventDocument
Assembly: Intentum.Persistence
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Persistence/Serialization/BehaviorSpaceSerialization.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 84
Line coverage: 100%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
From(...)100%22100%
ToBehaviorEvent()70%1010100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Persistence/Serialization/BehaviorSpaceSerialization.cs

#LineLine coverage
 1using System.Text.Json;
 2using Intentum.Core.Behavior;
 3
 4namespace Intentum.Persistence.Serialization;
 5
 6/// <summary>
 7/// Shared JSON options for behavior space serialization across MongoDB and Redis.
 8/// </summary>
 9public static class BehaviorSpaceSerialization
 10{
 11    public static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase
 12}
 13
 14/// <summary>
 15/// Document shape for behavior space (MongoDB and Redis).
 16/// </summary>
 17public sealed class BehaviorSpaceDocument
 18{
 19    public string Id { get; set; } = "";
 20    public DateTimeOffset? CreatedAt { get; set; }
 21    public string MetadataJson { get; set; } = "{}";
 22    public List<BehaviorEventDocument> Events { get; set; } = [];
 23
 24    public static BehaviorSpaceDocument From(BehaviorSpace space, string id, DateTimeOffset? createdAt = null)
 25    {
 26        return new BehaviorSpaceDocument
 27        {
 28            Id = id,
 29            CreatedAt = createdAt,
 30            MetadataJson = JsonSerializer.Serialize(space.Metadata, BehaviorSpaceSerialization.JsonOptions),
 31            Events = space.Events.Select(BehaviorEventDocument.From).ToList()
 32        };
 33    }
 34
 35    public BehaviorSpace ToBehaviorSpace()
 36    {
 37        var space = new BehaviorSpace();
 38        var metadata = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(MetadataJson);
 39        if (metadata != null)
 40        {
 41            foreach (var kv in metadata)
 42                space.SetMetadata(kv.Key, kv.Value.ValueKind == JsonValueKind.Number ? kv.Value.GetDouble() : kv.Value.G
 43        }
 44        foreach (var evt in Events.OrderBy(e => e.OccurredAt))
 45            space.Observe(evt.ToBehaviorEvent());
 46        return space;
 47    }
 48}
 49
 50/// <summary>
 51/// Document shape for a single behavior event (MongoDB and Redis).
 52/// </summary>
 53public sealed class BehaviorEventDocument
 54{
 3655    public string Actor { get; set; } = "";
 3656    public string Action { get; set; } = "";
 3257    public DateTimeOffset OccurredAt { get; set; }
 3658    public string MetadataJson { get; set; } = "{}";
 59
 60    public static BehaviorEventDocument From(BehaviorEvent e)
 1261    {
 1262        return new BehaviorEventDocument
 1263        {
 1264            Actor = e.Actor,
 1265            Action = e.Action,
 1266            OccurredAt = e.OccurredAt,
 1267            MetadataJson = e.Metadata != null ? JsonSerializer.Serialize(e.Metadata, BehaviorSpaceSerialization.JsonOpti
 1268        };
 1269    }
 70
 71    public BehaviorEvent ToBehaviorEvent()
 1172    {
 1173        var metadata = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(MetadataJson);
 1174        object? meta = null;
 1175        if (metadata is { Count: > 0 })
 176        {
 177            var dict = new Dictionary<string, object>();
 578            foreach (var kv in metadata)
 179                dict[kv.Key] = kv.Value.ValueKind == JsonValueKind.Number ? kv.Value.GetDouble() : kv.Value.GetString() 
 180            meta = dict;
 181        }
 1182        return new BehaviorEvent(Actor, Action, OccurredAt, (IReadOnlyDictionary<string, object>?)meta);
 1183    }
 84}