|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +/// Helper methods for the code generator that are added to the generated file. |
| 6 | +/// |
| 7 | +/// This simplifies the code generator. |
| 8 | +const helperLib = r''' |
| 9 | +extension on Map<String, Object?> { |
| 10 | + T get<T extends Object?>(String key) { |
| 11 | + final value = this[key]; |
| 12 | + if (value is T) return value; |
| 13 | + if (value == null) { |
| 14 | + throw FormatException('No value was provided for required key: $key'); |
| 15 | + } |
| 16 | + throw FormatException( |
| 17 | + 'Unexpected value \'$value\' for key \'.$key\'. ' |
| 18 | + 'Expected a $T.', |
| 19 | + ); |
| 20 | + } |
| 21 | +
|
| 22 | + List<T> list<T extends Object?>(String key) => |
| 23 | + _castList<T>(get<List<Object?>>(key), key); |
| 24 | +
|
| 25 | + List<T>? optionalList<T extends Object?>(String key) => |
| 26 | + switch (get<List<Object?>?>(key)?.cast<T>()) { |
| 27 | + null => null, |
| 28 | + final l => _castList<T>(l, key), |
| 29 | + }; |
| 30 | +
|
| 31 | + /// [List.cast] but with [FormatException]s. |
| 32 | + static List<T> _castList<T extends Object?>(List<Object?> list, String key) { |
| 33 | + for (final value in list) { |
| 34 | + if (value is! T) { |
| 35 | + throw FormatException( |
| 36 | + 'Unexpected value \'$list\' (${list.runtimeType}) for key \'.$key\'. ' |
| 37 | + 'Expected a ${List<T>}.', |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + return list.cast(); |
| 42 | + } |
| 43 | +
|
| 44 | + Map<String, T> map$<T extends Object?>(String key) => |
| 45 | + _castMap<T>(get<Map<String, Object?>>(key), key); |
| 46 | +
|
| 47 | + Map<String, T>? optionalMap<T extends Object?>(String key) => |
| 48 | + switch (get<Map<String, Object?>?>(key)) { |
| 49 | + null => null, |
| 50 | + final m => _castMap<T>(m, key), |
| 51 | + }; |
| 52 | +
|
| 53 | + /// [Map.cast] but with [FormatException]s. |
| 54 | + static Map<String, T> _castMap<T extends Object?>( |
| 55 | + Map<String, Object?> map_, |
| 56 | + String key, |
| 57 | + ) { |
| 58 | + for (final value in map_.values) { |
| 59 | + if (value is! T) { |
| 60 | + throw FormatException( |
| 61 | + 'Unexpected value \'$map_\' (${map_.runtimeType}) for key \'.$key\'.' |
| 62 | + 'Expected a ${Map<String, T>}.', |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + return map_.cast(); |
| 67 | + } |
| 68 | +
|
| 69 | + List<String>? optionalStringList(String key) => optionalList<String>(key); |
| 70 | +
|
| 71 | + List<String> stringList(String key) => list<String>(key); |
| 72 | +
|
| 73 | + Uri path(String key) => _fileSystemPathToUri(get<String>(key)); |
| 74 | +
|
| 75 | + Uri? optionalPath(String key) { |
| 76 | + final value = get<String?>(key); |
| 77 | + if (value == null) return null; |
| 78 | + return _fileSystemPathToUri(value); |
| 79 | + } |
| 80 | +
|
| 81 | + List<Uri>? optionalPathList(String key) { |
| 82 | + final strings = optionalStringList(key); |
| 83 | + if (strings == null) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + return [for (final string in strings) _fileSystemPathToUri(string)]; |
| 87 | + } |
| 88 | +
|
| 89 | + static Uri _fileSystemPathToUri(String path) { |
| 90 | + if (path.endsWith(Platform.pathSeparator)) { |
| 91 | + return Uri.directory(path); |
| 92 | + } |
| 93 | + return Uri.file(path); |
| 94 | + } |
| 95 | +} |
| 96 | +
|
| 97 | +extension on List<Uri> { |
| 98 | + List<String> toJson() => [for (final uri in this) uri.toFilePath()]; |
| 99 | +} |
| 100 | +
|
| 101 | +extension <K extends Comparable<K>, V extends Object?> on Map<K, V> { |
| 102 | + void sortOnKey() { |
| 103 | + final result = <K, V>{}; |
| 104 | + final keysSorted = keys.toList()..sort(); |
| 105 | + for (final key in keysSorted) { |
| 106 | + result[key] = this[key] as V; |
| 107 | + } |
| 108 | + clear(); |
| 109 | + addAll(result); |
| 110 | + } |
| 111 | +} |
| 112 | +'''; |
0 commit comments