Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, 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 library yaml.yaml_node_wrapper; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 | |
| 9 import 'package:collection/collection.dart' as pc; | |
|
Bob Nystrom
2014/06/21 00:14:15
Why "pc"? How about "collection"?
nweiz
2014/06/23 22:23:07
"pc" is for "package collection". Just calling it
Bob Nystrom
2014/06/23 23:11:44
I didn't infer that abbreviation at all. The other
nweiz
2014/06/24 00:03:09
The point of using a prefix is to make it clear wh
| |
| 10 import 'package:source_maps/source_maps.dart'; | |
| 11 | |
| 12 import 'dummy_span.dart'; | |
| 13 import 'yaml_node.dart'; | |
| 14 | |
| 15 /// A wrapper that makes a normal Dart map behave like a [YamlMap]. | |
| 16 class YamlMapWrapper extends MapBase | |
| 17 with pc.UnmodifiableMapMixin<dynamic, YamlNode> | |
| 18 implements YamlMap { | |
| 19 final Map _dartMap; | |
| 20 | |
| 21 final Span span; | |
| 22 | |
| 23 final Map<dynamic, YamlNode> nodes; | |
| 24 | |
| 25 Map get value => this; | |
| 26 | |
| 27 Iterable get keys => _dartMap.keys; | |
| 28 | |
| 29 YamlMapWrapper(Map dartMap, String sourceName) | |
| 30 : this._(dartMap, new DummySpan(sourceName)); | |
| 31 | |
| 32 YamlMapWrapper._(Map dartMap, Span span) | |
| 33 : _dartMap = dartMap, | |
| 34 span = span, | |
| 35 nodes = new _YamlMapNodes(dartMap, span); | |
| 36 | |
| 37 operator [](Object key) { | |
| 38 var value = _dartMap[key]; | |
| 39 if (value is Map) return new YamlMapWrapper._(value, span); | |
| 40 if (value is List) return new YamlListWrapper._(value, span); | |
|
Bob Nystrom
2014/06/21 00:14:15
Since the wrapper classes don't implement equality
nweiz
2014/06/23 22:23:07
I like implementing equality in terms of the wrapp
| |
| 41 return value; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /// The implementation of [YamlMapWrapper.nodes] as a wrapper around the Dart | |
| 46 /// map. | |
| 47 class _YamlMapNodes extends MapBase<dynamic, YamlNode> | |
| 48 with pc.UnmodifiableMapMixin<dynamic, YamlNode> { | |
| 49 final Map _dartMap; | |
| 50 | |
| 51 final Span _span; | |
| 52 | |
| 53 Iterable get keys => | |
| 54 _dartMap.keys.map((key) => new YamlScalar.internal(key, _span)); | |
| 55 | |
| 56 _YamlMapNodes(this._dartMap, this._span); | |
| 57 | |
| 58 YamlNode operator [](Object key) { | |
| 59 if (key is YamlScalar) key = key.value; | |
| 60 if (!_dartMap.containsKey(key)) return null; | |
| 61 var value = _dartMap[key]; | |
| 62 if (value is Map) return new YamlMapWrapper._(value, _span); | |
| 63 if (value is List) return new YamlListWrapper._(value, _span); | |
|
Bob Nystrom
2014/06/21 00:14:15
Encapsulate this little blob of logic in a helper
nweiz
2014/06/23 22:23:07
Done.
| |
| 64 return new YamlScalar.internal(value, _span); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. | |
| 69 /// A wrapper that makes a normal Dart list behave like a [YamlList]. | |
| 70 class YamlListWrapper extends ListBase implements YamlList { | |
| 71 final List _dartList; | |
| 72 | |
| 73 final Span span; | |
| 74 | |
| 75 final List<YamlNode> nodes; | |
| 76 | |
| 77 List get value => this; | |
| 78 | |
| 79 int get length => _dartList.length; | |
| 80 | |
| 81 set length(int index) { | |
| 82 throw new UnsupportedError("Cannot modify an unmodifiable List"); | |
|
Bob Nystrom
2014/06/21 00:14:15
"."
nweiz
2014/06/23 22:23:07
Done, although this now doesn't match the "dart:"
Bob Nystrom
2014/06/23 23:11:44
Ugh. Well, these are better. :)
| |
| 83 } | |
| 84 | |
| 85 YamlListWrapper(List dartList, String sourceName) | |
| 86 : this._(dartList, new DummySpan(sourceName)); | |
| 87 | |
| 88 YamlListWrapper._(List dartList, Span span) | |
| 89 : _dartList = dartList, | |
| 90 span = span, | |
| 91 nodes = new _YamlListNodes(dartList, span); | |
| 92 | |
| 93 operator [](int index) { | |
| 94 var value = _dartList[index]; | |
| 95 if (value is Map) return new YamlMapWrapper._(value, span); | |
| 96 if (value is List) return new YamlListWrapper._(value, span); | |
| 97 return value; | |
| 98 } | |
| 99 | |
| 100 operator []=(int index, value) { | |
| 101 throw new UnsupportedError("Cannot modify an unmodifiable List"); | |
|
Bob Nystrom
2014/06/21 00:14:15
"."
nweiz
2014/06/23 22:23:07
Done.
| |
| 102 } | |
| 103 } | |
| 104 | |
| 105 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. | |
| 106 /// The implementation of [YamlListWrapper.nodes] as a wrapper around the Dart | |
| 107 /// list. | |
| 108 class _YamlListNodes extends ListBase<YamlNode> { | |
| 109 final List _dartList; | |
| 110 | |
| 111 final Span _span; | |
| 112 | |
| 113 int get length => _dartList.length; | |
| 114 | |
| 115 set length(int index) { | |
| 116 throw new UnsupportedError("Cannot modify an unmodifiable List"); | |
| 117 } | |
| 118 | |
| 119 _YamlListNodes(this._dartList, this._span); | |
| 120 | |
| 121 YamlNode operator [](int index) { | |
| 122 var value = _dartList[index]; | |
| 123 if (value is Map) return new YamlMapWrapper._(value, _span); | |
| 124 if (value is List) return new YamlListWrapper._(value, _span); | |
| 125 return new YamlScalar.internal(value, _span); | |
| 126 } | |
| 127 | |
| 128 operator []=(int index, value) { | |
| 129 throw new UnsupportedError("Cannot modify an unmodifiable List"); | |
| 130 } | |
| 131 } | |
| OLD | NEW |