Skip to content

Commit

Permalink
Fix unions schema to match JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmorgan committed Aug 8, 2024
1 parent 5b70e02 commit 284ba87
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 26 deletions.
20 changes: 15 additions & 5 deletions schemas/dart_model.schema.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"oneOf": [
{"$ref": "#/$defs/Model"}
{
"$ref": "#/$defs/Model"
}
],
"$defs": {
"Augmentation": {
Expand Down Expand Up @@ -31,12 +33,16 @@
"metadataAnnotations": {
"description": "The metadata annotations attached to this iterface.",
"type": "array",
"items": {"$ref": "#/$defs/MetadataAnnotation"}
"items": {
"$ref": "#/$defs/MetadataAnnotation"
}
},
"members": {
"type": "object",
"description": "Map of members by name.",
"additionalProperties": {"$ref": "#/$defs/Member"}
"additionalProperties": {
"$ref": "#/$defs/Member"
}
},
"properties": {
"$comment": "The properties of this interface.",
Expand All @@ -51,7 +57,9 @@
"scopes": {
"type": "object",
"description": "Scopes by name.",
"additionalProperties": {"$ref": "#/$defs/Interface"}
"additionalProperties": {
"$ref": "#/$defs/Interface"
}
}
}
},
Expand All @@ -72,7 +80,9 @@
"uris": {
"type": "object",
"description": "Libraries by URI.",
"additionalProperties": {"$ref": "#/$defs/Library"}
"additionalProperties": {
"$ref": "#/$defs/Library"
}
}
}
},
Expand Down
86 changes: 67 additions & 19 deletions schemas/macro_service.schema.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"oneOf": [
{"$ref": "#/$defs/AugmentRequest"},
{"$ref": "#/$defs/AugmentResponse"}
"anyOf": [
{
"$ref": "#/$defs/HostRequest"
},
{
"$ref": "#/$defs/MacroRequest"
},
{
"$ref": "#/$defs/Response"
}
],
"$defs": {
"AugmentRequest": {
Expand All @@ -22,7 +29,9 @@
"augmentations": {
"description": "The augmentations.",
"type": "array",
"items": {"$ref": "file:dart_model.schema.json#/$defs/Augmentation"}
"items": {
"$ref": "file:dart_model.schema.json#/$defs/Augmentation"
}
}
}
},
Expand All @@ -38,9 +47,18 @@
},
"HostRequest": {
"$description": "A request sent from host to macro.",
"oneOf": [
{"$ref": "#/$defs/AugmentRequest"}
]
"properties": {
"type": {
"type": "string"
},
"value": {
"oneOf": [
{
"$ref": "#/$defs/AugmentRequest"
}
]
}
}
},
"MacroDescription": {
"type": "object",
Expand All @@ -59,7 +77,9 @@
"type": "object",
"description": "Informs the host that a macro has started.",
"properties": {
"macroDescription": {"$ref": "#/$defs/MacroDescription"}
"macroDescription": {
"$ref": "#/$defs/MacroDescription"
}
}
},
"MacroStartedResponse": {
Expand All @@ -68,32 +88,60 @@
},
"MacroRequest": {
"$description": "A request sent from macro to host.",
"oneOf": [
{"$ref": "#/$defs/MacroStartedRequest"},
{"$ref": "#/$defs/QueryRequest"}
]
"properties": {
"type": {
"type": "string"
},
"value": {
"oneOf": [
{
"$ref": "#/$defs/MacroStartedRequest"
},
{
"$ref": "#/$defs/QueryRequest"
}
]
}
}
},
"QueryRequest": {
"type": "object",
"description": "Macro's query about the code it should augment.",
"properties": {
"query": {"$ref": "file:dart_model.schema.json#/$defs/Query"}
"query": {
"$ref": "file:dart_model.schema.json#/$defs/Query"
}
}
},
"QueryResponse": {
"type": "object",
"description": "Host's response to a [QueryRequest].",
"properties": {
"model": {"$ref": "file:dart_model.schema.json#/$defs/Model"}
"model": {
"$ref": "file:dart_model.schema.json#/$defs/Model"
}
}
},
"Response": {
"$description": "A response to a [MacroRequest] or [HostRequest].",
"oneOf": [
{"$ref": "#/$defs/AugmentResponse"},
{"$ref": "#/$defs/MacroStartedResponse"},
{"$ref": "#/$defs/QueryResponse"}
]
"properties": {
"type": {
"type": "string"
},
"value": {
"oneOf": [
{
"$ref": "#/$defs/AugmentResponse"
},
{
"$ref": "#/$defs/MacroStartedResponse"
},
{
"$ref": "#/$defs/QueryResponse"
}
]
}
}
}
}
}
12 changes: 10 additions & 2 deletions tool/dart_model_generator/lib/generate_dart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ String generate(String schemaJson,
refProvider: LocalRefProvider(dartModelJson ??
File('schemas/dart_model.schema.json').readAsStringSync()));
for (final def in schema.defs.entries) {
if (def.value.oneOf.isNotEmpty) {
result.add(_generateUnion(def.key, def.value.oneOf));
if (_isUnion(def.value)) {
result.add(_generateUnion(def.key, def.value.properties['value']!.oneOf));
} else {
result.add(_generateExtensionType(def.key, def.value));
}
Expand Down Expand Up @@ -132,6 +132,14 @@ String _generateExtensionType(String name, JsonSchema definition) {
return result.toString();
}

/// Whether [schema] represents a union type.
///
/// To be a union type it must have exactly two properties, "type" and "value",
/// where "type" is a "string" and "value" is a "oneOf".
bool _isUnion(JsonSchema schema) =>
schema.properties['type']?.schemaMap!['type'] == 'string' &&
schema.properties['value']?.oneOf != null;

/// Generates a type called [name] that is a union of the specified [oneOf]
/// types, which must all be `$ref`s to class definitions.
///
Expand Down

0 comments on commit 284ba87

Please sign in to comment.