| | | 1 | | using System.Text; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.AgentFramework.Generators.Models; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.AgentFramework.Generators.CodeGen |
| | | 6 | | { |
| | | 7 | | internal static class AsyncLocalScopedCodeGenerator |
| | | 8 | | { |
| | | 9 | | public static string Generate(AsyncLocalScopedInfo info) |
| | | 10 | | { |
| | 8 | 11 | | var sb = new StringBuilder(); |
| | | 12 | | |
| | 8 | 13 | | sb.AppendLine("// <auto-generated/>"); |
| | 8 | 14 | | sb.AppendLine("// AsyncLocal-backed implementation for " + info.InterfaceName); |
| | 8 | 15 | | sb.AppendLine("#nullable enable"); |
| | 8 | 16 | | sb.AppendLine(); |
| | | 17 | | |
| | 8 | 18 | | if (!string.IsNullOrEmpty(info.NamespaceName)) |
| | | 19 | | { |
| | 8 | 20 | | sb.AppendLine("namespace " + info.NamespaceName); |
| | 8 | 21 | | sb.AppendLine("{"); |
| | | 22 | | } |
| | | 23 | | |
| | 8 | 24 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Gen |
| | 8 | 25 | | sb.AppendLine("internal sealed class " + info.GeneratedClassName + " : " + info.InterfaceFullName); |
| | 8 | 26 | | sb.AppendLine("{"); |
| | | 27 | | |
| | 8 | 28 | | if (info.IsMutable) |
| | | 29 | | { |
| | 5 | 30 | | GenerateMutablePattern(sb, info); |
| | | 31 | | } |
| | | 32 | | else |
| | | 33 | | { |
| | 3 | 34 | | GenerateSimplePattern(sb, info); |
| | | 35 | | } |
| | | 36 | | |
| | 8 | 37 | | sb.AppendLine("}"); |
| | | 38 | | |
| | 8 | 39 | | if (!string.IsNullOrEmpty(info.NamespaceName)) |
| | | 40 | | { |
| | 8 | 41 | | sb.AppendLine("}"); |
| | | 42 | | } |
| | | 43 | | |
| | 8 | 44 | | return sb.ToString(); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private static void GenerateSimplePattern(StringBuilder sb, AsyncLocalScopedInfo info) |
| | | 48 | | { |
| | 3 | 49 | | var valueType = info.ValueTypeFullName; |
| | | 50 | | |
| | 3 | 51 | | sb.AppendLine(" private static readonly global::System.Threading.AsyncLocal<" + valueType + "?> _current |
| | 3 | 52 | | sb.AppendLine(); |
| | 3 | 53 | | sb.AppendLine(" public " + valueType + "? Current => _current.Value;"); |
| | 3 | 54 | | sb.AppendLine(); |
| | | 55 | | |
| | 3 | 56 | | GeneratePropertyProxies(sb, info, "_current.Value"); |
| | | 57 | | |
| | 3 | 58 | | if (info.HasScopeParameter) |
| | | 59 | | { |
| | 3 | 60 | | sb.AppendLine(" public global::System.IDisposable " + info.ScopeMethodName + "(" + info.ScopeParamete |
| | 3 | 61 | | sb.AppendLine(" {"); |
| | 3 | 62 | | sb.AppendLine(" if (value == null) throw new global::System.ArgumentNullException(nameof(value)); |
| | 3 | 63 | | sb.AppendLine(); |
| | 3 | 64 | | sb.AppendLine(" var previous = _current.Value;"); |
| | 3 | 65 | | sb.AppendLine(" _current.Value = value;"); |
| | 3 | 66 | | sb.AppendLine(" return new Scope(previous);"); |
| | 3 | 67 | | sb.AppendLine(" }"); |
| | | 68 | | } |
| | | 69 | | else |
| | | 70 | | { |
| | 0 | 71 | | sb.AppendLine(" public global::System.IDisposable " + info.ScopeMethodName + "()"); |
| | 0 | 72 | | sb.AppendLine(" {"); |
| | 0 | 73 | | sb.AppendLine(" var previous = _current.Value;"); |
| | 0 | 74 | | sb.AppendLine(" _current.Value = default;"); |
| | 0 | 75 | | sb.AppendLine(" return new Scope(previous);"); |
| | 0 | 76 | | sb.AppendLine(" }"); |
| | | 77 | | } |
| | | 78 | | |
| | 3 | 79 | | sb.AppendLine(); |
| | 3 | 80 | | sb.AppendLine(" private sealed class Scope : global::System.IDisposable"); |
| | 3 | 81 | | sb.AppendLine(" {"); |
| | 3 | 82 | | sb.AppendLine(" private readonly " + valueType + "? _previous;"); |
| | 3 | 83 | | sb.AppendLine(" private bool _disposed;"); |
| | 3 | 84 | | sb.AppendLine(); |
| | 3 | 85 | | sb.AppendLine(" public Scope(" + valueType + "? previous) => _previous = previous;"); |
| | 3 | 86 | | sb.AppendLine(); |
| | 3 | 87 | | sb.AppendLine(" public void Dispose()"); |
| | 3 | 88 | | sb.AppendLine(" {"); |
| | 3 | 89 | | sb.AppendLine(" if (_disposed) return;"); |
| | 3 | 90 | | sb.AppendLine(" _disposed = true;"); |
| | 3 | 91 | | sb.AppendLine(" _current.Value = _previous;"); |
| | 3 | 92 | | sb.AppendLine(" }"); |
| | 3 | 93 | | sb.AppendLine(" }"); |
| | 3 | 94 | | } |
| | | 95 | | |
| | | 96 | | private static void GenerateMutablePattern(StringBuilder sb, AsyncLocalScopedInfo info) |
| | | 97 | | { |
| | 5 | 98 | | var valueType = info.ValueTypeFullName; |
| | | 99 | | |
| | 5 | 100 | | sb.AppendLine(" private static readonly global::System.Threading.AsyncLocal<Holder?> _current = new globa |
| | 5 | 101 | | sb.AppendLine(); |
| | 5 | 102 | | sb.AppendLine(" public " + valueType + "? Current => _current.Value?.Value;"); |
| | 5 | 103 | | sb.AppendLine(); |
| | | 104 | | |
| | 5 | 105 | | GeneratePropertyProxies(sb, info, "_current.Value?.Value"); |
| | | 106 | | |
| | 5 | 107 | | if (info.HasScopeParameter) |
| | | 108 | | { |
| | 3 | 109 | | sb.AppendLine(" public global::System.IDisposable " + info.ScopeMethodName + "(" + info.ScopeParamete |
| | 3 | 110 | | sb.AppendLine(" {"); |
| | 3 | 111 | | sb.AppendLine(" var previous = _current.Value;"); |
| | 3 | 112 | | sb.AppendLine(" _current.Value = new Holder() { Value = value };"); |
| | 3 | 113 | | sb.AppendLine(" return new Scope(previous);"); |
| | 3 | 114 | | sb.AppendLine(" }"); |
| | | 115 | | } |
| | | 116 | | else |
| | | 117 | | { |
| | 2 | 118 | | sb.AppendLine(" public global::System.IDisposable " + info.ScopeMethodName + "()"); |
| | 2 | 119 | | sb.AppendLine(" {"); |
| | 2 | 120 | | sb.AppendLine(" var previous = _current.Value;"); |
| | 2 | 121 | | sb.AppendLine(" _current.Value = new Holder();"); |
| | 2 | 122 | | sb.AppendLine(" return new Scope(previous);"); |
| | 2 | 123 | | sb.AppendLine(" }"); |
| | | 124 | | } |
| | | 125 | | |
| | 5 | 126 | | sb.AppendLine(); |
| | 5 | 127 | | sb.AppendLine(" internal void Set(" + valueType + " value)"); |
| | 5 | 128 | | sb.AppendLine(" {"); |
| | 5 | 129 | | sb.AppendLine(" var holder = _current.Value;"); |
| | 5 | 130 | | sb.AppendLine(" if (holder != null)"); |
| | 5 | 131 | | sb.AppendLine(" {"); |
| | 5 | 132 | | sb.AppendLine(" holder.Value = value;"); |
| | 5 | 133 | | sb.AppendLine(" }"); |
| | 5 | 134 | | sb.AppendLine(" else"); |
| | 5 | 135 | | sb.AppendLine(" {"); |
| | 5 | 136 | | sb.AppendLine(" _current.Value = new Holder() { Value = value };"); |
| | 5 | 137 | | sb.AppendLine(" }"); |
| | 5 | 138 | | sb.AppendLine(" }"); |
| | 5 | 139 | | sb.AppendLine(); |
| | | 140 | | |
| | 5 | 141 | | sb.AppendLine(" private sealed class Holder"); |
| | 5 | 142 | | sb.AppendLine(" {"); |
| | 5 | 143 | | sb.AppendLine(" public " + valueType + "? Value;"); |
| | 5 | 144 | | sb.AppendLine(" }"); |
| | 5 | 145 | | sb.AppendLine(); |
| | | 146 | | |
| | 5 | 147 | | sb.AppendLine(" private sealed class Scope : global::System.IDisposable"); |
| | 5 | 148 | | sb.AppendLine(" {"); |
| | 5 | 149 | | sb.AppendLine(" private readonly Holder? _previous;"); |
| | 5 | 150 | | sb.AppendLine(" private bool _disposed;"); |
| | 5 | 151 | | sb.AppendLine(); |
| | 5 | 152 | | sb.AppendLine(" public Scope(Holder? previous) => _previous = previous;"); |
| | 5 | 153 | | sb.AppendLine(); |
| | 5 | 154 | | sb.AppendLine(" public void Dispose()"); |
| | 5 | 155 | | sb.AppendLine(" {"); |
| | 5 | 156 | | sb.AppendLine(" if (_disposed) return;"); |
| | 5 | 157 | | sb.AppendLine(" _disposed = true;"); |
| | 5 | 158 | | sb.AppendLine(" _current.Value = _previous;"); |
| | 5 | 159 | | sb.AppendLine(" }"); |
| | 5 | 160 | | sb.AppendLine(" }"); |
| | 5 | 161 | | } |
| | | 162 | | |
| | | 163 | | private static void GeneratePropertyProxies( |
| | | 164 | | StringBuilder sb, |
| | | 165 | | AsyncLocalScopedInfo info, |
| | | 166 | | string currentExpression) |
| | | 167 | | { |
| | 8 | 168 | | if (info.ProxyProperties.Length == 0) |
| | 4 | 169 | | return; |
| | | 170 | | |
| | 24 | 171 | | foreach (var prop in info.ProxyProperties) |
| | | 172 | | { |
| | 8 | 173 | | var getExpr = currentExpression + "?." + prop.Name; |
| | 8 | 174 | | if (prop.IsNonNullableValueType) |
| | | 175 | | { |
| | 4 | 176 | | getExpr += " ?? default"; |
| | | 177 | | } |
| | | 178 | | |
| | 8 | 179 | | if (prop.HasSetter) |
| | | 180 | | { |
| | 5 | 181 | | sb.AppendLine(" public " + prop.TypeFullName + " " + prop.Name); |
| | 5 | 182 | | sb.AppendLine(" {"); |
| | 5 | 183 | | sb.AppendLine(" get => " + getExpr + ";"); |
| | 5 | 184 | | sb.AppendLine(" set { if (" + currentExpression + " is { } h) h." + prop.Name + " = value; }" |
| | 5 | 185 | | sb.AppendLine(" }"); |
| | | 186 | | } |
| | | 187 | | else |
| | | 188 | | { |
| | 3 | 189 | | sb.AppendLine(" public " + prop.TypeFullName + " " + prop.Name + " => " + getExpr + ";"); |
| | | 190 | | } |
| | | 191 | | |
| | 8 | 192 | | sb.AppendLine(); |
| | | 193 | | } |
| | 4 | 194 | | } |
| | | 195 | | } |
| | | 196 | | } |