Skip to content

Commit 0ff4589

Browse files
authored
Add ConstantValue.objectValue. (#244)
* Add ConstantValue.objectValue. * Fix.
1 parent 2657dfc commit 0ff4589

File tree

3 files changed

+60
-46
lines changed

3 files changed

+60
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Add `LibraryReader.allElements` - a utility to iterate across all `Element`
1818
instances contained in Dart library.
1919
* Add `LibraryReader.element` to get back to the `LibraryElement` instance.
20+
* Add `ConstantReader.objectValue` to get back to the `DartObject` instance.
2021
* Add `ConstantReader.peek` to read a value that returns `null` if not found:
2122

2223
```dart

lib/src/constants.dart

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -59,67 +59,70 @@ abstract class ConstantReader {
5959
/// is represented at least partially with [DartObject] instances.
6060
dynamic get anyValue;
6161

62-
/// Returns whether this constant represents a `bool` literal.
62+
/// Underlying object used to create this instance.
63+
DartObject get objectValue;
64+
65+
/// Whether this constant represents a `bool` literal.
6366
bool get isBool;
6467

65-
/// Returns this constant as a `bool` value.
68+
/// This constant as a `bool` value.
6669
bool get boolValue;
6770

68-
/// Returns whether this constant represents an `int` literal.
71+
/// This constant represents an `int` literal.
6972
bool get isInt;
7073

71-
/// Returns this constant as an `int` value.
74+
/// This constant as an `int` value.
7275
///
7376
/// Throws [FormatException] if [isInt] is `false`.
7477
int get intValue;
7578

76-
/// Returns whether this constant represents a `List` literal.
79+
/// Whether this constant represents a `List` literal.
7780
///
7881
/// If `true`, [listValue] will return a `List` (not throw).
7982
bool get isList;
8083

81-
/// Returns this constant as a `List` value.
84+
/// This constant as a `List` value.
8285
///
8386
/// Note: the list values are instances of [DartObject] which represent the
8487
/// original values.
8588
///
8689
/// Throws [FormatException] if [isList] is `false`.
8790
List<DartObject> get listValue;
8891

89-
/// Returns whether this constant represents a `Map` literal.
92+
/// Whether this constant represents a `Map` literal.
9093
///
9194
/// If `true`, [listValue] will return a `Map` (not throw).
9295
bool get isMap;
9396

94-
/// Returns this constant as a `Map` value.
97+
/// This constant as a `Map` value.
9598
///
9699
/// Note: the map keys and values are instances of [DartObject] which
97100
/// represent the original values.
98101
///
99102
/// Throws [FormatException] if [isMap] is `false`.
100103
Map<DartObject, DartObject> get mapValue;
101104

102-
/// Returns whether this constant represents a `String` literal.
105+
/// Whether this constant represents a `String` literal.
103106
///
104107
/// If `true`, [stringValue] will return a `String` (not throw).
105108
bool get isString;
106109

107-
/// Returns this constant as an `String` value.
110+
/// This constant as an `String` value.
108111
///
109112
/// Throws [FormatException] if [isString] is `false`.
110113
String get stringValue;
111114

112-
/// Returns whether this constant represents a `double` literal.
115+
/// Whether this constant represents a `double` literal.
113116
///
114117
/// If `true`, [doubleValue] will return a `double` (not throw).
115118
bool get isDouble;
116119

117-
/// Returns this constant as an `double` value.
120+
/// This constant as an `double` value.
118121
///
119122
/// Throws [FormatException] if [isDouble] is `false`.
120123
double get doubleValue;
121124

122-
/// Returns whether this constant represents a `Symbol` literal.
125+
/// Whether this constant represents a `Symbol` literal.
123126
///
124127
/// If `true`, [symbolValue] will return a `Symbol` (not throw).
125128
bool get isSymbol;
@@ -129,20 +132,20 @@ abstract class ConstantReader {
129132
/// Throws [FormatException] if [isSymbol] is `false`.
130133
Symbol get symbolValue;
131134

132-
/// Returns whether this constant represents a `Type` literal.
135+
/// Whether this constant represents a `Type` literal.
133136
///
134137
/// If `true`, [typeValue] will return a `DartType` (not throw).
135138
bool get isType;
136139

137-
/// Returns a [DartType] representing this as a `Type` value.
140+
/// A [DartType] representing this as a `Type` value.
138141
///
139142
/// Throws [FormatException] if [isType] is `false`.
140143
DartType get typeValue;
141144

142-
/// Returns whether this constant represents `null`.
145+
/// Whether this constant represents `null`.
143146
bool get isNull;
144147

145-
/// Returns whether this constant matches [checker].
148+
/// Whether this constant matches [checker].
146149
bool instanceOf(TypeChecker checker);
147150

148151
/// Reads [field] from the constant as another constant value.
@@ -172,6 +175,9 @@ class _NullConstant implements ConstantReader {
172175
@override
173176
dynamic get anyValue => null;
174177

178+
@override
179+
DartObject get objectValue => throw new UnsupportedError('Null');
180+
175181
@override
176182
bool get boolValue => _throw('bool');
177183

@@ -241,82 +247,83 @@ class _NullConstant implements ConstantReader {
241247

242248
/// Default implementation of [ConstantReader].
243249
class _Constant implements ConstantReader {
244-
final DartObject _object;
250+
const _Constant(this.objectValue);
245251

246-
const _Constant(this._object);
252+
@override
253+
final DartObject objectValue;
247254

248255
@override
249256
dynamic get anyValue =>
250-
_object.toBoolValue() ??
251-
_object.toIntValue() ??
252-
_object.toStringValue() ??
253-
_object.toDoubleValue() ??
257+
objectValue.toBoolValue() ??
258+
objectValue.toIntValue() ??
259+
objectValue.toStringValue() ??
260+
objectValue.toDoubleValue() ??
254261
(isSymbol ? this.symbolValue : null) ??
255262
_throw('bool, int, double, String or Symbol');
256263

257264
@override
258-
bool get boolValue => isBool ? _object.toBoolValue() : _throw('bool');
265+
bool get boolValue => isBool ? objectValue.toBoolValue() : _throw('bool');
259266

260267
@override
261-
int get intValue => isInt ? _object.toIntValue() : _throw('int');
268+
int get intValue => isInt ? objectValue.toIntValue() : _throw('int');
262269

263270
@override
264271
String get stringValue =>
265-
isString ? _object.toStringValue() : _throw('String');
272+
isString ? objectValue.toStringValue() : _throw('String');
266273

267274
@override
268275
List<DartObject> get listValue =>
269-
isList ? _object.toListValue() : _throw('List');
276+
isList ? objectValue.toListValue() : _throw('List');
270277

271278
@override
272279
Map<DartObject, DartObject> get mapValue =>
273-
isMap ? _object.toMapValue() : _throw('Map');
280+
isMap ? objectValue.toMapValue() : _throw('Map');
274281

275282
@override
276-
bool get isBool => _object.toBoolValue() != null;
283+
bool get isBool => objectValue.toBoolValue() != null;
277284

278285
@override
279-
bool get isInt => _object.toIntValue() != null;
286+
bool get isInt => objectValue.toIntValue() != null;
280287

281288
@override
282-
bool get isList => _object.toListValue() != null;
289+
bool get isList => objectValue.toListValue() != null;
283290

284291
@override
285-
bool get isNull => _isNull(_object);
292+
bool get isNull => _isNull(objectValue);
286293

287294
@override
288-
bool get isMap => _object.toMapValue() != null;
295+
bool get isMap => objectValue.toMapValue() != null;
289296

290297
@override
291-
bool get isString => _object.toStringValue() != null;
298+
bool get isString => objectValue.toStringValue() != null;
292299

293300
@override
294-
bool get isDouble => _object.toDoubleValue() != null;
301+
bool get isDouble => objectValue.toDoubleValue() != null;
295302

296303
@override
297304
double get doubleValue =>
298-
isDouble ? _object.toDoubleValue() : _throw('double');
305+
isDouble ? objectValue.toDoubleValue() : _throw('double');
299306

300307
@override
301-
bool get isSymbol => _object.toSymbolValue() != null;
308+
bool get isSymbol => objectValue.toSymbolValue() != null;
302309

303310
@override
304311
Symbol get symbolValue =>
305-
isSymbol ? new Symbol(_object.toSymbolValue()) : _throw('Symbol');
312+
isSymbol ? new Symbol(objectValue.toSymbolValue()) : _throw('Symbol');
306313

307314
@override
308-
bool get isType => _object.toTypeValue() != null;
315+
bool get isType => objectValue.toTypeValue() != null;
309316

310317
@override
311-
DartType get typeValue => isType ? _object.toTypeValue() : _throw("Type");
318+
DartType get typeValue => isType ? objectValue.toTypeValue() : _throw("Type");
312319

313320
@override
314321
bool instanceOf(TypeChecker checker) =>
315-
checker.isAssignableFromType(_object.type);
322+
checker.isAssignableFromType(objectValue.type);
316323

317324
@override
318325
ConstantReader peek(String field) {
319-
final constant = new ConstantReader(_getFieldRecursive(_object, field));
326+
final constant = new ConstantReader(_getFieldRecursive(objectValue, field));
320327
if (constant.isNull) {
321328
return null;
322329
}
@@ -327,18 +334,18 @@ class _Constant implements ConstantReader {
327334
ConstantReader read(String field) {
328335
final reader = peek(field);
329336
if (reader == null) {
330-
_assertHasField(_object?.type?.element, field);
337+
_assertHasField(objectValue?.type?.element, field);
331338
return const _NullConstant();
332339
}
333340
return reader;
334341
}
335342

336343
@override
337-
Revivable revive() => reviveInstance(_object);
344+
Revivable revive() => reviveInstance(objectValue);
338345

339346
@override
340-
String toString() => 'ConstantReader ${_object}';
347+
String toString() => 'ConstantReader ${objectValue}';
341348

342349
dynamic _throw(String expected) =>
343-
throw new FormatException('Not an instance of $expected.', _object);
350+
throw new FormatException('Not an instance of $expected.', objectValue);
344351
}

test/constants_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ void main() {
144144
expect(() => constants[11].anyValue, throwsFormatException);
145145
});
146146

147+
test('should give back the underlying value', () {
148+
final object = constants[11].objectValue;
149+
expect(object, isNotNull);
150+
expect(object.toTypeValue(), isNotNull);
151+
});
152+
147153
test('should fail reading from `null`', () {
148154
final $null = constants[3];
149155
expect($null.isNull, isTrue, reason: '${$null}');

0 commit comments

Comments
 (0)