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

Side by Side Diff: pkg/serialization/lib/src/serialization_helpers.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
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 /**
6 * This contains extra functions and classes useful for implementing
7 * serialiation. Some or all of these will be removed once the functionality is
8 * available in the core library.
9 */
10 library serialization_helpers;
11
12 import 'dart:collection';
13
14 /**
15 * A named function of one argument that just returns it. Useful for using
16 * as a default value for a function parameter or other places where you want
17 * to concisely provide a function that just returns its argument.
18 */
19 doNothing(x) => x;
20
21 /** Concatenate two lists. Handle the case where one or both might be null. */
22 // TODO(alanknight): Remove once issue 5342 is resolved.
23 Iterable append(Iterable a, Iterable b) {
24 if (a == null) {
25 return (b == null) ? [] : new List.from(b);
26 }
27 if (b == null) return new List.from(a);
28 var result = new List.from(a);
29 result.addAll(b);
30 return result;
31 }
32
33 /** Helper function for PrimitiveRule to tell which objects it applies to. */
34 bool isPrimitive(object) {
35 return identical(object, null) || object is num || object is String ||
36 identical(object, true) || identical(object, false);
37 }
38
39 /**
40 * Given either an Iterable or a Map, return a map. For a Map just return it.
41 * For an iterable, return a Map from the index to the value at that index.
42 *
43 * Used to iterate polymorphically between List-like and Map-like things.
44 * For example, keysAndValues(["a", "b", "c"]).forEach((key, value) => ...);
45 * will loop over the key/value pairs 1/"a", 2/"b", 3/"c", as if the argument
46 * was a Map from integer keys to string values.
47 */
48 Map keysAndValues(x) {
49 if (x is Map) return x;
50 if (x is List) return x.asMap();
51 if (x is Iterable) return x.toList().asMap();
52 throw new ArgumentError("Invalid argument, expected Map or Iterable, got $x");
53 }
54
55 /**
56 * Lets you iterate polymorphically between
57 * List-like and Map-like things, but making them behave like Lists, instead
58 * of behaving like Maps.
59 * So values(["a", "b", "c"]).forEach((value) => ...);
60 * will loop over the values "a", "b", "c", as if it were a List of values.
61 * Only supports forEach() and map() operations because that was all I needed
62 * for the moment.
63 */
64 Iterable values(x) {
65 if (x is Iterable) return x;
66 if (x is Map) return x.values;
67 throw new ArgumentError("Invalid argument, expected Map or Iterable, got $x");
68 }
69
70 /**
71 * Iterate over [collection] and return a new collection of the same type
72 * where each value has been transformed by [f]. For iterables and sets, this
73 * is equivalent to [map]. For a Map, it returns a new Map with the same keys
74 * and the corresponding values transformed by [f].
75 */
76 mapValues(collection, Function f) {
77 if (collection is Set) return collection.map(f).toSet();
78 if (collection is Iterable) return collection.map(f).toList();
79 if (collection is Map) return new Map.fromIterables(collection.keys,
80 collection.values.map(f));
81 throw new ArgumentError("Invalid argument, expected Map or Iterable, "
82 "got $collection");
83 }
84
85 /**
86 * This acts as a stand-in for some value that cannot be hashed. We can't
87 * just use const Object() because the compiler will fold them together.
88 */
89 class _Sentinel {
90 final _wrappedObject;
91 const _Sentinel(this._wrappedObject);
92 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/src/reader_writer.dart ('k') | pkg/serialization/lib/src/serialization_rule.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698