Skip to content

[native_assets_cli] Use JSON enums where applicable #2129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 30 additions & 58 deletions pkgs/code_assets/doc/schema/shared/shared_definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@
"type": "string",
"anyOf": [
{
"const": "arm"
},
{
"const": "arm64"
},
{
"const": "ia32"
},
{
"const": "riscv32"
},
{
"const": "riscv64"
},
{
"const": "x64"
"enum": [
"arm",
"arm64",
"ia32",
"riscv32",
"riscv64",
"x64"
]
},
{
"type": "string"
Expand Down Expand Up @@ -288,19 +280,13 @@
"type": "string",
"anyOf": [
{
"const": "dynamic_loading_bundle"
},
{
"const": "dynamic_loading_executable"
},
{
"const": "dynamic_loading_process"
},
{
"const": "dynamic_loading_system"
},
{
"const": "static"
"enum": [
"dynamic_loading_bundle",
"dynamic_loading_executable",
"dynamic_loading_process",
"dynamic_loading_system",
"static"
]
},
{
"type": "string"
Expand Down Expand Up @@ -333,22 +319,14 @@
"type": "string",
"anyOf": [
{
"const": "dynamic"
},
{
"const": "prefer_dynamic"
},
{
"const": "prefer_static"
},
{
"const": "prefer-dynamic"
},
{
"const": "prefer-static"
},
{
"const": "static"
"enum": [
"dynamic",
"prefer_dynamic",
"prefer-dynamic",
"prefer_static",
"prefer-static",
"static"
]
},
{
"type": "string"
Expand All @@ -370,19 +348,13 @@
"type": "string",
"anyOf": [
{
"const": "android"
},
{
"const": "ios"
},
{
"const": "linux"
},
{
"const": "macos"
},
{
"const": "windows"
"enum": [
"android",
"ios",
"linux",
"macos",
"windows"
]
},
{
"type": "string"
Expand Down
22 changes: 16 additions & 6 deletions pkgs/json_syntax_generator/lib/src/parser/schema_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ class SchemaAnalyzer {
typeName ??= _ucFirst(_snakeToCamelCase(name!));
if (_classes[typeName] != null) return; // Already analyzed.

final anyOf = schemas.anyOfs.single;
final values =
anyOf.map((e) => e.constValue).whereType<String>().toList()..sort();
final values = schemas.enumOrTaggedUnionValues;
_classes[typeName] = EnumClassInfo(
name: typeName,
enumValues: [
Expand Down Expand Up @@ -243,9 +241,7 @@ class SchemaAnalyzer {

final propertyKey = schemas.propertyKeys.single;
final typeProperty = schemas.property(propertyKey);
final anyOf = typeProperty.anyOfs.single;
final subTypes =
anyOf.map((e) => e.constValue).whereType<String>().toList();
final subTypes = typeProperty.enumOrTaggedUnionValues;
for (final subType in subTypes) {
final ifThenSchemas = schemas.ifThenSchemas;
final subTypeNameShort = _ucFirst(_snakeToCamelCase(subType));
Expand Down Expand Up @@ -591,6 +587,12 @@ extension type JsonSchemas._(List<JsonSchema> _schemas) {
return result.singleOrNull;
}

List<String> get enum_ => [
for (final schema in _schemas)
for (final value in schema.enumValues ?? [])
if (value is String) value,
];

List<Uri> get refs {
final result = <Uri>[];
for (final schema in _schemas) {
Expand Down Expand Up @@ -695,6 +697,14 @@ extension on JsonSchemas {
}
throw StateError('No super class schema found for $parentClassName.');
}

List<String> get enumOrTaggedUnionValues => [
for (final schema in _schemas)
for (final s in schema.anyOf) ...[
if (s.constValue is String) s.constValue as String,
...s.enumValues?.whereType<String>() ?? [],
],
]..sort();
}

extension on String {
Expand Down