Skip to content

Commit b96dc3b

Browse files
authored
Final tweaks (#186)
* Refine anyValue to only represent values that are identical to the input value * Prepare for 0.5.9 release
1 parent 41c9331 commit b96dc3b

File tree

4 files changed

+46
-44
lines changed

4 files changed

+46
-44
lines changed

lib/src/annotation.dart

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,13 @@ dynamic instantiateAnnotation(ElementAnnotation annotation) {
5151
dynamic _getValue(DartObject object) {
5252
var reader = new ConstantReader(object);
5353

54-
if (reader.isNull) {
55-
return null;
56-
}
57-
58-
if (reader.isBool) {
59-
return reader.boolValue;
60-
}
61-
62-
if (reader.isInt) {
63-
return reader.intValue;
64-
}
65-
66-
if (reader.isString) {
67-
return reader.stringValue;
68-
}
69-
70-
if (reader.isDouble) {
71-
return reader.doubleValue;
72-
}
73-
74-
if (reader.isSymbol) {
75-
return reader.symbolValue;
54+
if (reader.isNull ||
55+
reader.isBool ||
56+
reader.isInt ||
57+
reader.isString ||
58+
reader.isDouble ||
59+
reader.isSymbol) {
60+
return reader.anyValue;
7661
}
7762

7863
if (reader.isType) {

lib/src/constants.dart

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ abstract class ConstantReader {
5353
_isNull(object) ? const _NullConstant() : new _Constant(object);
5454

5555
/// Constant as any supported literal value.
56+
///
57+
/// Throws [FormatException] if a valid literal value cannot be returned. This
58+
/// is the case if the constant is not a literal or if the literal value
59+
/// is represented at least partially with [DartObject] instances.
5660
dynamic get anyValue;
5761

5862
/// Returns whether this constant represents a `bool` literal.
@@ -76,6 +80,9 @@ abstract class ConstantReader {
7680

7781
/// Returns this constant as a `List` value.
7882
///
83+
/// Note: the list values are instances of [DartObject] which represent the
84+
/// original values.
85+
///
7986
/// Throws [FormatException] if [isList] is `false`.
8087
List<DartObject> get listValue;
8188

@@ -86,6 +93,9 @@ abstract class ConstantReader {
8693

8794
/// Returns this constant as a `Map` value.
8895
///
96+
/// Note: the map keys and values are instances of [DartObject] which
97+
/// represent the original values.
98+
///
8999
/// Throws [FormatException] if [isMap] is `false`.
90100
Map<DartObject, DartObject> get mapValue;
91101

@@ -146,15 +156,12 @@ abstract class ConstantReader {
146156
Revivable revive();
147157
}
148158

149-
dynamic _throw(String expected, [dynamic object]) => throw new FormatException(
150-
'Not an instance of $expected.', object == null ? null : '$object');
151-
152159
/// Implements a [ConstantReader] representing a `null` value.
153160
class _NullConstant implements ConstantReader {
154161
const _NullConstant();
155162

156163
@override
157-
dynamic get anyValue => _throw('dynamic');
164+
dynamic get anyValue => null;
158165

159166
@override
160167
bool get boolValue => _throw('bool');
@@ -215,6 +222,9 @@ class _NullConstant implements ConstantReader {
215222

216223
@override
217224
Revivable revive() => throw new UnsupportedError('Null');
225+
226+
dynamic _throw(String expected) =>
227+
throw new FormatException('Not an instance of $expected.');
218228
}
219229

220230
/// Default implementation of [ConstantReader].
@@ -230,28 +240,25 @@ class _Constant implements ConstantReader {
230240
_object.toStringValue() ??
231241
_object.toDoubleValue() ??
232242
(isSymbol ? this.symbolValue : null) ??
233-
_object.toTypeValue() ??
234-
_object.toListValue() ??
235-
_object.toMapValue();
243+
_throw('bool, int, double, String or Symbol');
236244

237245
@override
238-
bool get boolValue =>
239-
isBool ? _object.toBoolValue() : _throw('bool', _object);
246+
bool get boolValue => isBool ? _object.toBoolValue() : _throw('bool');
240247

241248
@override
242-
int get intValue => isInt ? _object.toIntValue() : _throw('int', _object);
249+
int get intValue => isInt ? _object.toIntValue() : _throw('int');
243250

244251
@override
245252
String get stringValue =>
246-
isString ? _object.toStringValue() : _throw('String', _object);
253+
isString ? _object.toStringValue() : _throw('String');
247254

248255
@override
249256
List<DartObject> get listValue =>
250-
isList ? _object.toListValue() : _throw('List', _object);
257+
isList ? _object.toListValue() : _throw('List');
251258

252259
@override
253260
Map<DartObject, DartObject> get mapValue =>
254-
isMap ? _object.toMapValue() : _throw('Map', _object);
261+
isMap ? _object.toMapValue() : _throw('Map');
255262

256263
@override
257264
bool get isBool => _object.toBoolValue() != null;
@@ -276,22 +283,20 @@ class _Constant implements ConstantReader {
276283

277284
@override
278285
double get doubleValue =>
279-
isDouble ? _object.toDoubleValue() : _throw('double', _object);
286+
isDouble ? _object.toDoubleValue() : _throw('double');
280287

281288
@override
282289
bool get isSymbol => _object.toSymbolValue() != null;
283290

284291
@override
285-
Symbol get symbolValue => isSymbol
286-
? new Symbol(_object.toSymbolValue())
287-
: _throw('Symbol', _object);
292+
Symbol get symbolValue =>
293+
isSymbol ? new Symbol(_object.toSymbolValue()) : _throw('Symbol');
288294

289295
@override
290296
bool get isType => _object.toTypeValue() != null;
291297

292298
@override
293-
DartType get typeValue =>
294-
isType ? _object.toTypeValue() : _throw("Type", _object);
299+
DartType get typeValue => isType ? _object.toTypeValue() : _throw("Type");
295300

296301
@override
297302
bool instanceOf(TypeChecker checker) =>
@@ -311,4 +316,7 @@ class _Constant implements ConstantReader {
311316

312317
@override
313318
String toString() => 'ConstantReader ${_object}';
319+
320+
dynamic _throw(String expected) =>
321+
throw new FormatException('Not an instance of $expected.', _object);
314322
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 0.5.9-dev
2+
version: 0.5.9
33
author: Dart Team <[email protected]>
44
description: Automated source code generation for Dart.
55
homepage: https://github.com/dart-lang/source_gen

test/constants_test.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,31 @@ void main() {
6868
test('should read a String', () {
6969
expect(constants[0].isString, isTrue);
7070
expect(constants[0].stringValue, 'Hello');
71+
expect(constants[0].anyValue, 'Hello');
7172
});
7273

7374
test('should read an Int', () {
7475
expect(constants[1].isInt, isTrue);
7576
expect(constants[1].intValue, 1234);
77+
expect(constants[1].anyValue, 1234);
7678
});
7779

7880
test('should read a Bool', () {
7981
expect(constants[2].isBool, isTrue);
80-
expect(constants[2].boolValue, true);
82+
expect(constants[2].boolValue, isTrue);
83+
expect(constants[2].anyValue, isTrue);
8184
});
8285

8386
test('should read a Null', () {
8487
expect(constants[3].isNull, isTrue);
88+
expect(constants[3].anyValue, isNull);
8589
});
8690

8791
test('should read an arbitrary object', () {
8892
final constant = constants[4];
93+
94+
expect(() => constant.anyValue, throwsFormatException);
95+
8996
expect(constant.read('aString').stringValue, 'Hello');
9097
expect(constant.read('aInt').intValue, 1234);
9198
expect(constant.read('aBool').boolValue, true);
@@ -106,6 +113,7 @@ void main() {
106113
expect(constants[6].isList, isTrue, reason: '${constants[6]}');
107114
expect(constants[6].listValue.map((c) => new ConstantReader(c).intValue),
108115
[1, 2, 3]);
116+
expect(() => constants[6].anyValue, throwsFormatException);
109117
});
110118

111119
test('should read a map', () {
@@ -115,6 +123,7 @@ void main() {
115123
key: (k, _) => new ConstantReader(k).intValue,
116124
value: (_, v) => new ConstantReader(v).stringValue),
117125
{1: 'A', 2: 'B'});
126+
expect(() => constants[7].anyValue, throwsFormatException);
118127
});
119128

120129
test('should read a double', () {
@@ -132,7 +141,7 @@ void main() {
132141
test('should read a Type', () {
133142
expect(constants[11].isType, isTrue);
134143
expect(constants[11].typeValue.name, 'DateTime');
135-
expect(constants[11].anyValue.toString(), 'DateTime');
144+
expect(() => constants[11].anyValue, throwsFormatException);
136145
});
137146

138147
test('should fail reading from `null`', () {

0 commit comments

Comments
 (0)