Skip to content

Commit c1c1d82

Browse files
authored
Migrate checked_yaml to null safety (#762)
Fixes #758
1 parent 30e0505 commit c1c1d82

File tree

19 files changed

+84
-174
lines changed

19 files changed

+84
-174
lines changed

.github/workflows/dart.yml

Lines changed: 7 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ jobs:
3636
- run: pub global activate mono_repo 3.3.0
3737
- run: pub global run mono_repo generate --validate
3838
job_002:
39-
name: "OS: linux; SDK: dev; PKGS: _test_yaml, checked_yaml, json_annotation, json_serializable; TASKS: [`dartfmt -n --set-exit-if-changed .`, `dartanalyzer --fatal-warnings --fatal-infos .`]"
39+
name: "OS: linux; SDK: dev; PKGS: _test_yaml, checked_yaml, example, json_annotation, json_serializable; TASKS: [`dartfmt -n --set-exit-if-changed .`, `dartanalyzer --fatal-infos .`]"
4040
runs-on: ubuntu-latest
4141
steps:
4242
- name: Cache Pub hosted dependencies
4343
uses: actions/cache@v2
4444
with:
4545
path: "~/.pub-cache/hosted"
46-
key: "os:ubuntu-latest;pub-cache-hosted;dart:dev;packages:_test_yaml-checked_yaml-json_annotation-json_serializable;commands:dartfmt-dartanalyzer_0"
46+
key: "os:ubuntu-latest;pub-cache-hosted;dart:dev;packages:_test_yaml-checked_yaml-example-json_annotation-json_serializable;commands:dartfmt-dartanalyzer"
4747
restore-keys: |
48-
os:ubuntu-latest;pub-cache-hosted;dart:dev;packages:_test_yaml-checked_yaml-json_annotation-json_serializable
48+
os:ubuntu-latest;pub-cache-hosted;dart:dev;packages:_test_yaml-checked_yaml-example-json_annotation-json_serializable
4949
os:ubuntu-latest;pub-cache-hosted;dart:dev
5050
os:ubuntu-latest;pub-cache-hosted
5151
os:ubuntu-latest
@@ -55,56 +55,9 @@ jobs:
5555
- run: dart --version
5656
- uses: actions/checkout@v2
5757
- env:
58-
PKGS: _test_yaml checked_yaml json_annotation json_serializable
58+
PKGS: _test_yaml checked_yaml example json_annotation json_serializable
5959
TRAVIS_OS_NAME: linux
60-
run: tool/ci.sh dartfmt dartanalyzer_0
61-
job_005:
62-
name: "OS: linux; SDK: 2.7.0; PKG: checked_yaml; TASKS: `dartanalyzer --fatal-warnings .`"
63-
runs-on: ubuntu-latest
64-
steps:
65-
- name: Cache Pub hosted dependencies
66-
uses: actions/cache@v2
67-
with:
68-
path: "~/.pub-cache/hosted"
69-
key: "os:ubuntu-latest;pub-cache-hosted;dart:2.7.0;packages:checked_yaml;commands:dartanalyzer_1"
70-
restore-keys: |
71-
os:ubuntu-latest;pub-cache-hosted;dart:2.7.0;packages:checked_yaml
72-
os:ubuntu-latest;pub-cache-hosted;dart:2.7.0
73-
os:ubuntu-latest;pub-cache-hosted
74-
os:ubuntu-latest
75-
- uses: cedx/setup-dart@v2
76-
with:
77-
release-channel: stable
78-
version: "2.7.0"
79-
- run: dart --version
80-
- uses: actions/checkout@v2
81-
- env:
82-
PKGS: checked_yaml
83-
TRAVIS_OS_NAME: linux
84-
run: tool/ci.sh dartanalyzer_1
85-
job_008:
86-
name: "OS: linux; SDK: dev; PKG: example; TASKS: [`dartfmt -n --set-exit-if-changed .`, `dartanalyzer --fatal-warnings .`]"
87-
runs-on: ubuntu-latest
88-
steps:
89-
- name: Cache Pub hosted dependencies
90-
uses: actions/cache@v2
91-
with:
92-
path: "~/.pub-cache/hosted"
93-
key: "os:ubuntu-latest;pub-cache-hosted;dart:dev;packages:example;commands:dartfmt-dartanalyzer_1"
94-
restore-keys: |
95-
os:ubuntu-latest;pub-cache-hosted;dart:dev;packages:example
96-
os:ubuntu-latest;pub-cache-hosted;dart:dev
97-
os:ubuntu-latest;pub-cache-hosted
98-
os:ubuntu-latest
99-
- uses: cedx/setup-dart@v2
100-
with:
101-
release-channel: dev
102-
- run: dart --version
103-
- uses: actions/checkout@v2
104-
- env:
105-
PKGS: example
106-
TRAVIS_OS_NAME: linux
107-
run: tool/ci.sh dartfmt dartanalyzer_1
60+
run: tool/ci.sh dartfmt dartanalyzer
10861
job_003:
10962
name: "OS: linux; SDK: dev; PKGS: _test_yaml, checked_yaml, example, json_serializable; TASKS: `pub run test`"
11063
runs-on: ubuntu-latest
@@ -131,38 +84,7 @@ jobs:
13184
needs:
13285
- job_001
13386
- job_002
134-
- job_005
135-
- job_008
136-
job_006:
137-
name: "OS: linux; SDK: 2.7.0; PKG: checked_yaml; TASKS: `pub run test`"
138-
runs-on: ubuntu-latest
139-
steps:
140-
- name: Cache Pub hosted dependencies
141-
uses: actions/cache@v2
142-
with:
143-
path: "~/.pub-cache/hosted"
144-
key: "os:ubuntu-latest;pub-cache-hosted;dart:2.7.0;packages:checked_yaml;commands:test_0"
145-
restore-keys: |
146-
os:ubuntu-latest;pub-cache-hosted;dart:2.7.0;packages:checked_yaml
147-
os:ubuntu-latest;pub-cache-hosted;dart:2.7.0
148-
os:ubuntu-latest;pub-cache-hosted
149-
os:ubuntu-latest
150-
- uses: cedx/setup-dart@v2
151-
with:
152-
release-channel: stable
153-
version: "2.7.0"
154-
- run: dart --version
155-
- uses: actions/checkout@v2
156-
- env:
157-
PKGS: checked_yaml
158-
TRAVIS_OS_NAME: linux
159-
run: tool/ci.sh test_0
160-
needs:
161-
- job_001
162-
- job_002
163-
- job_005
164-
- job_008
165-
job_009:
87+
job_005:
16688
name: "OS: linux; SDK: dev; PKG: json_serializable; TASKS: `pub run test -p chrome`"
16789
runs-on: ubuntu-latest
16890
steps:
@@ -188,8 +110,6 @@ jobs:
188110
needs:
189111
- job_001
190112
- job_002
191-
- job_005
192-
- job_008
193113
job_004:
194114
name: "OS: linux; SDK: dev; PKGS: _test_yaml, checked_yaml, example, json_serializable; TASKS: `pub run test --run-skipped -t presubmit-only test/ensure_build_test.dart`"
195115
runs-on: ubuntu-latest
@@ -215,33 +135,4 @@ jobs:
215135
run: tool/ci.sh test_1
216136
needs:
217137
- job_003
218-
- job_006
219-
- job_009
220-
job_007:
221-
name: "OS: linux; SDK: 2.7.0; PKG: checked_yaml; TASKS: `pub run test --run-skipped -t presubmit-only test/ensure_build_test.dart`"
222-
runs-on: ubuntu-latest
223-
steps:
224-
- name: Cache Pub hosted dependencies
225-
uses: actions/cache@v2
226-
with:
227-
path: "~/.pub-cache/hosted"
228-
key: "os:ubuntu-latest;pub-cache-hosted;dart:2.7.0;packages:checked_yaml;commands:test_1"
229-
restore-keys: |
230-
os:ubuntu-latest;pub-cache-hosted;dart:2.7.0;packages:checked_yaml
231-
os:ubuntu-latest;pub-cache-hosted;dart:2.7.0
232-
os:ubuntu-latest;pub-cache-hosted
233-
os:ubuntu-latest
234-
- uses: cedx/setup-dart@v2
235-
with:
236-
release-channel: stable
237-
version: "2.7.0"
238-
- run: dart --version
239-
- uses: actions/checkout@v2
240-
- env:
241-
PKGS: checked_yaml
242-
TRAVIS_OS_NAME: linux
243-
run: tool/ci.sh test_1
244-
needs:
245-
- job_003
246-
- job_006
247-
- job_009
138+
- job_005

_test_yaml/mono_pkg.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ stages:
66
- analyzer_and_format:
77
- group:
88
- dartfmt
9-
- dartanalyzer: --fatal-warnings --fatal-infos .
10-
dart: [dev]
9+
- dartanalyzer: --fatal-infos .
1110
- unit_test:
1211
- test
1312
- ensure_build:

_test_yaml/pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ dependency_overrides:
1919
path: ../json_annotation
2020
json_serializable:
2121
path: ../json_serializable
22+
23+
# For yaml!
24+
yaml: 3.0.0-nullsafety.0

_test_yaml/test/yaml_test.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ void main() {
2727
for (final filePath in _tests) {
2828
test(p.basenameWithoutExtension(filePath), () {
2929
final content = File(filePath).readAsStringSync();
30-
final yamlContent = loadYaml(content, sourceUrl: filePath) as YamlMap;
30+
final yamlContent = loadYaml(
31+
content,
32+
sourceUrl: Uri.file(filePath),
33+
) as YamlMap;
3134

3235
try {
3336
final config = Config.fromJson(yamlContent);
@@ -47,7 +50,7 @@ void main() {
4750
for (final entry in _badConfigs.entries) {
4851
test('${index++}', () {
4952
final yamlContent =
50-
loadYaml(entry.key, sourceUrl: 'file.yaml') as YamlMap;
53+
loadYaml(entry.key, sourceUrl: Uri.file('file.yaml')) as YamlMap;
5154

5255
expect(yamlContent, isNotNull);
5356
printOnFailure(entry.key);

checked_yaml/changelog.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## 1.0.3-dev
1+
## 2.0.0-nullsafety-dev
22

3-
- Require at least Dart `2.7.0`.
3+
- *BREAKING* `checkedYamlDecode` `sourceUrl` parameter is now a `Uri`.
4+
- Require at least Dart `2.12.0-0`.
45

56
## 1.0.2
67

checked_yaml/example/example.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ part 'example.g.dart';
1313
anyMap: true,
1414
checked: true,
1515
disallowUnrecognizedKeys: true,
16-
nullable: false,
1716
)
1817
class Configuration {
1918
@JsonKey(required: true)
2019
final String name;
2120
@JsonKey(required: true)
2221
final int count;
2322

24-
Configuration({this.name, this.count}) {
23+
Configuration({required this.name, required this.count}) {
2524
if (name.isEmpty) {
2625
throw ArgumentError.value(name, 'name', 'Cannot be empty.');
2726
}
@@ -36,18 +35,21 @@ class Configuration {
3635
}
3736

3837
void main(List<String> arguments) {
39-
var sourcePathOrYaml = arguments.single;
38+
final sourcePathOrYaml = arguments.single;
4039
String yamlContent;
40+
Uri? sourceUri;
4141

4242
if (FileSystemEntity.isFileSync(sourcePathOrYaml)) {
4343
yamlContent = File(sourcePathOrYaml).readAsStringSync();
44+
sourceUri = Uri.parse(sourcePathOrYaml);
4445
} else {
4546
yamlContent = sourcePathOrYaml;
46-
sourcePathOrYaml = null;
4747
}
4848

4949
final config = checkedYamlDecode(
50-
yamlContent, (m) => Configuration.fromJson(m),
51-
sourceUrl: sourcePathOrYaml);
50+
yamlContent,
51+
(m) => Configuration.fromJson(m!),
52+
sourceUrl: sourceUri,
53+
);
5254
print(config);
5355
}

checked_yaml/lib/checked_yaml.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import 'package:yaml/yaml.dart';
2020
/// `null` values.
2121
T checkedYamlDecode<T>(
2222
String yamlContent,
23-
T Function(Map) constructor, {
24-
sourceUrl,
23+
T Function(Map?) constructor, {
24+
Uri? sourceUrl,
2525
bool allowNull = false,
2626
}) {
27-
allowNull ??= false;
2827
YamlNode yaml;
2928

3029
try {
@@ -33,7 +32,7 @@ T checkedYamlDecode<T>(
3332
throw ParsedYamlException.fromYamlException(e);
3433
}
3534

36-
Map map;
35+
Map? map;
3736
if (yaml is YamlMap) {
3837
map = yaml;
3938
} else if (allowNull && yaml is YamlScalar && yaml.value == null) {
@@ -56,7 +55,7 @@ T checkedYamlDecode<T>(
5655
/// `package:yaml`. If not, you may provide an alternative via [exceptionMap].
5756
ParsedYamlException toParsedYamlException(
5857
CheckedFromJsonException exception, {
59-
YamlMap exceptionMap,
58+
YamlMap? exceptionMap,
6059
}) {
6160
final yamlMap = exceptionMap ?? exception.map as YamlMap;
6261

@@ -71,7 +70,7 @@ ParsedYamlException toParsedYamlException(
7170
(k) => (k as YamlScalar).value == key,
7271
orElse: () => yamlMap) as YamlNode;
7372
return ParsedYamlException(
74-
exception.message,
73+
exception.message!,
7574
node,
7675
innerError: exception,
7776
);
@@ -81,7 +80,7 @@ ParsedYamlException toParsedYamlException(
8180
if (yamlValue == null) {
8281
// TODO: test this case!
8382
return ParsedYamlException(
84-
exception.message,
83+
exception.message!,
8584
yamlMap,
8685
innerError: exception,
8786
);
@@ -108,25 +107,26 @@ class ParsedYamlException implements Exception {
108107
/// The node associated with this exception.
109108
///
110109
/// May be `null` if there was an error decoding.
111-
final YamlNode yamlNode;
110+
final YamlNode? yamlNode;
112111

113112
/// If this exception was thrown as a result of another error,
114113
/// contains the source error object.
115-
final Object innerError;
114+
final Object? innerError;
116115

117116
ParsedYamlException(
118117
this.message,
119-
this.yamlNode, {
118+
YamlNode yamlNode, {
120119
this.innerError,
121-
}) : assert(message != null),
122-
assert(yamlNode != null);
120+
}) :
121+
// ignore: prefer_initializing_formals
122+
yamlNode = yamlNode;
123123

124124
factory ParsedYamlException.fromYamlException(YamlException exception) =>
125125
_WrappedYamlException(exception);
126126

127127
/// Returns [message] formatted with source information provided by
128128
/// [yamlNode].
129-
String get formattedMessage => yamlNode.span.message(message);
129+
String? get formattedMessage => yamlNode?.span.message(message);
130130

131131
@override
132132
String toString() => 'ParsedYamlException: $formattedMessage';
@@ -136,7 +136,7 @@ class _WrappedYamlException implements ParsedYamlException {
136136
_WrappedYamlException(this.innerError);
137137

138138
@override
139-
String get formattedMessage => innerError.span.message(innerError.message);
139+
String? get formattedMessage => innerError.span?.message(innerError.message);
140140

141141
@override
142142
final YamlException innerError;
@@ -145,7 +145,7 @@ class _WrappedYamlException implements ParsedYamlException {
145145
String get message => innerError.message;
146146

147147
@override
148-
YamlNode get yamlNode => null;
148+
YamlNode? get yamlNode => null;
149149

150150
@override
151151
String toString() => 'ParsedYamlException: $formattedMessage';

checked_yaml/mono_pkg.yaml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
# See https://github.com/google/mono_repo.dart for details on this file
22
dart:
3-
- 2.7.0
43
- dev
54

65
stages:
76
- analyzer_and_format:
87
- group:
98
- dartfmt
10-
- dartanalyzer: --fatal-warnings --fatal-infos .
11-
dart: [dev]
12-
- group:
13-
- dartanalyzer: --fatal-warnings .
14-
dart: [2.7.0]
9+
- dartanalyzer: --fatal-infos .
1510

1611
- unit_test:
1712
- test

0 commit comments

Comments
 (0)