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

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

Issue 14578008: Add a DateTimeRule to default serialization (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 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
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of serialization; 5 part of serialization;
6 6
7 // TODO(alanknight): We should have an example and tests for subclassing 7 // TODO(alanknight): We should have an example and tests for subclassing
8 // serialization rule rather than using the hard-coded ClosureToMap rule. And 8 // serialization rule rather than using the hard-coded ClosureToMap rule. And
9 // possibly an abstract superclass that's designed to be subclassed that way. 9 // possibly an abstract superclass that's designed to be subclassed that way.
10 /** 10 /**
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 /** A hard-coded rule for serializing Symbols. */ 485 /** A hard-coded rule for serializing Symbols. */
486 class SymbolRule extends CustomRule { 486 class SymbolRule extends CustomRule {
487 bool appliesTo(instance, _) => instance is Symbol; 487 bool appliesTo(instance, _) => instance is Symbol;
488 getState(instance) => [MirrorSystem.getName(instance)]; 488 getState(instance) => [MirrorSystem.getName(instance)];
489 create(state) => new Symbol(state[0]); 489 create(state) => new Symbol(state[0]);
490 void setState(symbol, state) {} 490 void setState(symbol, state) {}
491 int get dataLength => 1; 491 int get dataLength => 1;
492 bool get hasVariableLengthEntries => false; 492 bool get hasVariableLengthEntries => false;
493 } 493 }
494 494
495 /** A hard-coded rule for DateTime. */
496 class DateTimeRule extends CustomRule {
497 /** A helper to make it easier to create dates either locally or UTC. */
498 static DateTime makeUtc(year, month, day, hour, minute, second, millisecond)
499 => new DateTime.utc(year, month, day, hour, minute, second, millisecond);
500 /** A helper to make it easier to create dates either locally or UTC. */
501 static DateTime makeLocal(year, month, day, hour, minute, second, millisecond)
502 => new DateTime(year, month, day, hour, minute, second, millisecond);
503
504 bool appliesTo(instance, _) => instance is DateTime;
505
506 List<int> getState(DateTime date) =>
507 [date.year, date.month, date.day, date.hour, date.minute,
Jennifer Messerly 2013/05/14 23:18:33 alternatively, just store millisecondsSinceEpoch a
Alan Knight 2013/05/14 23:32:55 Doh! That's much simpler. Done.
508 date.second, date.millisecond, date.isUtc];
509
510 create(List<int> state) {
511 var creation = state.last ? makeUtc : makeLocal;
512 return Function.apply(creation, state.take(7).toList());
Jennifer Messerly 2013/05/14 23:18:33 TIL: we have Function.apply :)
Alan Knight 2013/05/14 23:32:55 But now I don't get to use it :-(
513 }
514
515 void setState(date, state) {}
516 // Let the system know we don't have to store a length for these.
517 int get dataLength => 8;
518 bool get hasVariableLengthEntries => false;
519 }
520
495 /** Create a lazy list/map that will inflate its items on demand in [r]. */ 521 /** Create a lazy list/map that will inflate its items on demand in [r]. */
496 _lazy(l, Reader r) { 522 _lazy(l, Reader r) {
497 if (l is List) return new _LazyList(l, r); 523 if (l is List) return new _LazyList(l, r);
498 if (l is Map) return new _LazyMap(l, r); 524 if (l is Map) return new _LazyMap(l, r);
499 throw new SerializationException("Invalid type: must be Map or List - $l"); 525 throw new SerializationException("Invalid type: must be Map or List - $l");
500 } 526 }
501 527
502 /** 528 /**
503 * This provides an implementation of Map that wraps a list which may 529 * This provides an implementation of Map that wraps a list which may
504 * contain references to (potentially) non-inflated objects. If these 530 * contain references to (potentially) non-inflated objects. If these
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 operator [](x) => _reader.inflateReference(_raw[x]); 580 operator [](x) => _reader.inflateReference(_raw[x]);
555 581
556 int get length => _raw.length; 582 int get length => _raw.length;
557 bool get isEmpty => _raw.isEmpty; 583 bool get isEmpty => _raw.isEmpty;
558 get first => _reader.inflateReference(_raw.first); 584 get first => _reader.inflateReference(_raw.first);
559 get last => _reader.inflateReference(_raw.last); 585 get last => _reader.inflateReference(_raw.last);
560 586
561 // These operations, and other inherited methods that iterate over the whole 587 // These operations, and other inherited methods that iterate over the whole
562 // list will work, but may be expensive, and are probably 588 // list will work, but may be expensive, and are probably
563 // best avoided. 589 // best avoided.
564 List get _inflated => _raw.map(_reader.inflateReference); 590 Iterable get _inflated => _raw.map(_reader.inflateReference);
565 Iterator get iterator => _inflated.iterator; 591 Iterator get iterator => _inflated.iterator;
566 indexOf(x, [pos = 0]) => _inflated.toList().indexOf(x); 592 indexOf(x, [pos = 0]) => _inflated.toList().indexOf(x);
567 lastIndexOf(x, [pos]) => _inflated.toList().lastIndexOf(x); 593 lastIndexOf(x, [pos]) => _inflated.toList().lastIndexOf(x);
568 sublist(start, [end]) => _inflated.sublist(start, end); 594 sublist(start, [end]) => _inflated.sublist(start, end);
569 595
570 Map<int, dynamic> asMap() => _inflated.asMap(); 596 Map<int, dynamic> asMap() => _inflated.asMap();
571 597
572 // These operations are all invalid 598 // These operations are all invalid
573 _throw() { 599 _throw() {
574 throw new UnsupportedError("Not modifiable"); 600 throw new UnsupportedError("Not modifiable");
(...skipping 14 matching lines...) Expand all
589 removeWhere(x) => _throw(); 615 removeWhere(x) => _throw();
590 retainWhere(x) => _throw(); 616 retainWhere(x) => _throw();
591 replaceRange(x, y, z) => _throw(); 617 replaceRange(x, y, z) => _throw();
592 getRange(x, y) => _throw(); 618 getRange(x, y) => _throw();
593 setRange(x, y, z, [a = 0]) => _throw(); 619 setRange(x, y, z, [a = 0]) => _throw();
594 setAll(x, y) => _throw(); 620 setAll(x, y) => _throw();
595 removeRange(x, y) => _throw(); 621 removeRange(x, y) => _throw();
596 get reversed => _throw(); 622 get reversed => _throw();
597 void set length(x) => _throw(); 623 void set length(x) => _throw();
598 } 624 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698