< Summary

Information
Class: Intentum.Persistence.Serialization.BehaviorSpaceDocument
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
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
From(...)100%11100%
ToBehaviorSpace()90%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{
 2219    public string Id { get; set; } = "";
 1020    public DateTimeOffset? CreatedAt { get; set; }
 2821    public string MetadataJson { get; set; } = "{}";
 2722    public List<BehaviorEventDocument> Events { get; set; } = [];
 23
 24    public static BehaviorSpaceDocument From(BehaviorSpace space, string id, DateTimeOffset? createdAt = null)
 825    {
 826        return new BehaviorSpaceDocument
 827        {
 828            Id = id,
 829            CreatedAt = createdAt,
 830            MetadataJson = JsonSerializer.Serialize(space.Metadata, BehaviorSpaceSerialization.JsonOptions),
 831            Events = space.Events.Select(BehaviorEventDocument.From).ToList()
 832        };
 833    }
 34
 35    public BehaviorSpace ToBehaviorSpace()
 736    {
 737        var space = new BehaviorSpace();
 738        var metadata = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(MetadataJson);
 639        if (metadata != null)
 640        {
 2641            foreach (var kv in metadata)
 442                space.SetMetadata(kv.Key, kv.Value.ValueKind == JsonValueKind.Number ? kv.Value.GetDouble() : kv.Value.G
 643        }
 4244        foreach (var evt in Events.OrderBy(e => e.OccurredAt))
 845            space.Observe(evt.ToBehaviorEvent());
 646        return space;
 647    }
 48}
 49
 50/// <summary>
 51/// Document shape for a single behavior event (MongoDB and Redis).
 52/// </summary>
 53public sealed class BehaviorEventDocument
 54{
 55    public string Actor { get; set; } = "";
 56    public string Action { get; set; } = "";
 57    public DateTimeOffset OccurredAt { get; set; }
 58    public string MetadataJson { get; set; } = "{}";
 59
 60    public static BehaviorEventDocument From(BehaviorEvent e)
 61    {
 62        return new BehaviorEventDocument
 63        {
 64            Actor = e.Actor,
 65            Action = e.Action,
 66            OccurredAt = e.OccurredAt,
 67            MetadataJson = e.Metadata != null ? JsonSerializer.Serialize(e.Metadata, BehaviorSpaceSerialization.JsonOpti
 68        };
 69    }
 70
 71    public BehaviorEvent ToBehaviorEvent()
 72    {
 73        var metadata = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(MetadataJson);
 74        object? meta = null;
 75        if (metadata is { Count: > 0 })
 76        {
 77            var dict = new Dictionary<string, object>();
 78            foreach (var kv in metadata)
 79                dict[kv.Key] = kv.Value.ValueKind == JsonValueKind.Number ? kv.Value.GetDouble() : kv.Value.GetString() 
 80            meta = dict;
 81        }
 82        return new BehaviorEvent(Actor, Action, OccurredAt, (IReadOnlyDictionary<string, object>?)meta);
 83    }
 84}