|
| 1 | +// Payload-type + render-handle codegen for prompt construction (FR-004 Plan #3, B). |
| 2 | +// |
| 3 | +// Emits the TYPED PAYLOAD as an idiomatic C# record (no runtime ValueObject; the |
| 4 | +// render engine consumes the record's properties, and the record type gives the |
| 5 | +// caller-side compile-time guarantee) plus a typed render handle. Property names |
| 6 | +// are kept as the exact metadata field names so the render engine resolves |
| 7 | +// `{{field}}` against the record. |
| 8 | +// |
| 9 | +// Ported from typescript/packages/codegen-ts/src/payload-codegen.ts. The |
| 10 | +// assembler (RDB materialization + host overlay) is out of scope — this only |
| 11 | +// emits the contract. |
| 12 | + |
| 13 | +using System.Text; |
| 14 | +using MetaObjects.Meta; |
| 15 | +using MetaObjects.Render; |
| 16 | +using static MetaObjects.Shared.BaseTypes; |
| 17 | +using static MetaObjects.Shared.Structural; |
| 18 | +using static MetaObjects.Core.Field.FieldConstants; |
| 19 | +using static MetaObjects.Template.TemplateConstants; |
| 20 | + |
| 21 | +namespace MetaObjects.Codegen; |
| 22 | + |
| 23 | +/// <summary>Emits typed payload records + render handles from view-object / template metadata.</summary> |
| 24 | +public static class PayloadCodegen |
| 25 | +{ |
| 26 | + // Field subtype -> idiomatic C# scalar type. Mirrors the TS SCALAR map |
| 27 | + // (number split into int/long/double; dates are ISO strings on the wire). |
| 28 | + private static readonly IReadOnlyDictionary<string, string> ScalarType = |
| 29 | + new Dictionary<string, string>(StringComparer.Ordinal) |
| 30 | + { |
| 31 | + [FIELD_SUBTYPE_STRING] = "string", |
| 32 | + [FIELD_SUBTYPE_CLASS] = "string", |
| 33 | + [FIELD_SUBTYPE_INT] = "int", |
| 34 | + [FIELD_SUBTYPE_SHORT] = "int", |
| 35 | + [FIELD_SUBTYPE_BYTE] = "int", |
| 36 | + [FIELD_SUBTYPE_LONG] = "long", |
| 37 | + [FIELD_SUBTYPE_CURRENCY] = "long", |
| 38 | + [FIELD_SUBTYPE_DOUBLE] = "double", |
| 39 | + [FIELD_SUBTYPE_FLOAT] = "double", |
| 40 | + [FIELD_SUBTYPE_DECIMAL] = "double", |
| 41 | + [FIELD_SUBTYPE_BOOLEAN] = "bool", |
| 42 | + [FIELD_SUBTYPE_DATE] = "string", |
| 43 | + [FIELD_SUBTYPE_TIME] = "string", |
| 44 | + [FIELD_SUBTYPE_TIMESTAMP] = "string", |
| 45 | + }; |
| 46 | + |
| 47 | + private static MetaData? FindObject(MetaData root, string name) => |
| 48 | + root.OwnChildren().FirstOrDefault(c => c.Type == TYPE_OBJECT && c.Name == name); |
| 49 | + |
| 50 | + private static bool IsArrayField(MetaData field) => |
| 51 | + field.OwnAttr(RESERVED_KEY_IS_ARRAY) is true || field.IsArray; |
| 52 | + |
| 53 | + private static (string Type, string? RefVo) FieldType(MetaData field) |
| 54 | + { |
| 55 | + if (field.SubType == FIELD_SUBTYPE_OBJECT) |
| 56 | + { |
| 57 | + var refAttr = field.OwnAttr(FIELD_ATTR_OBJECT_REF); |
| 58 | + string refName = refAttr is string s ? s : "object"; |
| 59 | + string? refVo = refAttr is string r ? r : null; |
| 60 | + return (IsArrayField(field) ? $"IReadOnlyList<{refName}>" : refName, refVo); |
| 61 | + } |
| 62 | + return (ScalarType.GetValueOrDefault(field.SubType, "object"), null); |
| 63 | + } |
| 64 | + |
| 65 | + private static void EmitRecord(MetaData root, string voName, HashSet<string> emitted, List<string> output) |
| 66 | + { |
| 67 | + if (!emitted.Add(voName)) return; |
| 68 | + var vo = FindObject(root, voName); |
| 69 | + if (vo is null) return; |
| 70 | + |
| 71 | + var lines = new List<string> { $"public sealed record {voName}", "{" }; |
| 72 | + var refs = new List<string>(); |
| 73 | + // Use Children() (effective) so inherited projection fields are included. |
| 74 | + foreach (var f in vo.Children().Where(c => c.Type == TYPE_FIELD)) |
| 75 | + { |
| 76 | + var (type, refVo) = FieldType(f); |
| 77 | + lines.Add($" public required {type} {f.Name} {{ get; init; }}"); |
| 78 | + if (refVo is not null) refs.Add(refVo); |
| 79 | + } |
| 80 | + lines.Add("}"); |
| 81 | + output.Add(string.Join("\n", lines)); |
| 82 | + |
| 83 | + foreach (var r in refs) EmitRecord(root, r, emitted, output); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Emit the payload record (+ nested element records) for an object.value view-object. |
| 88 | + /// </summary> |
| 89 | + public static string GeneratePayloadRecords(MetaData root, string voName) |
| 90 | + { |
| 91 | + var output = new List<string>(); |
| 92 | + EmitRecord(root, voName, new HashSet<string>(StringComparer.Ordinal), output); |
| 93 | + return string.Join("\n\n", output) + "\n"; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Derive the verify field tree (the input to <c>Verify.Check</c>) from an |
| 98 | + /// object.value view-object: scalars become leaves, object-ref fields recurse |
| 99 | + /// into nested element trees. This is the metadata→verify bridge a `meta verify` |
| 100 | + /// command uses to drift-check a template against its @payloadRef. |
| 101 | + /// </summary> |
| 102 | + public static IReadOnlyList<PayloadField> BuildPayloadFieldTree(MetaData root, string voName) => |
| 103 | + BuildTree(root, voName, new HashSet<string>(StringComparer.Ordinal)); |
| 104 | + |
| 105 | + private static IReadOnlyList<PayloadField> BuildTree(MetaData root, string voName, HashSet<string> visiting) |
| 106 | + { |
| 107 | + var vo = FindObject(root, voName); |
| 108 | + if (vo is null || !visiting.Add(voName)) return []; |
| 109 | + var fields = new List<PayloadField>(); |
| 110 | + foreach (var f in vo.Children().Where(c => c.Type == TYPE_FIELD)) |
| 111 | + { |
| 112 | + if (f.SubType == FIELD_SUBTYPE_OBJECT && f.OwnAttr(FIELD_ATTR_OBJECT_REF) is string refName) |
| 113 | + fields.Add(new PayloadField(f.Name, BuildTree(root, refName, visiting))); |
| 114 | + else |
| 115 | + fields.Add(new PayloadField(f.Name)); |
| 116 | + } |
| 117 | + visiting.Remove(voName); |
| 118 | + return fields; |
| 119 | + } |
| 120 | + |
| 121 | + private static string Pascal(string s) => |
| 122 | + s.Length > 0 ? char.ToUpperInvariant(s[0]) + s[1..] : s; |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Emit a typed render handle binding a template's @textRef + @format and typing |
| 126 | + /// its payload to the @payloadRef record. The generated code's only MetaObjects |
| 127 | + /// dependency is MetaObjects.Render (framework philosophy: generated code is |
| 128 | + /// idiomatic and runtime-light). |
| 129 | + /// </summary> |
| 130 | + public static string GenerateRenderHandle(MetaData root, string templateName) |
| 131 | + { |
| 132 | + var tmpl = root.OwnChildren() |
| 133 | + .FirstOrDefault(c => c.Type == TYPE_TEMPLATE && c.Name == templateName) |
| 134 | + ?? throw new ArgumentException($"template \"{templateName}\" not found", nameof(templateName)); |
| 135 | + |
| 136 | + var payloadRef = tmpl.OwnAttr(TEMPLATE_ATTR_PAYLOAD_REF) as string |
| 137 | + ?? throw new InvalidOperationException($"template \"{templateName}\" has no @payloadRef"); |
| 138 | + var textRef = tmpl.OwnAttr(TEMPLATE_ATTR_TEXT_REF) as string ?? ""; |
| 139 | + var format = tmpl.OwnAttr(TEMPLATE_ATTR_FORMAT) as string ?? "text"; |
| 140 | + var fn = $"Render{Pascal(templateName)}"; |
| 141 | + |
| 142 | + var sb = new StringBuilder(); |
| 143 | + sb.AppendLine("using MetaObjects.Render;"); |
| 144 | + sb.AppendLine(); |
| 145 | + sb.AppendLine("public static class RenderHandles"); |
| 146 | + sb.AppendLine("{"); |
| 147 | + sb.AppendLine($" public static string {fn}({payloadRef} payload, IProvider provider) =>"); |
| 148 | + sb.AppendLine(" Renderer.Render(new RenderRequest"); |
| 149 | + sb.AppendLine(" {"); |
| 150 | + sb.AppendLine($" Ref = {Quote(textRef)},"); |
| 151 | + sb.AppendLine(" Payload = payload,"); |
| 152 | + sb.AppendLine($" Format = {Quote(format)},"); |
| 153 | + sb.AppendLine(" Provider = provider,"); |
| 154 | + sb.AppendLine(" });"); |
| 155 | + sb.AppendLine("}"); |
| 156 | + return sb.ToString(); |
| 157 | + } |
| 158 | + |
| 159 | + private static string Quote(string s) => "\"" + s.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\""; |
| 160 | +} |
0 commit comments