| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library yaml.yaml_node_wrapper; | 5 library yaml.yaml_node_wrapper; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:collection/collection.dart' as pkg_collection; | 9 import 'package:collection/collection.dart' as pkg_collection; |
| 10 import 'package:source_maps/source_maps.dart'; | 10 import 'package:source_span/source_span.dart'; |
| 11 | 11 |
| 12 import 'null_span.dart'; | 12 import 'null_span.dart'; |
| 13 import 'yaml_node.dart'; | 13 import 'yaml_node.dart'; |
| 14 | 14 |
| 15 /// A wrapper that makes a normal Dart map behave like a [YamlMap]. | 15 /// A wrapper that makes a normal Dart map behave like a [YamlMap]. |
| 16 class YamlMapWrapper extends MapBase | 16 class YamlMapWrapper extends MapBase |
| 17 with pkg_collection.UnmodifiableMapMixin | 17 with pkg_collection.UnmodifiableMapMixin |
| 18 implements YamlMap { | 18 implements YamlMap { |
| 19 final Map _dartMap; | 19 final Map _dartMap; |
| 20 | 20 |
| 21 final Span span; | 21 final SourceSpan span; |
| 22 | 22 |
| 23 final Map<dynamic, YamlNode> nodes; | 23 final Map<dynamic, YamlNode> nodes; |
| 24 | 24 |
| 25 Map get value => this; | 25 Map get value => this; |
| 26 | 26 |
| 27 Iterable get keys => _dartMap.keys; | 27 Iterable get keys => _dartMap.keys; |
| 28 | 28 |
| 29 YamlMapWrapper(Map dartMap, String sourceName) | 29 YamlMapWrapper(Map dartMap, sourceUrl) |
| 30 : this._(dartMap, new NullSpan(sourceName)); | 30 : this._(dartMap, new NullSpan(sourceUrl)); |
| 31 | 31 |
| 32 YamlMapWrapper._(Map dartMap, Span span) | 32 YamlMapWrapper._(Map dartMap, SourceSpan span) |
| 33 : _dartMap = dartMap, | 33 : _dartMap = dartMap, |
| 34 span = span, | 34 span = span, |
| 35 nodes = new _YamlMapNodes(dartMap, span); | 35 nodes = new _YamlMapNodes(dartMap, span); |
| 36 | 36 |
| 37 operator [](Object key) { | 37 operator [](Object key) { |
| 38 var value = _dartMap[key]; | 38 var value = _dartMap[key]; |
| 39 if (value is Map) return new YamlMapWrapper._(value, span); | 39 if (value is Map) return new YamlMapWrapper._(value, span); |
| 40 if (value is List) return new YamlListWrapper._(value, span); | 40 if (value is List) return new YamlListWrapper._(value, span); |
| 41 return value; | 41 return value; |
| 42 } | 42 } |
| 43 | 43 |
| 44 int get hashCode => _dartMap.hashCode; | 44 int get hashCode => _dartMap.hashCode; |
| 45 | 45 |
| 46 operator ==(Object other) => | 46 operator ==(Object other) => |
| 47 other is YamlMapWrapper && other._dartMap == _dartMap; | 47 other is YamlMapWrapper && other._dartMap == _dartMap; |
| 48 } | 48 } |
| 49 | 49 |
| 50 /// The implementation of [YamlMapWrapper.nodes] as a wrapper around the Dart | 50 /// The implementation of [YamlMapWrapper.nodes] as a wrapper around the Dart |
| 51 /// map. | 51 /// map. |
| 52 class _YamlMapNodes extends MapBase<dynamic, YamlNode> | 52 class _YamlMapNodes extends MapBase<dynamic, YamlNode> |
| 53 with pkg_collection.UnmodifiableMapMixin<dynamic, YamlNode> { | 53 with pkg_collection.UnmodifiableMapMixin<dynamic, YamlNode> { |
| 54 final Map _dartMap; | 54 final Map _dartMap; |
| 55 | 55 |
| 56 final Span _span; | 56 final SourceSpan _span; |
| 57 | 57 |
| 58 Iterable get keys => | 58 Iterable get keys => |
| 59 _dartMap.keys.map((key) => new YamlScalar.internal(key, _span)); | 59 _dartMap.keys.map((key) => new YamlScalar.internal(key, _span)); |
| 60 | 60 |
| 61 _YamlMapNodes(this._dartMap, this._span); | 61 _YamlMapNodes(this._dartMap, this._span); |
| 62 | 62 |
| 63 YamlNode operator [](Object key) { | 63 YamlNode operator [](Object key) { |
| 64 // Use "as" here because key being assigned to invalidates type propagation. | 64 // Use "as" here because key being assigned to invalidates type propagation. |
| 65 if (key is YamlScalar) key = (key as YamlScalar).value; | 65 if (key is YamlScalar) key = (key as YamlScalar).value; |
| 66 if (!_dartMap.containsKey(key)) return null; | 66 if (!_dartMap.containsKey(key)) return null; |
| 67 return _nodeForValue(_dartMap[key], _span); | 67 return _nodeForValue(_dartMap[key], _span); |
| 68 } | 68 } |
| 69 | 69 |
| 70 int get hashCode => _dartMap.hashCode; | 70 int get hashCode => _dartMap.hashCode; |
| 71 | 71 |
| 72 operator ==(Object other) => | 72 operator ==(Object other) => |
| 73 other is _YamlMapNodes && other._dartMap == _dartMap; | 73 other is _YamlMapNodes && other._dartMap == _dartMap; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. | 76 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
| 77 /// A wrapper that makes a normal Dart list behave like a [YamlList]. | 77 /// A wrapper that makes a normal Dart list behave like a [YamlList]. |
| 78 class YamlListWrapper extends ListBase implements YamlList { | 78 class YamlListWrapper extends ListBase implements YamlList { |
| 79 final List _dartList; | 79 final List _dartList; |
| 80 | 80 |
| 81 final Span span; | 81 final SourceSpan span; |
| 82 | 82 |
| 83 final List<YamlNode> nodes; | 83 final List<YamlNode> nodes; |
| 84 | 84 |
| 85 List get value => this; | 85 List get value => this; |
| 86 | 86 |
| 87 int get length => _dartList.length; | 87 int get length => _dartList.length; |
| 88 | 88 |
| 89 set length(int index) { | 89 set length(int index) { |
| 90 throw new UnsupportedError("Cannot modify an unmodifiable List."); | 90 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 91 } | 91 } |
| 92 | 92 |
| 93 YamlListWrapper(List dartList, String sourceName) | 93 YamlListWrapper(List dartList, sourceUrl) |
| 94 : this._(dartList, new NullSpan(sourceName)); | 94 : this._(dartList, new NullSpan(sourceUrl)); |
| 95 | 95 |
| 96 YamlListWrapper._(List dartList, Span span) | 96 YamlListWrapper._(List dartList, SourceSpan span) |
| 97 : _dartList = dartList, | 97 : _dartList = dartList, |
| 98 span = span, | 98 span = span, |
| 99 nodes = new _YamlListNodes(dartList, span); | 99 nodes = new _YamlListNodes(dartList, span); |
| 100 | 100 |
| 101 operator [](int index) { | 101 operator [](int index) { |
| 102 var value = _dartList[index]; | 102 var value = _dartList[index]; |
| 103 if (value is Map) return new YamlMapWrapper._(value, span); | 103 if (value is Map) return new YamlMapWrapper._(value, span); |
| 104 if (value is List) return new YamlListWrapper._(value, span); | 104 if (value is List) return new YamlListWrapper._(value, span); |
| 105 return value; | 105 return value; |
| 106 } | 106 } |
| 107 | 107 |
| 108 operator []=(int index, value) { | 108 operator []=(int index, value) { |
| 109 throw new UnsupportedError("Cannot modify an unmodifiable List."); | 109 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 110 } | 110 } |
| 111 | 111 |
| 112 int get hashCode => _dartList.hashCode; | 112 int get hashCode => _dartList.hashCode; |
| 113 | 113 |
| 114 operator ==(Object other) => | 114 operator ==(Object other) => |
| 115 other is YamlListWrapper && other._dartList == _dartList; | 115 other is YamlListWrapper && other._dartList == _dartList; |
| 116 } | 116 } |
| 117 | 117 |
| 118 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. | 118 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
| 119 /// The implementation of [YamlListWrapper.nodes] as a wrapper around the Dart | 119 /// The implementation of [YamlListWrapper.nodes] as a wrapper around the Dart |
| 120 /// list. | 120 /// list. |
| 121 class _YamlListNodes extends ListBase<YamlNode> { | 121 class _YamlListNodes extends ListBase<YamlNode> { |
| 122 final List _dartList; | 122 final List _dartList; |
| 123 | 123 |
| 124 final Span _span; | 124 final SourceSpan _span; |
| 125 | 125 |
| 126 int get length => _dartList.length; | 126 int get length => _dartList.length; |
| 127 | 127 |
| 128 set length(int index) { | 128 set length(int index) { |
| 129 throw new UnsupportedError("Cannot modify an unmodifiable List."); | 129 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 130 } | 130 } |
| 131 | 131 |
| 132 _YamlListNodes(this._dartList, this._span); | 132 _YamlListNodes(this._dartList, this._span); |
| 133 | 133 |
| 134 YamlNode operator [](int index) => _nodeForValue(_dartList[index], _span); | 134 YamlNode operator [](int index) => _nodeForValue(_dartList[index], _span); |
| 135 | 135 |
| 136 operator []=(int index, value) { | 136 operator []=(int index, value) { |
| 137 throw new UnsupportedError("Cannot modify an unmodifiable List."); | 137 throw new UnsupportedError("Cannot modify an unmodifiable List."); |
| 138 } | 138 } |
| 139 | 139 |
| 140 int get hashCode => _dartList.hashCode; | 140 int get hashCode => _dartList.hashCode; |
| 141 | 141 |
| 142 operator ==(Object other) => | 142 operator ==(Object other) => |
| 143 other is _YamlListNodes && other._dartList == _dartList; | 143 other is _YamlListNodes && other._dartList == _dartList; |
| 144 } | 144 } |
| 145 | 145 |
| 146 YamlNode _nodeForValue(value, Span span) { | 146 YamlNode _nodeForValue(value, SourceSpan span) { |
| 147 if (value is Map) return new YamlMapWrapper._(value, span); | 147 if (value is Map) return new YamlMapWrapper._(value, span); |
| 148 if (value is List) return new YamlListWrapper._(value, span); | 148 if (value is List) return new YamlListWrapper._(value, span); |
| 149 return new YamlScalar.internal(value, span); | 149 return new YamlScalar.internal(value, span); |
| 150 } | 150 } |
| OLD | NEW |