diff --git a/pkgs/json_syntax_generator/lib/src/generator/helper_library.dart b/pkgs/json_syntax_generator/lib/src/generator/helper_library.dart new file mode 100644 index 0000000000..8703d13764 --- /dev/null +++ b/pkgs/json_syntax_generator/lib/src/generator/helper_library.dart @@ -0,0 +1,112 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// Helper methods for the code generator that are added to the generated file. +/// +/// This simplifies the code generator. +const helperLib = r''' +extension on Map { + T get(String key) { + final value = this[key]; + if (value is T) return value; + if (value == null) { + throw FormatException('No value was provided for required key: $key'); + } + throw FormatException( + 'Unexpected value \'$value\' for key \'.$key\'. ' + 'Expected a $T.', + ); + } + + List list(String key) => + _castList(get>(key), key); + + List? optionalList(String key) => + switch (get?>(key)?.cast()) { + null => null, + final l => _castList(l, key), + }; + + /// [List.cast] but with [FormatException]s. + static List _castList(List list, String key) { + for (final value in list) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$list\' (${list.runtimeType}) for key \'.$key\'. ' + 'Expected a ${List}.', + ); + } + } + return list.cast(); + } + + Map map$(String key) => + _castMap(get>(key), key); + + Map? optionalMap(String key) => + switch (get?>(key)) { + null => null, + final m => _castMap(m, key), + }; + + /// [Map.cast] but with [FormatException]s. + static Map _castMap( + Map map_, + String key, + ) { + for (final value in map_.values) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$map_\' (${map_.runtimeType}) for key \'.$key\'.' + 'Expected a ${Map}.', + ); + } + } + return map_.cast(); + } + + List? optionalStringList(String key) => optionalList(key); + + List stringList(String key) => list(key); + + Uri path(String key) => _fileSystemPathToUri(get(key)); + + Uri? optionalPath(String key) { + final value = get(key); + if (value == null) return null; + return _fileSystemPathToUri(value); + } + + List? optionalPathList(String key) { + final strings = optionalStringList(key); + if (strings == null) { + return null; + } + return [for (final string in strings) _fileSystemPathToUri(string)]; + } + + static Uri _fileSystemPathToUri(String path) { + if (path.endsWith(Platform.pathSeparator)) { + return Uri.directory(path); + } + return Uri.file(path); + } +} + +extension on List { + List toJson() => [for (final uri in this) uri.toFilePath()]; +} + +extension , V extends Object?> on Map { + void sortOnKey() { + final result = {}; + final keysSorted = keys.toList()..sort(); + for (final key in keysSorted) { + result[key] = this[key] as V; + } + clear(); + addAll(result); + } +} +'''; diff --git a/pkgs/json_syntax_generator/lib/src/generator/syntax_generator.dart b/pkgs/json_syntax_generator/lib/src/generator/syntax_generator.dart index 1ee2e774cd..2873f6e4f4 100644 --- a/pkgs/json_syntax_generator/lib/src/generator/syntax_generator.dart +++ b/pkgs/json_syntax_generator/lib/src/generator/syntax_generator.dart @@ -6,6 +6,7 @@ import '../model/class_info.dart'; import '../model/dart_type.dart'; import '../model/property_info.dart'; import '../model/schema_info.dart'; +import 'helper_library.dart'; /// Generates Dart code from a [SchemaInfo]. /// @@ -35,13 +36,17 @@ class SyntaxGenerator { // This file is generated, do not edit. -import '../utils/json.dart'; +// ignore_for_file: unused_element + +import 'dart:io'; '''); for (final classInfo in schemaInfo.classes) { buffer.writeln(_generateClass(classInfo)); } + buffer.writeln(helperLib); + return buffer.toString(); } @@ -301,7 +306,7 @@ class $className { case EnumClassInfo(): if (required) { buffer.writeln(''' -$dartType get $fieldName => $classType.fromJson( json.string('$jsonKey') ); +$dartType get $fieldName => $classType.fromJson( json.get('$jsonKey') ); '''); if (!property.isOverride) { buffer.writeln(''' @@ -313,7 +318,7 @@ set $setterName($dartType value) { } else { buffer.writeln(''' $dartType get $fieldName { - final string = json.optionalString('$jsonKey'); + final string = json.get('$jsonKey'); if(string == null) return null; return $classType.fromJson(string); } @@ -384,7 +389,7 @@ set $setterName($dartType value) { case 'String': if (required) { buffer.writeln(''' -String get $fieldName => json.string('$jsonKey'); +String get $fieldName => json.get('$jsonKey'); set $setterName(String value) { json['$jsonKey'] = value; @@ -393,7 +398,7 @@ set $setterName(String value) { '''); } else { buffer.writeln(''' -String? get $fieldName => json.optionalString('$jsonKey'); +String? get $fieldName => json.get('$jsonKey'); set $setterName(String? value) { if (value == null) { @@ -415,7 +420,7 @@ set $setterName(int value) => json['$jsonKey'] = value; '''); } else { buffer.writeln(''' -int? get $fieldName => json.getOptional('$jsonKey'); +int? get $fieldName => json.get('$jsonKey'); set $setterName(int? value) { if (value == null) { @@ -440,7 +445,7 @@ set $setterName(bool value) { '''); } else { buffer.writeln(''' -bool? get $fieldName => json.getOptional('$jsonKey'); +bool? get $fieldName => json.get('$jsonKey'); set $setterName(bool? value) { if (value == null) { diff --git a/pkgs/native_assets_cli/lib/src/code_assets/syntax.g.dart b/pkgs/native_assets_cli/lib/src/code_assets/syntax.g.dart index e515eac434..bcdcd325d7 100644 --- a/pkgs/native_assets_cli/lib/src/code_assets/syntax.g.dart +++ b/pkgs/native_assets_cli/lib/src/code_assets/syntax.g.dart @@ -4,7 +4,9 @@ // This file is generated, do not edit. -import '../utils/json.dart'; +// ignore_for_file: unused_element + +import 'dart:io'; class AndroidCodeConfig { final Map json; @@ -16,7 +18,7 @@ class AndroidCodeConfig { json.sortOnKey(); } - int? get targetNdkApi => json.getOptional('target_ndk_api'); + int? get targetNdkApi => json.get('target_ndk_api'); set _targetNdkApi(int? value) { if (value == null) { @@ -86,7 +88,7 @@ class Asset { json.sortOnKey(); } - String? get type => json.optionalString('type'); + String? get type => json.get('type'); set _type(String? value) { if (value == null) { @@ -136,7 +138,7 @@ class NativeCodeAsset extends Asset { } Architecture? get architecture { - final string = json.optionalString('architecture'); + final string = json.get('architecture'); if (string == null) return null; return Architecture.fromJson(string); } @@ -159,7 +161,7 @@ class NativeCodeAsset extends Asset { } } - String get id => json.string('id'); + String get id => json.get('id'); set _id(String value) { json['id'] = value; @@ -169,7 +171,7 @@ class NativeCodeAsset extends Asset { set _linkMode(LinkMode value) => json['link_mode'] = value.json; - OS get os => OS.fromJson(json.string('os')); + OS get os => OS.fromJson(json.get('os')); set _os(OS value) { json['os'] = value.name; @@ -397,7 +399,7 @@ class CodeConfig { } LinkModePreference get linkModePreference => - LinkModePreference.fromJson(json.string('link_mode_preference')); + LinkModePreference.fromJson(json.get('link_mode_preference')); set _linkModePreference(LinkModePreference value) { json['link_mode_preference'] = value.name; @@ -420,13 +422,13 @@ class CodeConfig { } Architecture get targetArchitecture => - Architecture.fromJson(json.string('target_architecture')); + Architecture.fromJson(json.get('target_architecture')); set _targetArchitecture(Architecture value) { json['target_architecture'] = value.name; } - OS get targetOs => OS.fromJson(json.string('target_os')); + OS get targetOs => OS.fromJson(json.get('target_os')); set _targetOs(OS value) { json['target_os'] = value.name; @@ -478,7 +480,7 @@ class IOSCodeConfig { json.sortOnKey(); } - String? get targetSdk => json.optionalString('target_sdk'); + String? get targetSdk => json.get('target_sdk'); set _targetSdk(String? value) { if (value == null) { @@ -488,7 +490,7 @@ class IOSCodeConfig { } } - int? get targetVersion => json.getOptional('target_version'); + int? get targetVersion => json.get('target_version'); set _targetVersion(int? value) { if (value == null) { @@ -512,7 +514,7 @@ class LinkMode { json.sortOnKey(); } - String get type => json.string('type'); + String get type => json.get('type'); set _type(String value) { json['type'] = value; @@ -670,7 +672,7 @@ class MacOSCodeConfig { json.sortOnKey(); } - int? get targetVersion => json.getOptional('target_version'); + int? get targetVersion => json.get('target_version'); set _targetVersion(int? value) { if (value == null) { @@ -720,3 +722,107 @@ class OS { @override String toString() => name; } + +extension on Map { + T get(String key) { + final value = this[key]; + if (value is T) return value; + if (value == null) { + throw FormatException('No value was provided for required key: $key'); + } + throw FormatException( + 'Unexpected value \'$value\' for key \'.$key\'. ' + 'Expected a $T.', + ); + } + + List list(String key) => + _castList(get>(key), key); + + List? optionalList(String key) => + switch (get?>(key)?.cast()) { + null => null, + final l => _castList(l, key), + }; + + /// [List.cast] but with [FormatException]s. + static List _castList(List list, String key) { + for (final value in list) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$list\' (${list.runtimeType}) for key \'.$key\'. ' + 'Expected a ${List}.', + ); + } + } + return list.cast(); + } + + Map map$(String key) => + _castMap(get>(key), key); + + Map? optionalMap(String key) => + switch (get?>(key)) { + null => null, + final m => _castMap(m, key), + }; + + /// [Map.cast] but with [FormatException]s. + static Map _castMap( + Map map_, + String key, + ) { + for (final value in map_.values) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$map_\' (${map_.runtimeType}) for key \'.$key\'.' + 'Expected a ${Map}.', + ); + } + } + return map_.cast(); + } + + List? optionalStringList(String key) => optionalList(key); + + List stringList(String key) => list(key); + + Uri path(String key) => _fileSystemPathToUri(get(key)); + + Uri? optionalPath(String key) { + final value = get(key); + if (value == null) return null; + return _fileSystemPathToUri(value); + } + + List? optionalPathList(String key) { + final strings = optionalStringList(key); + if (strings == null) { + return null; + } + return [for (final string in strings) _fileSystemPathToUri(string)]; + } + + static Uri _fileSystemPathToUri(String path) { + if (path.endsWith(Platform.pathSeparator)) { + return Uri.directory(path); + } + return Uri.file(path); + } +} + +extension on List { + List toJson() => [for (final uri in this) uri.toFilePath()]; +} + +extension, V extends Object?> on Map { + void sortOnKey() { + final result = {}; + final keysSorted = keys.toList()..sort(); + for (final key in keysSorted) { + result[key] = this[key] as V; + } + clear(); + addAll(result); + } +} diff --git a/pkgs/native_assets_cli/lib/src/config.dart b/pkgs/native_assets_cli/lib/src/config.dart index f764f5c93d..a285ab2147 100644 --- a/pkgs/native_assets_cli/lib/src/config.dart +++ b/pkgs/native_assets_cli/lib/src/config.dart @@ -130,7 +130,7 @@ sealed class HookInputBuilder { final class BuildInput extends HookInput { Map get metadata => { for (final entry in (_syntaxBuildInput.dependencyMetadata ?? {}).entries) - entry.key: Metadata.fromJson(as>(entry.value)), + entry.key: Metadata.fromJson(entry.value), }; @override diff --git a/pkgs/native_assets_cli/lib/src/data_assets/syntax.g.dart b/pkgs/native_assets_cli/lib/src/data_assets/syntax.g.dart index 8a87071ccd..592092901d 100644 --- a/pkgs/native_assets_cli/lib/src/data_assets/syntax.g.dart +++ b/pkgs/native_assets_cli/lib/src/data_assets/syntax.g.dart @@ -4,7 +4,9 @@ // This file is generated, do not edit. -import '../utils/json.dart'; +// ignore_for_file: unused_element + +import 'dart:io'; class Asset { final Map json; @@ -16,7 +18,7 @@ class Asset { json.sortOnKey(); } - String? get type => json.optionalString('type'); + String? get type => json.get('type'); set _type(String? value) { if (value == null) { @@ -60,13 +62,13 @@ class DataAsset extends Asset { json['file'] = value.toFilePath(); } - String get name => json.string('name'); + String get name => json.get('name'); set _name(String value) { json['name'] = value; } - String get package => json.string('package'); + String get package => json.get('package'); set _package(String value) { json['package'] = value; @@ -81,3 +83,107 @@ extension DataAssetExtension on Asset { DataAsset get asDataAsset => DataAsset.fromJson(json); } + +extension on Map { + T get(String key) { + final value = this[key]; + if (value is T) return value; + if (value == null) { + throw FormatException('No value was provided for required key: $key'); + } + throw FormatException( + 'Unexpected value \'$value\' for key \'.$key\'. ' + 'Expected a $T.', + ); + } + + List list(String key) => + _castList(get>(key), key); + + List? optionalList(String key) => + switch (get?>(key)?.cast()) { + null => null, + final l => _castList(l, key), + }; + + /// [List.cast] but with [FormatException]s. + static List _castList(List list, String key) { + for (final value in list) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$list\' (${list.runtimeType}) for key \'.$key\'. ' + 'Expected a ${List}.', + ); + } + } + return list.cast(); + } + + Map map$(String key) => + _castMap(get>(key), key); + + Map? optionalMap(String key) => + switch (get?>(key)) { + null => null, + final m => _castMap(m, key), + }; + + /// [Map.cast] but with [FormatException]s. + static Map _castMap( + Map map_, + String key, + ) { + for (final value in map_.values) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$map_\' (${map_.runtimeType}) for key \'.$key\'.' + 'Expected a ${Map}.', + ); + } + } + return map_.cast(); + } + + List? optionalStringList(String key) => optionalList(key); + + List stringList(String key) => list(key); + + Uri path(String key) => _fileSystemPathToUri(get(key)); + + Uri? optionalPath(String key) { + final value = get(key); + if (value == null) return null; + return _fileSystemPathToUri(value); + } + + List? optionalPathList(String key) { + final strings = optionalStringList(key); + if (strings == null) { + return null; + } + return [for (final string in strings) _fileSystemPathToUri(string)]; + } + + static Uri _fileSystemPathToUri(String path) { + if (path.endsWith(Platform.pathSeparator)) { + return Uri.directory(path); + } + return Uri.file(path); + } +} + +extension on List { + List toJson() => [for (final uri in this) uri.toFilePath()]; +} + +extension, V extends Object?> on Map { + void sortOnKey() { + final result = {}; + final keysSorted = keys.toList()..sort(); + for (final key in keysSorted) { + result[key] = this[key] as V; + } + clear(); + addAll(result); + } +} diff --git a/pkgs/native_assets_cli/lib/src/encoded_asset.dart b/pkgs/native_assets_cli/lib/src/encoded_asset.dart index 46a8fc33a1..959f3a6f08 100644 --- a/pkgs/native_assets_cli/lib/src/encoded_asset.dart +++ b/pkgs/native_assets_cli/lib/src/encoded_asset.dart @@ -4,6 +4,7 @@ import 'package:collection/collection.dart'; +import 'hook/syntax.g.dart' as syntax; import 'utils/json.dart'; /// An encoding of a particular asset type. @@ -17,11 +18,13 @@ final class EncodedAsset { EncodedAsset(this.type, this.encoding); /// Decode an [EncodedAsset] from json. - factory EncodedAsset.fromJson(Map json) => - EncodedAsset(json.get(_typeKey), { - for (final key in json.keys) - if (key != _typeKey) key: json[key], - }); + factory EncodedAsset.fromJson(Map json) { + final syntax_ = syntax.Asset.fromJson(json); + return EncodedAsset(syntax_.type, { + for (final key in json.keys) + if (key != _typeKey) key: json[key], + }); + } /// Encode this [EncodedAsset] tojson. Map toJson() => diff --git a/pkgs/native_assets_cli/lib/src/hook/syntax.g.dart b/pkgs/native_assets_cli/lib/src/hook/syntax.g.dart index 5bd62885da..5accc92283 100644 --- a/pkgs/native_assets_cli/lib/src/hook/syntax.g.dart +++ b/pkgs/native_assets_cli/lib/src/hook/syntax.g.dart @@ -4,7 +4,9 @@ // This file is generated, do not edit. -import '../utils/json.dart'; +// ignore_for_file: unused_element + +import 'dart:io'; class Asset { final Map json; @@ -16,7 +18,7 @@ class Asset { json.sortOnKey(); } - String get type => json.string('type'); + String get type => json.get('type'); set _type(String value) { json['type'] = value; @@ -236,7 +238,7 @@ class HookInput { json.sortOnKey(); } - String get packageName => json.string('package_name'); + String get packageName => json.get('package_name'); set packageName(String value) { json['package_name'] = value; @@ -250,7 +252,7 @@ class HookInput { json.sortOnKey(); } - String get version => json.string('version'); + String get version => json.get('version'); set version(String value) { json['version'] = value; @@ -311,14 +313,14 @@ class HookOutput { json.sortOnKey(); } - String get timestamp => json.string('timestamp'); + String get timestamp => json.get('timestamp'); set timestamp(String value) { json['timestamp'] = value; json.sortOnKey(); } - String get version => json.string('version'); + String get version => json.get('version'); set version(String value) { json['version'] = value; @@ -403,3 +405,107 @@ class LinkOutput extends HookOutput { @override String toString() => 'LinkOutput($json)'; } + +extension on Map { + T get(String key) { + final value = this[key]; + if (value is T) return value; + if (value == null) { + throw FormatException('No value was provided for required key: $key'); + } + throw FormatException( + 'Unexpected value \'$value\' for key \'.$key\'. ' + 'Expected a $T.', + ); + } + + List list(String key) => + _castList(get>(key), key); + + List? optionalList(String key) => + switch (get?>(key)?.cast()) { + null => null, + final l => _castList(l, key), + }; + + /// [List.cast] but with [FormatException]s. + static List _castList(List list, String key) { + for (final value in list) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$list\' (${list.runtimeType}) for key \'.$key\'. ' + 'Expected a ${List}.', + ); + } + } + return list.cast(); + } + + Map map$(String key) => + _castMap(get>(key), key); + + Map? optionalMap(String key) => + switch (get?>(key)) { + null => null, + final m => _castMap(m, key), + }; + + /// [Map.cast] but with [FormatException]s. + static Map _castMap( + Map map_, + String key, + ) { + for (final value in map_.values) { + if (value is! T) { + throw FormatException( + 'Unexpected value \'$map_\' (${map_.runtimeType}) for key \'.$key\'.' + 'Expected a ${Map}.', + ); + } + } + return map_.cast(); + } + + List? optionalStringList(String key) => optionalList(key); + + List stringList(String key) => list(key); + + Uri path(String key) => _fileSystemPathToUri(get(key)); + + Uri? optionalPath(String key) { + final value = get(key); + if (value == null) return null; + return _fileSystemPathToUri(value); + } + + List? optionalPathList(String key) { + final strings = optionalStringList(key); + if (strings == null) { + return null; + } + return [for (final string in strings) _fileSystemPathToUri(string)]; + } + + static Uri _fileSystemPathToUri(String path) { + if (path.endsWith(Platform.pathSeparator)) { + return Uri.directory(path); + } + return Uri.file(path); + } +} + +extension on List { + List toJson() => [for (final uri in this) uri.toFilePath()]; +} + +extension, V extends Object?> on Map { + void sortOnKey() { + final result = {}; + final keysSorted = keys.toList()..sort(); + for (final key in keysSorted) { + result[key] = this[key] as V; + } + clear(); + addAll(result); + } +} diff --git a/pkgs/native_assets_cli/lib/src/metadata.dart b/pkgs/native_assets_cli/lib/src/metadata.dart index c02e85deca..c3bd3c56a3 100644 --- a/pkgs/native_assets_cli/lib/src/metadata.dart +++ b/pkgs/native_assets_cli/lib/src/metadata.dart @@ -4,15 +4,13 @@ import 'package:collection/collection.dart'; -import 'utils/json.dart'; - class Metadata { final Map metadata; const Metadata(this.metadata); - factory Metadata.fromJson(Map? jsonMap) => - Metadata(jsonMap?.formatCast() ?? {}); + factory Metadata.fromJson(Map? jsonMap) => + Metadata(jsonMap ?? {}); Map toJson() => metadata; diff --git a/pkgs/native_assets_cli/lib/src/model/dependencies.dart b/pkgs/native_assets_cli/lib/src/model/dependencies.dart index 79920ae3d9..369b6d2b62 100644 --- a/pkgs/native_assets_cli/lib/src/model/dependencies.dart +++ b/pkgs/native_assets_cli/lib/src/model/dependencies.dart @@ -7,7 +7,6 @@ import 'dart:convert'; import 'package:collection/collection.dart'; import '../utils/file.dart'; -import '../utils/json.dart'; import '../utils/uri.dart'; class Dependencies { @@ -16,12 +15,6 @@ class Dependencies { const Dependencies(this.dependencies); - factory Dependencies.fromJson(List? jsonList) => Dependencies([ - if (jsonList != null) - for (final dependency in jsonList) - fileSystemPathToUri(as(dependency)), - ]); - List toJson() => toJsonList(dependencies); static List toJsonList(List dependencies) => [ diff --git a/pkgs/native_assets_cli/lib/src/utils/json.dart b/pkgs/native_assets_cli/lib/src/utils/json.dart index 9adfbcc975..8f6b0c07e8 100644 --- a/pkgs/native_assets_cli/lib/src/utils/json.dart +++ b/pkgs/native_assets_cli/lib/src/utils/json.dart @@ -2,165 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'dart:core'; -import 'dart:core' as core; -import 'dart:io'; - -T as(Object? object) { - if (object is T) { - return object; - } - throw FormatException( - "Unexpected value '$object' of type ${object.runtimeType} in JSON. Expected" - ' a $T.', - ); -} - -T get(Map map, String key) { - final object = map[key]; - if (object is T) { - return object; - } - throw FormatException( - "Unexpected value '$object' of type ${object.runtimeType} in JSON for key " - '$key. Expected a $T.', - ); -} - -extension MapCast on Map { - Map formatCast() => { - for (final e in entries) as(e.key): as(e.value), - }; -} - -extension MapJsonUtils on Map { - String string(String key, {Iterable? validValues}) { - final value = get(key); - if (validValues != null && !validValues.contains(value)) { - throw FormatException( - 'Json "$key" had value $value but expected one of ' - '${validValues.join(',')}', - ); - } - return value; - } - - String? optionalString(String key, {Iterable? validValues}) { - final value = getOptional(key); - if (value == null) return null; - if (validValues != null && !validValues.contains(value)) { - throw FormatException( - 'Json "$key" had value $value but expected one of ' - '${validValues.join(',')}', - ); - } - return value; - } - - core.bool bool(String key) => get(key); - core.bool? optionalBool(String key) => getOptional(key); - core.int int(String key) => get(key); - core.int? optionalInt(String key) => getOptional(key); - - Uri path(String key) => _fileSystemPathToUri(get(key)); - - Uri? optionalPath(String key) { - final value = getOptional(key); - if (value == null) return null; - return _fileSystemPathToUri(value); - } - - List? optionalStringList(String key) { - final value = getOptional>(key); - if (value == null) return null; - return value.cast(); - } - - List stringList(String key) => get>(key).cast(); - - List? optionalPathList(String key) { - final strings = optionalStringList(key); - if (strings == null) { - return null; - } - return [for (final string in strings) _fileSystemPathToUri(string)]; - } - - List list(String key) => get>(key); - List? optionalList(String key) => getOptional>(key); - Map map$(String key) => get>(key); - Map? optionalMap(String key) { - final map_ = getOptional>(key); - if (map_ is Map?) return map_; - for (final value in map_.values) { - if (value is! T) { - throw FormatException( - 'Unexpected value \'$map_\' (${map_.runtimeType}) for key \'.$key\'' - ' in input file. Expected a ${Map}?.', - ); - } - } - return map_.cast(); - } - - T get(String key) { - final value = this[key]; - if (value == null) { - throw FormatException('No value was provided for required key: $key'); - } - if (value is T) return value; - throw FormatException( - 'Unexpected value \'$value\' for key \'.$key\' in input file. ' - 'Expected a $T.', - ); - } - - T? getOptional(String key) { - final value = this[key]; - if (value is T?) return value; - throw FormatException( - 'Unexpected value \'$value\' for key \'.$key\' in input file. ' - 'Expected a $T?.', - ); - } - - void setNested(List nestedMapKeys, Object? value) { - var map = this; - for (final key in nestedMapKeys.sublist(0, nestedMapKeys.length - 1)) { - map = (map[key] ??= {}) as Map; - } - map[nestedMapKeys.last] = value; - } -} - -extension ListJsonUtils on List { - T get(int index) { - final value = this[index]; - if (value == null) { - throw FormatException('No value was provided for index: $index'); - } - if (value is T) return value; - throw FormatException( - 'Unexpected value \'$value\' for index \'.$index\' in input file. ' - 'Expected a $T.', - ); - } - - Map mapAt(int index) => get>(index); - Uri pathAt(int index) => _fileSystemPathToUri(get(index)); -} - -Uri _fileSystemPathToUri(String path) { - if (path.endsWith(Platform.pathSeparator)) { - return Uri.directory(path); - } - return Uri.file(path); -} - -extension UriList on List { - List toJson() => [for (final uri in this) uri.toFilePath()]; -} - extension MapSorting, V extends Object?> on Map { void sortOnKey() { final result = {}; diff --git a/pkgs/native_assets_cli/test/build_config_test.dart b/pkgs/native_assets_cli/test/build_config_test.dart index 1e10360da7..47e421f420 100644 --- a/pkgs/native_assets_cli/test/build_config_test.dart +++ b/pkgs/native_assets_cli/test/build_config_test.dart @@ -168,7 +168,7 @@ void main() async { e is FormatException && e.message.contains('Unexpected value') && e.message.contains( - 'Expected a Map>?', + 'Expected a Map>', ), ), ), diff --git a/pkgs/native_assets_cli/test/link_config_test.dart b/pkgs/native_assets_cli/test/link_config_test.dart index 4a6d4be499..32785e9a6d 100644 --- a/pkgs/native_assets_cli/test/link_config_test.dart +++ b/pkgs/native_assets_cli/test/link_config_test.dart @@ -138,7 +138,7 @@ void main() async { (e) => e is FormatException && e.message.contains( - "Unexpected value 'astring' for key '.assets' in input file. " + "Unexpected value 'astring' for key '.assets'. " 'Expected a List?.', ), ),