|
3 | 3 | import java.util.List; |
4 | 4 | import java.util.Map; |
5 | 5 |
|
6 | | -/// Minimal AST for the experimental "flat JTD → ESM validator" code generator. |
| 6 | +/// Complete AST for RFC 8927 JTD code generation. |
| 7 | +/// Supports all schema forms: empty, type, enum, elements, properties, values, |
| 8 | +/// discriminator, ref, and nullable. |
7 | 9 | /// |
8 | | -/// Supported JTD subset: |
9 | | -/// - root: `properties`, `optionalProperties`, `metadata.id` |
10 | | -/// - leaf: `{}` (empty form), `{ "type": ... }`, `{ "enum": [...] }` |
| 10 | +/// This AST is designed for stack-based code generation where each node |
| 11 | +/// knows how to generate its own validation logic. |
11 | 12 | public final class JtdAst { |
12 | 13 | private JtdAst() {} |
13 | 14 |
|
14 | | - public sealed interface JtdNode permits SchemaNode, PropertyNode, TypeNode, EnumNode, EmptyNode {} |
| 15 | + public sealed interface JtdNode permits |
| 16 | + EmptyNode, TypeNode, EnumNode, ElementsNode, PropertiesNode, |
| 17 | + ValuesNode, DiscriminatorNode, RefNode, NullableNode {} |
15 | 18 |
|
16 | | - public record SchemaNode( |
17 | | - String id, // metadata.id |
18 | | - Map<String, PropertyNode> properties, |
19 | | - Map<String, PropertyNode> optionalProperties |
20 | | - ) implements JtdNode {} |
| 19 | + /// Root of a JTD document with metadata, definitions, and root schema. |
| 20 | + public record RootNode( |
| 21 | + String id, |
| 22 | + Map<String, JtdNode> definitions, |
| 23 | + JtdNode rootSchema |
| 24 | + ) {} |
21 | 25 |
|
22 | | - public record PropertyNode(String name, JtdNode type) implements JtdNode {} |
| 26 | + /// Empty form {} - accepts any JSON value. |
| 27 | + public record EmptyNode() implements JtdNode {} |
23 | 28 |
|
24 | | - /// JTD primitive type keyword as a string, e.g. "string", "int32", "timestamp". |
| 29 | + /// Type form - validates primitive types. |
| 30 | + /// Type values: string, boolean, timestamp, int8, uint8, int16, uint16, |
| 31 | + /// int32, uint32, float32, float64 |
25 | 32 | public record TypeNode(String type) implements JtdNode {} |
26 | 33 |
|
27 | | - /// Enum values (strings only in RFC 8927). |
| 34 | + /// Enum form - validates string is one of allowed values. |
28 | 35 | public record EnumNode(List<String> values) implements JtdNode {} |
29 | 36 |
|
30 | | - /// Empty form `{}`: accepts any JSON value. |
31 | | - public record EmptyNode() implements JtdNode {} |
32 | | -} |
| 37 | + /// Elements form - validates array where each element matches schema. |
| 38 | + public record ElementsNode(JtdNode schema) implements JtdNode {} |
| 39 | + |
| 40 | + /// Properties form - validates object with required/optional properties. |
| 41 | + public record PropertiesNode( |
| 42 | + Map<String, JtdNode> properties, |
| 43 | + Map<String, JtdNode> optionalProperties, |
| 44 | + boolean additionalProperties |
| 45 | + ) implements JtdNode {} |
33 | 46 |
|
| 47 | + /// Values form - validates object where all values match schema. |
| 48 | + public record ValuesNode(JtdNode schema) implements JtdNode {} |
| 49 | + |
| 50 | + /// Discriminator form - validates tagged unions. |
| 51 | + public record DiscriminatorNode( |
| 52 | + String discriminator, |
| 53 | + Map<String, JtdNode> mapping |
| 54 | + ) implements JtdNode {} |
| 55 | + |
| 56 | + /// Ref form - references a definition. |
| 57 | + public record RefNode(String ref) implements JtdNode {} |
| 58 | + |
| 59 | + /// Nullable wrapper - allows null in addition to wrapped schema. |
| 60 | + public record NullableNode(JtdNode wrapped) implements JtdNode {} |
| 61 | +} |
0 commit comments