Skip to content

Commit d30c2fd

Browse files
authored
checked_yaml: prepare release (#1500)
Did some cleanup of the implementation so the control flow reads easier Added a missing test to handle the `null` case.
1 parent 275eba6 commit d30c2fd

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

checked_yaml/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 2.0.4-wip
1+
## 2.0.4
22

33
- Require Dart 3.8
44

checked_yaml/lib/checked_yaml.dart

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ T checkedYamlDecode<T>(
3636
if (yaml is YamlMap) {
3737
map = yaml;
3838
} else if (allowNull && yaml is YamlScalar && yaml.value == null) {
39-
// TODO: test this case!
4039
map = null;
4140
} else {
4241
throw ParsedYamlException('Not a map', yaml);
@@ -73,34 +72,29 @@ ParsedYamlException toParsedYamlException(
7372
)
7473
as YamlNode;
7574
return ParsedYamlException(exception.message!, node, innerError: exception);
76-
} else {
77-
if (exception.key == null) {
78-
return ParsedYamlException(
79-
exception.message ?? 'There was an error parsing the map.',
80-
yamlMap,
81-
innerError: exception,
82-
);
83-
} else if (!yamlMap.containsKey(exception.key)) {
84-
return ParsedYamlException(
85-
[
86-
'Missing key "${exception.key}".',
87-
if (exception.message != null) exception.message!,
88-
].join(' '),
89-
yamlMap,
90-
innerError: exception,
91-
);
92-
} else {
93-
var message = 'Unsupported value for "${exception.key}".';
94-
if (exception.message != null) {
95-
message = '$message ${exception.message}';
96-
}
97-
return ParsedYamlException(
98-
message,
99-
yamlMap.nodes[exception.key] ?? yamlMap,
100-
innerError: exception,
101-
);
102-
}
10375
}
76+
77+
if (exception.key == null) {
78+
return ParsedYamlException(
79+
exception.message ?? 'There was an error parsing the map.',
80+
yamlMap,
81+
innerError: exception,
82+
);
83+
}
84+
85+
if (!yamlMap.containsKey(exception.key)) {
86+
return ParsedYamlException(
87+
['Missing key "${exception.key}".', ?exception.message].join(' '),
88+
yamlMap,
89+
innerError: exception,
90+
);
91+
}
92+
93+
return ParsedYamlException(
94+
['Unsupported value for "${exception.key}".', ?exception.message].join(' '),
95+
yamlMap.nodes[exception.key] ?? yamlMap,
96+
innerError: exception,
97+
);
10498
}
10599

106100
/// An exception thrown when parsing YAML that contains information about the
@@ -118,12 +112,10 @@ class ParsedYamlException implements Exception {
118112
/// contains the source error object.
119113
final Object? innerError;
120114

121-
ParsedYamlException(String message, YamlNode this.yamlNode, {this.innerError})
122-
: // TODO(kevmoo) remove when dart-lang/sdk#50756 is fixed!
123-
message = message.replaceAll(" of ' in type cast'", ' in type cast');
115+
ParsedYamlException(this.message, YamlNode this.yamlNode, {this.innerError});
124116

125-
factory ParsedYamlException.fromYamlException(YamlException exception) =>
126-
_WrappedYamlException(exception);
117+
factory ParsedYamlException.fromYamlException(YamlException exception) =
118+
_WrappedYamlException;
127119

128120
/// Returns [message] formatted with source information provided by
129121
/// [yamlNode].
@@ -147,7 +139,4 @@ class _WrappedYamlException implements ParsedYamlException {
147139

148140
@override
149141
YamlNode? get yamlNode => null;
150-
151-
@override
152-
String toString() => 'ParsedYamlException: $formattedMessage';
153142
}

checked_yaml/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: checked_yaml
2-
version: 2.0.4-wip
2+
version: 2.0.4
33

44
description: >-
55
Generate more helpful exceptions when decoding YAML documents using

checked_yaml/test/custom_error_test.dart

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:test/test.dart';
44
import 'package:yaml/yaml.dart';
55

66
void main() {
7-
test('bob', () {
7+
test('simple test', () {
88
expect(
99
() => checkedYamlDecode('{"innerMap": {}}', (m) {
1010
throw CheckedFromJsonException(
@@ -36,4 +36,35 @@ line 1, column 14: There was an error parsing the map.
3636
),
3737
);
3838
});
39+
40+
test('null map - allowed', () {
41+
expect(
42+
() => checkedYamlDecode(allowNull: true, 'null', (m) => Object()),
43+
isA<Object>(),
44+
);
45+
});
46+
47+
test('null map - not allowed', () {
48+
expect(
49+
() => checkedYamlDecode('null', (m) {
50+
throw TestFailure('should never get here!');
51+
}),
52+
throwsA(
53+
isA<ParsedYamlException>()
54+
.having((e) => e.message, 'message', 'Not a map')
55+
.having(
56+
(e) => e.yamlNode,
57+
'yamlNode',
58+
isA<YamlScalar>().having((s) => s.value, 'value', isNull),
59+
)
60+
.having((e) => e.innerError, 'innerError', isNull)
61+
.having((e) => e.formattedMessage, 'formattedMessage', '''
62+
line 1, column 1: Not a map
63+
64+
1 │ null
65+
│ ^^^^
66+
╵'''),
67+
),
68+
);
69+
});
3970
}

0 commit comments

Comments
 (0)