Skip to content

Commit 2657dfc

Browse files
authored
Add ConstantReader.peek. (#243)
* Add ConstantReader.peek. * Update constants_test.dart
1 parent 30ad156 commit 2657dfc

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313
`package:json_serializable`.
1414
* Removed `lib/builder.dart`. Import through `source_gen.dart` instead.
1515
* Removed `OutputFormatter` typedef.
16+
1617
* Add `LibraryReader.allElements` - a utility to iterate across all `Element`
1718
instances contained in Dart library.
1819
* Add `LibraryReader.element` to get back to the `LibraryElement` instance.
20+
* Add `ConstantReader.peek` to read a value that returns `null` if not found:
21+
22+
```dart
23+
// Tries to read the field "token" first, then "_token".
24+
findTokenField(DartObject o) {
25+
final reader = new ConstantReader(o);
26+
final token = o.peek('token') ?? o.read('_token');
27+
}
28+
```
1929

2030
## 0.6.1+1
2131

lib/src/constants.dart

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ abstract class ConstantReader {
152152
/// [FormatException].
153153
ConstantReader read(String field);
154154

155+
/// Reads [field] from the constant as another constant value.
156+
///
157+
/// Unlike [read], returns `null` if the field is not found.
158+
ConstantReader peek(String field);
159+
155160
/// Returns as a revived meta class.
156161
///
157162
/// This is appropriate for cases where the underlying object is not a literal
@@ -224,6 +229,9 @@ class _NullConstant implements ConstantReader {
224229
@override
225230
ConstantReader read(_) => throw new UnsupportedError('Null');
226231

232+
@override
233+
ConstantReader peek(_) => null;
234+
227235
@override
228236
Revivable revive() => throw new UnsupportedError('Null');
229237

@@ -307,14 +315,24 @@ class _Constant implements ConstantReader {
307315
checker.isAssignableFromType(_object.type);
308316

309317
@override
310-
ConstantReader read(String field) {
318+
ConstantReader peek(String field) {
311319
final constant = new ConstantReader(_getFieldRecursive(_object, field));
312320
if (constant.isNull) {
313-
_assertHasField(_object?.type?.element, field);
321+
return null;
314322
}
315323
return constant;
316324
}
317325

326+
@override
327+
ConstantReader read(String field) {
328+
final reader = peek(field);
329+
if (reader == null) {
330+
_assertHasField(_object?.type?.element, field);
331+
return const _NullConstant();
332+
}
333+
return reader;
334+
}
335+
318336
@override
319337
Revivable revive() => reviveInstance(_object);
320338

test/constants_test.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void main() {
9999

100100
final nested = constant.read('nested');
101101
expect(nested.isNull, isFalse, reason: '$nested');
102-
expect(nested.read('aString').isNull, isTrue);
102+
expect(nested.read('aString').isNull, isTrue, reason: '$nested');
103103
expect(nested.read('aInt').isNull, isTrue);
104104
expect(nested.read('aBool').isNull, isTrue);
105105
});
@@ -150,6 +150,12 @@ void main() {
150150
expect(() => $null.read('foo'), throwsUnsupportedError);
151151
});
152152

153+
test('should not fail reading from `null` when using peek', () {
154+
final $null = constants[3];
155+
expect($null.isNull, isTrue, reason: '${$null}');
156+
expect($null.peek('foo'), isNull);
157+
});
158+
153159
test('should fail reading a missing field', () {
154160
final $super = constants[5];
155161
expect(() => $super.read('foo'), throwsFormatException);

0 commit comments

Comments
 (0)