GPAL - Generally Positive Automation Library v1.0
GPAL The Fluent Automation LIbrary
Loading...
Searching...
No Matches
HumanReadableAliasEventEmitter.cs
1// =============================================================================
2// GPAL - Generally Positive Automation Library
3// Copyright © 2026 Software Decisions, Inc. All rights reserved.
4//
5// This file is part of GPAL.
6// Licensed under the Business Source License 1.1
7//
8// Primary development, architecture, and vision by Michael B. Vederman,
9// CEO of Software Decisions, Inc., Texas.
10//
11// Internal development maintained privately.
12// Public releases appear on GitHub: https://github.com/SoftwareDecisionsInc/GPAL.
13//
14// See LICENSE for full terms, including Additional Use Grant.
15// =============================================================================
16
17using System.Collections.Generic;
18using YamlDotNet.Core;
19using YamlDotNet.Core.Events;
20using YamlDotNet.Serialization;
21using YamlDotNet.Serialization.EventEmitters;
22
23public class HumanReadableAliasEventEmitter : ChainedEventEmitter
24{
25 private readonly Dictionary<string, string> _anchorRenames = new Dictionary<string, string>();
26 private readonly Dictionary<string, int> _typeCounters = new Dictionary<string, int>();
27
28 public HumanReadableAliasEventEmitter(IEventEmitter nextEmitter) : base(nextEmitter) { }
29
30 public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
31 {
32 if (!eventInfo.Anchor.IsEmpty)
33 eventInfo.Anchor = new AnchorName(Rename(eventInfo.Anchor.Value, eventInfo.Source.Type?.Name));
34 base.Emit(eventInfo, emitter);
35 }
36
37 public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter)
38 {
39 if (!eventInfo.Anchor.IsEmpty)
40 eventInfo.Anchor = new AnchorName(Rename(eventInfo.Anchor.Value, eventInfo.Source.Type?.Name));
41 base.Emit(eventInfo, emitter);
42 }
43
44 public override void Emit(AliasEventInfo eventInfo, IEmitter emitter)
45 {
46 if (_anchorRenames.TryGetValue(eventInfo.Alias.Value, out var readable))
47 emitter.Emit(new AnchorAlias(new AnchorName(readable)));
48 else
49 base.Emit(eventInfo, emitter);
50 }
51
52 private string Rename(string original, string typeName)
53 {
54 var clean = CleanTypeName(typeName);
55 _typeCounters.TryGetValue(clean, out int count);
56 _typeCounters[clean] = count + 1;
57 var readable = count == 0 ? clean : $"{clean}_{count + 1}";
58 _anchorRenames[original] = readable;
59 return readable;
60 }
61
62 private static string CleanTypeName(string name)
63 {
64 if (name == null) return "object";
65 var backtick = name.IndexOf('`');
66 return backtick >= 0 ? name.Substring(0, backtick) : name;
67 }
68}