Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: pkg/serialization/lib/src/basic_rule.dart

Issue 584473004: Revert "remove serialization. it's moved to github" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/serialization/lib/serialization.dart ('k') | pkg/serialization/lib/src/format.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of serialization;
6
7 // TODO(alanknight): Figure out how to reasonably separate out the things
8 // that require reflection without making the API more awkward. Or if that is
9 // in fact necessary. Maybe the tree-shaking will just remove it if unused.
10
11 /**
12 * This is the basic rule for handling "normal" objects, which have a list of
13 * fields and a constructor, as opposed to simple types or collections. It uses
14 * mirrors to access the state, and can also use them to figure out the list
15 * of fields and the constructor if it's not provided.
16 *
17 * If you call [Serialization.addRule], this is what you get.
18 *
19 */
20 class BasicRule extends SerializationRule {
21 /**
22 * The [type] is used both to find fields and to verify if the object is one
23 * that we handle.
24 */
25 final ClassMirror type;
26
27 /** Used to create new objects when reading. */
28 final Constructor constructor;
29
30 /** This holds onto our list of fields, and can also calculate them. */
31 final _FieldList _fields;
32
33 /**
34 * Instances can either use maps or lists to hold the object's state. The list
35 * representation is much more compact and used by default. The map
36 * representation is more human-readable. The default is to use lists.
37 */
38 bool useMaps = false;
39
40 // TODO(alanknight) Change the type parameter once we have class literals.
41 // Issue 6282.
42 // TODO(alanknight) Does the comment for this format properly?
43 /**
44 * Create this rule. Right now the user is obliged to pass a ClassMirror,
45 * but once we allow class literals (Issue 6282) it will support that. The
46 * other parameters can all be left as null, and are optional on the
47 * [Serialization.addRule] method which is the normal caller for this.
48 * [constructorName] is the constructor, if not the default.
49 * [constructorFields] are the fields required to call the constructor, which
50 * is the essential state. They don't have to be actual fields,
51 * getter/setter pairs or getter/constructor pairs are fine. Note that
52 * the constructorFields do not need to be strings, they can be arbitrary
53 * values. For non-strings, these will be treated as constant values to be
54 * used instead of data read from the objects.
55 * [regularFields] are the non-essential fields. They don't have to be actual
56 * fields, getter/setter pairs are fine. If this is null, it's assumed
57 * that we should figure them out.
58 * [excludeFields] lets you tell it to find the fields automatically, but
59 * omit some that would otherwise be included.
60 */
61 factory BasicRule(ClassMirror type, String constructorName,
62 List constructorFields, List regularFields, List excludeFields) {
63
64 var fields = new _FieldList(type);
65 fields.constructorFields = constructorFields;
66 fields.regular = regularFields;
67 // TODO(alanknight): The order of this matters. It shouldn't.
68 fields.exclude = excludeFields;
69 fields.figureOutFields();
70
71 var constructor = new Constructor(type, constructorName,
72 fields.constructorFieldIndices());
73
74 return new BasicRule._(type, constructor, fields);
75 }
76
77 BasicRule._(this.type, this.constructor, this._fields) {
78 configureForLists();
79 }
80
81 /**
82 * Sometimes it's necessary to treat fields of an object differently, based
83 * on the containing object. For example, by default a list treats its
84 * contents as non-essential state, so it will be populated only after all
85 * objects have been created. An object may have a list which is used in its
86 * constructor and must be fully created before the owning object can be
87 * created. Alternatively, it may not be possible to set a field directly,
88 * and some other method must be called to set it, perhaps calling a method
89 * on the owning object to add each individual element.
90 *
91 * This method lets you designate a function to use to set the value of a
92 * field. It also makes the contents of that field be treated as essential,
93 * which currently only has meaning if the field is a list. This is done
94 * because you might set a list field's special treatment function to add
95 * each item individually and that will only work if those objects already
96 * exist.
97 *
98 * For example, to serialize a Serialization, we need its rules to be
99 * individually added rather than just setting the rules field.
100 * ..addRuleFor(Serialization).setFieldWith('rules',
101 * (InstanceMirror s, List rules) {
102 * rules.forEach((x) => s.reflectee.addRule(x));
103 * Note that the function is passed the owning object as well as the field
104 * value, but that it is passed as a mirror.
105 */
106 void setFieldWith(String fieldName, SetWithFunction setWith) {
107 _fields.addAllByName([fieldName]);
108 _NamedField field = _fields.named(_asSymbol(fieldName));
109 Function setter = (setWith == null) ? field.defaultSetter : setWith;
110 field.customSetter = setter;
111 }
112
113 /** Return the name of the constructor used to create new instances on read.*/
114 String get constructorName => constructor.name;
115
116 /** Return the list of field names to be passed to the constructor.*/
117 List<String> get constructorFields => _fields.constructorFieldNames();
118
119 /** Return the list of field names not used in the constructor. */
120 List<String> get regularFields => _fields.regularFieldNames();
121
122 String toString() => "Basic Rule for ${type.simpleName}";
123
124 /**
125 * Configure this instance to use maps by field name as its output.
126 * Instances can either produce maps or lists. The list representation
127 * is much more compact and used by default. The map representation is
128 * much easier to debug. The default is to use lists.
129 */
130 void configureForMaps() {
131 useMaps = true;
132 }
133
134 /**
135 * Configure this instance to use lists accessing fields by index as its
136 * output. Instances can either produce maps or lists. The list representation
137 * is much more compact and used by default. The map representation is
138 * much easier to debug. The default is to use lists.
139 */
140 void configureForLists() {
141 useMaps = false;
142 }
143
144 /**
145 * Create either a list or a map to hold the object's state, depending
146 * on the [useMaps] variable. If using a Map, we wrap it in order to keep
147 * the protocol compatible. See [configureForLists]/[configureForMaps].
148 *
149 * If a list is returned, it is growable.
150 */
151 createStateHolder() {
152 if (useMaps) return new _MapWrapper(_fields.contents);
153 List list = [];
154 list.length = _fields.length;
155 return list;
156 }
157
158 /**
159 * Wrap the state if it's passed in as a map, and if the keys are references,
160 * resolve them to the strings we expect. We leave the previous keys in there
161 * as well, as they shouldn't be harmful, and it costs more to remove them.
162 */
163 makeIndexableByNumber(state) {
164 if (!(state is Map)) return state;
165 // TODO(alanknight): This is quite inefficient, and we do it twice per
166 // instance. If the keys are references, we need to turn them into strings
167 // before we can look at indexing them by field position. It's also eager,
168 // but we know our keys are always primitives, so we don't have to worry
169 // about their instances not having been created yet.
170 var newState = new Map();
171 for (var each in state.keys) {
172 var newKey = (each is Reference) ? each.inflated() : each;
173 newState[newKey] = state[each];
174 }
175 return new _MapWrapper.fromMap(newState, _fields.contents);
176 }
177
178 /**
179 * Extract the state from [object] using an instanceMirror and the field
180 * names in [_fields]. Call the function [callback] on each value.
181 */
182 extractState(object, Function callback, Writer w) {
183 var result = createStateHolder();
184 var mirror = reflect(object);
185
186 keysAndValues(_fields).forEach(
187 (index, field) {
188 var value = _value(mirror, field);
189 callback(field.name);
190 callback(checkForEssentialLists(index, value));
191 result[index] = value;
192 });
193 return _unwrap(result);
194 }
195
196 flatten(state, Writer writer) {
197 if (state is List) {
198 keysAndValues(state).forEach((index, value) {
199 state[index] = writer._referenceFor(value);
200 });
201 } else {
202 var newState = new Map();
203 keysAndValues(state).forEach((key, value) {
204 newState[writer._referenceFor(key)] = writer._referenceFor(value);
205 });
206 return newState;
207 }
208 }
209
210 /**
211 * If the value is a List, and the field is a constructor field or
212 * otherwise specially designated, we wrap it in something that indicates
213 * a restriction on the rules that can be used. Which in this case amounts
214 * to designating the rule, since we so far only have one rule per object.
215 */
216 checkForEssentialLists(index, value) {
217 if (value is List && _fields.contents[index].isEssential) {
218 return new DesignatedRuleForObject(value,
219 (SerializationRule rule) => rule is ListRuleEssential);
220 } else {
221 return value;
222 }
223 }
224
225 /** Remove any MapWrapper from the extracted state. */
226 _unwrap(result) => (result is _MapWrapper) ? result.asMap() : result;
227
228 /**
229 * Call the designated constructor with the appropriate fields from [state],
230 * first resolving references in the context of [reader].
231 */
232 inflateEssential(state, Reader reader) {
233 InstanceMirror mirror = constructor.constructFrom(
234 makeIndexableByNumber(state), reader);
235 return mirror.reflectee;
236 }
237
238 /** For all [rawState] not required in the constructor, set it in the
239 * [object], resolving references in the context of [reader].
240 */
241 inflateNonEssential(rawState, object, Reader reader) {
242 InstanceMirror mirror = reflect(object);
243 var state = makeIndexableByNumber(rawState);
244 _fields.forEachRegularField( (_Field field) {
245 var value = reader.inflateReference(state[field.index]);
246 field.setValue(mirror, value);
247 });
248 }
249
250 /**
251 * Determine if this rule applies to the object in question. In our case
252 * this is true if the type mirrors are the same.
253 */
254 // TODO(alanknight): This seems likely to be slow. Verify. Other options?
255 bool appliesTo(object, Writer w) => reflect(object).type == type;
256
257 bool get hasVariableLengthEntries => false;
258
259 int get dataLength => _fields.length;
260
261 /**
262 * Extract the value of [field] from the object reflected
263 * by [mirror].
264 */
265 // TODO(alanknight): The framework should be resilient if there are fields
266 // it expects that are missing, either for the case of de-serializing to a
267 // different definition, or for the case that tree-shaking has removed state.
268 // TODO(alanknight): This, and other places, rely on synchronous access to
269 // mirrors. Should be changed to use a synchronous API once one is available,
270 // or to be async, but that would be extremely ugly.
271 _value(InstanceMirror mirror, _Field field) => field.valueIn(mirror);
272 }
273
274 /**
275 * This represents a field in an object. It is intended to be used as part of
276 * a [_FieldList].
277 */
278 abstract class _Field implements Comparable<_Field> {
279
280 /** The FieldList that contains us. */
281 final _FieldList fieldList;
282
283 /**
284 * Our position in the [fieldList._contents] collection. This is used
285 * to index into the state, so it's extremely important.
286 */
287 int index;
288
289 /** Is this field used in the constructor? */
290 bool usedInConstructor = false;
291
292 /**
293 * Create a new [_Field] instance. This will be either a [_NamedField] or a
294 * [_ConstantField] depending on whether or not [value] corresponds to a
295 * field in the class which [fieldList] models.
296 */
297 factory _Field(value, _FieldList fieldList) {
298 if (_isReallyAField(value, fieldList)) {
299 return new _NamedField._internal(value, fieldList);
300 } else {
301 return new _ConstantField._internal(value, fieldList);
302 }
303 }
304
305 /**
306 * Determine if [value] represents a field or getter in the class that
307 * [fieldList] models.
308 */
309 static bool _isReallyAField(value, _FieldList fieldList) {
310 var symbol = _asSymbol(value);
311 return hasField(symbol, fieldList.mirror) ||
312 hasGetter(symbol, fieldList.mirror);
313 }
314
315 /** Private constructor. */
316 _Field._internal(this.fieldList);
317
318 /**
319 * Extracts the value for the field that this represents from the instance
320 * mirrored by [mirror] and return it.
321 */
322 valueIn(InstanceMirror mirror);
323
324 // TODO(alanknight): Is this the right name, or is it confusing that essential
325 // is not the inverse of regular.
326 /** Return true if this is field is not used in the constructor. */
327 bool get isRegular => !usedInConstructor;
328
329 /**
330 * Return true if this field is treated as essential state, either because
331 * it is used in the constructor, or because it's been designated
332 * using [BasicRule.setFieldWith].
333 */
334 bool get isEssential => usedInConstructor;
335
336 /** Set the [value] of our field in the given mirrored [object]. */
337 void setValue(InstanceMirror object, value);
338
339 // Because [x] may not be a named field, we compare the toString. We don't
340 // care that much where constants come in the sort order as long as it's
341 // consistent.
342 int compareTo(_Field x) => toString().compareTo(x.toString());
343 }
344
345 /**
346 * This represents a field in the object, either stored as a field or
347 * accessed via getter/setter/constructor parameter. It has a name and
348 * will attempt to access the state for that name using an [InstanceMirror].
349 */
350 class _NamedField extends _Field {
351 /** The name of the field (or getter) */
352 String _name;
353 Symbol nameSymbol;
354
355 /**
356 * If this is set, then it is used as a way to set the value rather than
357 * using the default mechanism.
358 */
359 Function customSetter;
360
361 _NamedField._internal(fieldName, fieldList) : super._internal(fieldList) {
362 nameSymbol = _asSymbol(fieldName);
363 if (nameSymbol == null) {
364 throw new SerializationException("Invalid field name $fieldName");
365 }
366 }
367
368 String get name =>
369 _name == null ? _name = MirrorSystem.getName(nameSymbol) : _name;
370
371 operator ==(x) => x is _NamedField && (nameSymbol == x.nameSymbol);
372 int get hashCode => name.hashCode;
373
374 /**
375 * Return true if this field is treated as essential state, either because
376 * it is used in the constructor, or because it's been designated
377 * using [BasicRule.setFieldWith].
378 */
379 bool get isEssential => super.isEssential || customSetter != null;
380
381 /** Set the [value] of our field in the given mirrored [object]. */
382 void setValue(InstanceMirror object, value) {
383 setter(object, value);
384 }
385
386 valueIn(InstanceMirror mirror) => mirror.getField(nameSymbol).reflectee;
387
388 /** Return the function to use to set our value. */
389 Function get setter =>
390 (customSetter != null) ? customSetter : defaultSetter;
391
392 /** The default setter function. */
393 void defaultSetter(InstanceMirror object, value) {
394 object.setField(nameSymbol, value);
395 }
396
397 String toString() => 'Field($name)';
398 }
399
400 /**
401 * This represents a constant value that will be passed as a constructor
402 * parameter. Rather than having a name it has a constant value.
403 */
404 class _ConstantField extends _Field {
405
406 /** The value we always return.*/
407 final value;
408
409 _ConstantField._internal(this.value, fieldList) : super._internal(fieldList);
410
411 operator ==(x) => x is _ConstantField && (value == x.value);
412 int get hashCode => value.hashCode;
413 String toString() => 'ConstantField($value)';
414 valueIn(InstanceMirror mirror) => value;
415
416 /** We cannot be set, so setValue is a no-op. */
417 void setValue(InstanceMirror object, value) {}
418
419 /** There are places where the code expects us to have an identifier, so
420 * use the value for that.
421 */
422 get name => value;
423 }
424
425 /**
426 * The organization of fields in an object can be reasonably complex, so they
427 * are kept in a separate object, which also has the ability to compute the
428 * default fields to use reflectively.
429 */
430 class _FieldList extends IterableBase<_Field> {
431 /**
432 * All of our fields, indexed by name. Note that the names are
433 * typically Symbols, but can also be arbitrary constants.
434 */
435 Map<dynamic, _Field> allFields = new Map<dynamic, _Field>();
436
437 /**
438 * The fields which are used in the constructor. The fields themselves also
439 * know if they are constructor fields or not, but we need to keep this
440 * information here because the order matters.
441 */
442 List _constructorFields = const [];
443
444 /** The list of fields to exclude if we are computing the list ourselves. */
445 List<Symbol> _excludedFieldNames = const [];
446
447 /** The mirror we will use to compute the fields. */
448 final ClassMirror mirror;
449
450 /** Cached, sorted list of fields. */
451 List<_Field> _contents;
452
453 /** Should we compute the fields or just use whatever we were given. */
454 bool _shouldFigureOutFields = true;
455
456 _FieldList(this.mirror);
457
458 /** Look up a field by [name]. */
459 _Field named(name) => allFields[name];
460
461 /** Set the fields to be used in the constructor. */
462 set constructorFields(List fieldNames) {
463 if (fieldNames == null || fieldNames.isEmpty) return;
464 _constructorFields = [];
465 for (var each in fieldNames) {
466 var symbol = _asSymbol(each);
467 var name = _Field._isReallyAField(symbol, this) ? symbol : each;
468 var field = new _Field(name, this)..usedInConstructor = true;
469 allFields[name] = field;
470 _constructorFields.add(field);
471 }
472 invalidate();
473 }
474
475 /** Set the fields that aren't used in the constructor. */
476 set regular(List<String> fields) {
477 if (fields == null) return;
478 _shouldFigureOutFields = false;
479 addAllByName(fields);
480 }
481
482 /** Set the fields to be excluded. This is mutually exclusive with setting
483 * the regular fields.
484 */
485 set exclude(List<String> fields) {
486 // TODO(alanknight): This isn't well tested.
487 if (fields == null || fields.isEmpty) return;
488 if (allFields.length > _constructorFields.length) {
489 throw "You can't specify both excludeFields and regular fields";
490 }
491 _excludedFieldNames = fields.map((x) => new Symbol(x)).toList();
492 }
493
494 int get length => allFields.length;
495
496 /** Add all the fields which aren't on the exclude list. */
497 void addAllNotExplicitlyExcluded(Iterable<String> aCollection) {
498 if (aCollection == null) return;
499 var names = aCollection;
500 names = names.where((x) => !_excludedFieldNames.contains(x));
501 addAllByName(names);
502 }
503
504 /** Add all the fields with the given names without any special properties. */
505 void addAllByName(Iterable<String> names) {
506 for (var each in names) {
507 var symbol = _asSymbol(each);
508 var field = new _Field(symbol, this);
509 allFields.putIfAbsent(symbol, () => new _Field(symbol, this));
510 }
511 invalidate();
512 }
513
514 /**
515 * Fields have been added. In case we had already forced calculation of the
516 * list of contents, re-set it.
517 */
518 void invalidate() {
519 _contents = null;
520 contents;
521 }
522
523 Iterator<_Field> get iterator => contents.iterator;
524
525 /** Return a cached, sorted list of all the fields. */
526 List<_Field> get contents {
527 if (_contents == null) {
528 _contents = allFields.values.toList()..sort();
529 for (var i = 0; i < _contents.length; i++)
530 _contents[i].index = i;
531 }
532 return _contents;
533 }
534
535 /** Iterate over the regular fields, i.e. those not used in the constructor.*/
536 void forEachRegularField(Function f) {
537 for (var each in contents) {
538 if (each.isRegular) {
539 f(each);
540 }
541 }
542 }
543
544 /** Iterate over the fields used in the constructor. */
545 void forEachConstructorField(Function f) {
546 for (var each in contents) {
547 if (each.usedInConstructor) {
548 f(each);
549 }
550 }
551 }
552
553 List get constructorFields => _constructorFields;
554 List constructorFieldNames() =>
555 constructorFields.map((x) => x.name).toList();
556 List constructorFieldIndices() =>
557 constructorFields.map((x) => x.index).toList();
558 List regularFields() => contents.where((x) => !x.usedInConstructor).toList();
559 List regularFieldNames() => regularFields().map((x) => x.name).toList();
560 List regularFieldIndices() =>
561 regularFields().map((x) => x.index).toList();
562
563 /**
564 * If we weren't given any non-constructor fields to use, figure out what
565 * we think they ought to be, based on the class definition.
566 * We find public fields, getters that have corresponding setters, and getters
567 * that are listed in the constructor fields.
568 */
569 void figureOutFields() {
570 Iterable names(Iterable<DeclarationMirror> mirrors) =>
571 mirrors.map((each) => each.simpleName).toList();
572
573 if (!_shouldFigureOutFields || !regularFields().isEmpty) return;
574 var fields = publicFields(mirror);
575 var getters = publicGetters(mirror);
576 var gettersWithSetters = getters.where( (each)
577 => mirror.declarations[
578 new Symbol("${MirrorSystem.getName(each.simpleName)}=")] != null);
579 var gettersThatMatchConstructor = getters.where((each)
580 => (named(each.simpleName) != null) &&
581 (named(each.simpleName).usedInConstructor)).toList();
582 addAllNotExplicitlyExcluded(names(fields));
583 addAllNotExplicitlyExcluded(names(gettersWithSetters));
584 addAllNotExplicitlyExcluded(names(gettersThatMatchConstructor));
585 }
586
587 toString() => "FieldList($contents)";
588 }
589
590 /**
591 * Provide a typedef for the setWith argument to setFieldWith. It would
592 * be nice if we could put this closer to the definition.
593 */
594 typedef SetWithFunction(InstanceMirror m, object);
595
596 /**
597 * This represents a constructor that is to be used when re-creating a
598 * serialized object.
599 */
600 class Constructor {
601 /** The mirror of the class we construct. */
602 final ClassMirror type;
603
604 /** The name of the constructor to use, if not the default constructor.*/
605 String name;
606 Symbol nameSymbol;
607
608 /**
609 * The indices of the fields used as constructor arguments. We will look
610 * these up in the state by number. The index is according to a list of the
611 * fields in alphabetical order by name.
612 */
613 List<int> fieldNumbers;
614
615 /**
616 * Creates a new constructor for the [type] with the constructor named [name]
617 * and the [fieldNumbers] of the constructor fields.
618 */
619 Constructor(this.type, this.name, this.fieldNumbers) {
620 if (name == null) name = '';
621 nameSymbol = new Symbol(name);
622 if (fieldNumbers == null) fieldNumbers = const [];
623 }
624
625 /**
626 * Find the field values in [state] and pass them to the constructor.
627 * If any of [fieldNumbers] is not an int, then use it as a literal value.
628 */
629 constructFrom(state, Reader r) {
630 // TODO(alanknight): Handle named parameters
631 Iterable inflated = fieldNumbers.map(
632 (x) => (x is int) ? r.inflateReference(state[x]) : x);
633 return type.newInstance(nameSymbol, inflated.toList());
634 }
635 }
636
637 /**
638 * This wraps a map to make it indexable by integer field numbers. It translates
639 * from the index into a field name and then looks it up in the map.
640 */
641 class _MapWrapper {
642 final Map _map;
643 final List fieldList;
644 _MapWrapper(this.fieldList) : _map = new Map();
645 _MapWrapper.fromMap(this._map, this.fieldList);
646
647 operator [](key) => _map[fieldList[key].name];
648
649 operator []=(key, value) { _map[fieldList[key].name] = value; }
650 get length => _map.length;
651
652 Map asMap() => _map;
653 }
654
655 /**
656 * Return a symbol corresponding to [value], which may be a String or a
657 * Symbol. If it is any other type, or if the string is an
658 * invalid symbol, return null;
659 */
660 Symbol _asSymbol(value) {
661 if (value is Symbol) return value;
662 if (value is String) {
663 try {
664 return new Symbol(value);
665 } on ArgumentError {
666 return null;
667 };
668 } else {
669 return null;
670 }
671 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/serialization.dart ('k') | pkg/serialization/lib/src/format.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698