| 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 pkg_collection; |
| 10 import 'package:source_maps/source_maps.dart'; |
| 11 |
| 12 import 'null_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 pkg_collection.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 NullSpan(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); |
| 41 return value; |
| 42 } |
| 43 |
| 44 int get hashCode => _dartMap.hashCode; |
| 45 |
| 46 operator ==(Object other) => |
| 47 other is YamlMapWrapper && other._dartMap == _dartMap; |
| 48 } |
| 49 |
| 50 /// The implementation of [YamlMapWrapper.nodes] as a wrapper around the Dart |
| 51 /// map. |
| 52 class _YamlMapNodes extends MapBase<dynamic, YamlNode> |
| 53 with pkg_collection.UnmodifiableMapMixin<dynamic, YamlNode> { |
| 54 final Map _dartMap; |
| 55 |
| 56 final Span _span; |
| 57 |
| 58 Iterable get keys => |
| 59 _dartMap.keys.map((key) => new YamlScalar.internal(key, _span)); |
| 60 |
| 61 _YamlMapNodes(this._dartMap, this._span); |
| 62 |
| 63 YamlNode operator [](Object key) { |
| 64 if (key is YamlScalar) key = key.value; |
| 65 if (!_dartMap.containsKey(key)) return null; |
| 66 return _nodeForValue(_dartMap[key], _span); |
| 67 } |
| 68 |
| 69 int get hashCode => _dartMap.hashCode; |
| 70 |
| 71 operator ==(Object other) => |
| 72 other is _YamlMapNodes && other._dartMap == _dartMap; |
| 73 } |
| 74 |
| 75 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
| 76 /// A wrapper that makes a normal Dart list behave like a [YamlList]. |
| 77 class YamlListWrapper extends ListBase implements YamlList { |
| 78 final List _dartList; |
| 79 |
| 80 final Span span; |
| 81 |
| 82 final List<YamlNode> nodes; |
| 83 |
| 84 List get value => this; |
| 85 |
| 86 int get length => _dartList.length; |
| 87 |
| 88 set length(int index) { |
| 89 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 90 } |
| 91 |
| 92 YamlListWrapper(List dartList, String sourceName) |
| 93 : this._(dartList, new NullSpan(sourceName)); |
| 94 |
| 95 YamlListWrapper._(List dartList, Span span) |
| 96 : _dartList = dartList, |
| 97 span = span, |
| 98 nodes = new _YamlListNodes(dartList, span); |
| 99 |
| 100 operator [](int index) { |
| 101 var value = _dartList[index]; |
| 102 if (value is Map) return new YamlMapWrapper._(value, span); |
| 103 if (value is List) return new YamlListWrapper._(value, span); |
| 104 return value; |
| 105 } |
| 106 |
| 107 operator []=(int index, value) { |
| 108 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 109 } |
| 110 |
| 111 int get hashCode => _dartList.hashCode; |
| 112 |
| 113 operator ==(Object other) => |
| 114 other is YamlListWrapper && other._dartList == _dartList; |
| 115 } |
| 116 |
| 117 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
| 118 /// The implementation of [YamlListWrapper.nodes] as a wrapper around the Dart |
| 119 /// list. |
| 120 class _YamlListNodes extends ListBase<YamlNode> { |
| 121 final List _dartList; |
| 122 |
| 123 final Span _span; |
| 124 |
| 125 int get length => _dartList.length; |
| 126 |
| 127 set length(int index) { |
| 128 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 129 } |
| 130 |
| 131 _YamlListNodes(this._dartList, this._span); |
| 132 |
| 133 YamlNode operator [](int index) => _nodeForValue(_dartList[index], _span); |
| 134 |
| 135 operator []=(int index, value) { |
| 136 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 137 } |
| 138 |
| 139 int get hashCode => _dartList.hashCode; |
| 140 |
| 141 operator ==(Object other) => |
| 142 other is _YamlListNodes && other._dartList == _dartList; |
| 143 } |
| 144 |
| 145 YamlNode _nodeForValue(value, Span span) { |
| 146 if (value is Map) return new YamlMapWrapper._(value, span); |
| 147 if (value is List) return new YamlListWrapper._(value, span); |
| 148 return new YamlScalar.internal(value, span); |
| 149 } |
| OLD | NEW |