From 39ad8bf150f01257979ec1c714eb978b4677d69b Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Wed, 4 Jun 2025 15:17:21 -0700 Subject: [PATCH 1/2] checked_yaml: prepare release Did some reasonable cleanup of the implementation --- checked_yaml/CHANGELOG.md | 2 +- checked_yaml/lib/checked_yaml.dart | 66 ++++++++++++++---------------- checked_yaml/pubspec.yaml | 2 +- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/checked_yaml/CHANGELOG.md b/checked_yaml/CHANGELOG.md index a3b7dac7..567d1479 100644 --- a/checked_yaml/CHANGELOG.md +++ b/checked_yaml/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.0.4-wip +## 2.0.4 - Require Dart 3.8 diff --git a/checked_yaml/lib/checked_yaml.dart b/checked_yaml/lib/checked_yaml.dart index 17c5ce0c..8932abc6 100644 --- a/checked_yaml/lib/checked_yaml.dart +++ b/checked_yaml/lib/checked_yaml.dart @@ -73,34 +73,29 @@ ParsedYamlException toParsedYamlException( ) as YamlNode; return ParsedYamlException(exception.message!, node, innerError: exception); - } else { - if (exception.key == null) { - return ParsedYamlException( - exception.message ?? 'There was an error parsing the map.', - yamlMap, - innerError: exception, - ); - } else if (!yamlMap.containsKey(exception.key)) { - return ParsedYamlException( - [ - 'Missing key "${exception.key}".', - if (exception.message != null) exception.message!, - ].join(' '), - yamlMap, - innerError: exception, - ); - } else { - var message = 'Unsupported value for "${exception.key}".'; - if (exception.message != null) { - message = '$message ${exception.message}'; - } - return ParsedYamlException( - message, - yamlMap.nodes[exception.key] ?? yamlMap, - innerError: exception, - ); - } } + + if (exception.key == null) { + return ParsedYamlException( + exception.message ?? 'There was an error parsing the map.', + yamlMap, + innerError: exception, + ); + } + + if (!yamlMap.containsKey(exception.key)) { + return ParsedYamlException( + ['Missing key "${exception.key}".', ?exception.message].join(' '), + yamlMap, + innerError: exception, + ); + } + + return ParsedYamlException( + ['Unsupported value for "${exception.key}".', ?exception.message].join(' '), + yamlMap.nodes[exception.key] ?? yamlMap, + innerError: exception, + ); } /// An exception thrown when parsing YAML that contains information about the @@ -118,12 +113,14 @@ class ParsedYamlException implements Exception { /// contains the source error object. final Object? innerError; - ParsedYamlException(String message, YamlNode this.yamlNode, {this.innerError}) - : // TODO(kevmoo) remove when dart-lang/sdk#50756 is fixed! - message = message.replaceAll(" of ' in type cast'", ' in type cast'); + const ParsedYamlException( + this.message, + YamlNode this.yamlNode, { + this.innerError, + }); - factory ParsedYamlException.fromYamlException(YamlException exception) => - _WrappedYamlException(exception); + factory ParsedYamlException.fromYamlException(YamlException exception) = + _WrappedYamlException; /// Returns [message] formatted with source information provided by /// [yamlNode]. @@ -134,7 +131,7 @@ class ParsedYamlException implements Exception { } class _WrappedYamlException implements ParsedYamlException { - _WrappedYamlException(this.innerError); + const _WrappedYamlException(this.innerError); @override String? get formattedMessage => innerError.span?.message(innerError.message); @@ -147,7 +144,4 @@ class _WrappedYamlException implements ParsedYamlException { @override YamlNode? get yamlNode => null; - - @override - String toString() => 'ParsedYamlException: $formattedMessage'; } diff --git a/checked_yaml/pubspec.yaml b/checked_yaml/pubspec.yaml index ada78745..6195805d 100644 --- a/checked_yaml/pubspec.yaml +++ b/checked_yaml/pubspec.yaml @@ -1,5 +1,5 @@ name: checked_yaml -version: 2.0.4-wip +version: 2.0.4 description: >- Generate more helpful exceptions when decoding YAML documents using From f4203c32e8c3a034fa17f1d5af5439fe3debca8f Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Wed, 4 Jun 2025 15:27:21 -0700 Subject: [PATCH 2/2] is this better? --- checked_yaml/lib/checked_yaml.dart | 65 +++++++++++++++--------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/checked_yaml/lib/checked_yaml.dart b/checked_yaml/lib/checked_yaml.dart index 8932abc6..2a988308 100644 --- a/checked_yaml/lib/checked_yaml.dart +++ b/checked_yaml/lib/checked_yaml.dart @@ -59,43 +59,42 @@ ParsedYamlException toParsedYamlException( }) { final yamlMap = exceptionMap ?? exception.map as YamlMap; - final innerError = exception.innerError; - - if (exception.badKey) { - final key = (innerError is UnrecognizedKeysException) - ? innerError.unrecognizedKeys.first - : exception.key; - - final node = - yamlMap.nodes.keys.singleWhere( - (k) => (k as YamlScalar).value == key, - orElse: () => yamlMap, - ) - as YamlNode; - return ParsedYamlException(exception.message!, node, innerError: exception); - } - - if (exception.key == null) { - return ParsedYamlException( + return switch (exception) { + CheckedFromJsonException(badKey: true, message: final String message) => + () { + final innerError = exception.innerError; + final key = (innerError is UnrecognizedKeysException) + ? innerError.unrecognizedKeys.first + : exception.key; + + final node = + yamlMap.nodes.keys.singleWhere( + (k) => (k as YamlScalar).value == key, + orElse: () => yamlMap, + ) + as YamlNode; + return ParsedYamlException(message, node, innerError: exception); + }(), + CheckedFromJsonException(key: null) => ParsedYamlException( exception.message ?? 'There was an error parsing the map.', yamlMap, innerError: exception, - ); - } - - if (!yamlMap.containsKey(exception.key)) { - return ParsedYamlException( - ['Missing key "${exception.key}".', ?exception.message].join(' '), - yamlMap, + ), + CheckedFromJsonException(key: final key!) when !yamlMap.containsKey(key) => + ParsedYamlException( + ['Missing key "$key".', ?exception.message].join(' '), + yamlMap, + innerError: exception, + ), + _ => ParsedYamlException( + [ + 'Unsupported value for "${exception.key}".', + ?exception.message, + ].join(' '), + yamlMap.nodes[exception.key] ?? yamlMap, innerError: exception, - ); - } - - return ParsedYamlException( - ['Unsupported value for "${exception.key}".', ?exception.message].join(' '), - yamlMap.nodes[exception.key] ?? yamlMap, - innerError: exception, - ); + ), + }; } /// An exception thrown when parsing YAML that contains information about the