| OLD | NEW |
| 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 library yaml.map; | 5 library yaml.yaml_node; |
| 6 | 6 |
| 7 import 'dart:collection' as collection; | 7 import 'dart:collection' as collection; |
| 8 | 8 |
| 9 import 'package:collection/collection.dart'; | 9 import 'package:collection/collection.dart'; |
| 10 import 'package:source_maps/source_maps.dart'; | 10 import 'package:source_maps/source_maps.dart'; |
| 11 | 11 |
| 12 import 'null_span.dart'; |
| 13 import 'yaml_node_wrapper.dart'; |
| 14 |
| 12 /// An interface for parsed nodes from a YAML source tree. | 15 /// An interface for parsed nodes from a YAML source tree. |
| 13 /// | 16 /// |
| 14 /// [YamlMap]s and [YamlList]s implement this interface in addition to the | 17 /// [YamlMap]s and [YamlList]s implement this interface in addition to the |
| 15 /// normal [Map] and [List] interfaces, so any maps and lists will be | 18 /// normal [Map] and [List] interfaces, so any maps and lists will be |
| 16 /// [YamlNode]s regardless of how they're accessed. | 19 /// [YamlNode]s regardless of how they're accessed. |
| 17 /// | 20 /// |
| 18 /// Scalars values like strings and numbers, on the other hand, don't have this | 21 /// Scalars values like strings and numbers, on the other hand, don't have this |
| 19 /// interface by default. Instead, they can be accessed as [YamlScalar]s via | 22 /// interface by default. Instead, they can be accessed as [YamlScalar]s via |
| 20 /// [YamlMap.nodes] or [YamlList.nodes]. | 23 /// [YamlMap.nodes] or [YamlList.nodes]. |
| 21 abstract class YamlNode { | 24 abstract class YamlNode { |
| 22 /// The source span for this node. | 25 /// The source span for this node. |
| 23 /// | 26 /// |
| 24 /// [Span.getLocationMessage] can be used to produce a human-friendly message | 27 /// [Span.getLocationMessage] can be used to produce a human-friendly message |
| 25 /// about this node. | 28 /// about this node. |
| 26 Span get span; | 29 Span get span; |
| 27 | 30 |
| 28 /// The inner value of this node. | 31 /// The inner value of this node. |
| 29 /// | 32 /// |
| 30 /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and | 33 /// For [YamlScalar]s, this will return the wrapped value. For [YamlMap] and |
| 31 /// [YamlList], it will return [this], since they already implement [Map] and | 34 /// [YamlList], it will return [this], since they already implement [Map] and |
| 32 /// [List], respectively. | 35 /// [List], respectively. |
| 33 get value; | 36 get value; |
| 34 } | 37 } |
| 35 | 38 |
| 36 /// A read-only [Map] parsed from YAML. | 39 /// A read-only [Map] parsed from YAML. |
| 37 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { | 40 class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { |
| 38 final Span span; | 41 final Span span; |
| 39 | 42 |
| 43 /// A view of [this] where the keys and values are guaranteed to be |
| 44 /// [YamlNode]s. |
| 45 /// |
| 46 /// The key type is `dynamic` to allow values to be accessed using |
| 47 /// non-[YamlNode] keys, but [Map.keys] and [Map.forEach] will always expose |
| 48 /// them as [YamlNode]s. For example, for `{"foo": [1, 2, 3]}` [nodes] will be |
| 49 /// a map from a [YamlScalar] to a [YamlList], but since the key type is |
| 50 /// `dynamic` `map.nodes["foo"]` will still work. |
| 40 final Map<dynamic, YamlNode> nodes; | 51 final Map<dynamic, YamlNode> nodes; |
| 41 | 52 |
| 42 Map get value => this; | 53 Map get value => this; |
| 43 | 54 |
| 44 Iterable get keys => nodes.keys.map((node) => node.value); | 55 Iterable get keys => nodes.keys.map((node) => node.value); |
| 45 | 56 |
| 46 /// Users of the library should not construct [YamlMap]s manually. | 57 /// Creates an empty YamlMap. |
| 47 YamlMap(Map<dynamic, YamlNode> nodes, this.span) | 58 /// |
| 59 /// This map's [span] won't have useful location information. However, it will |
| 60 /// have a reasonable implementation of [Span.getLocationMessage]. If |
| 61 /// [sourceName] is passed, it's used as the [Span.sourceUrl]. |
| 62 factory YamlMap({String sourceName}) => |
| 63 new YamlMapWrapper(const {}, sourceName); |
| 64 |
| 65 /// Wraps a Dart map so that it can be accessed (recursively) like a |
| 66 /// [YamlMap]. |
| 67 /// |
| 68 /// Any [Span]s returned by this map or its children will be dummies without |
| 69 /// useful location information. However, they will have a reasonable |
| 70 /// implementation of [Span.getLocationMessage]. If [sourceName] is passed, |
| 71 /// it's used as the [Span.sourceUrl]. |
| 72 factory YamlMap.wrap(Map dartMap, {String sourceName}) => |
| 73 new YamlMapWrapper(dartMap, sourceName); |
| 74 |
| 75 /// Users of the library should not use this constructor. |
| 76 YamlMap.internal(Map<dynamic, YamlNode> nodes, this.span) |
| 48 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); | 77 : nodes = new UnmodifiableMapView<dynamic, YamlNode>(nodes); |
| 49 | 78 |
| 50 operator [](key) { | 79 operator [](key) { |
| 51 var node = nodes[key]; | 80 var node = nodes[key]; |
| 52 return node == null ? null : node.value; | 81 return node == null ? null : node.value; |
| 53 } | 82 } |
| 54 } | 83 } |
| 55 | 84 |
| 56 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. | 85 // TODO(nweiz): Use UnmodifiableListMixin when issue 18970 is fixed. |
| 57 /// A read-only [List] parsed from YAML. | 86 /// A read-only [List] parsed from YAML. |
| 58 class YamlList extends YamlNode with collection.ListMixin { | 87 class YamlList extends YamlNode with collection.ListMixin { |
| 59 final Span span; | 88 final Span span; |
| 60 | 89 |
| 61 final List<YamlNode> nodes; | 90 final List<YamlNode> nodes; |
| 62 | 91 |
| 63 List get value => this; | 92 List get value => this; |
| 64 | 93 |
| 65 int get length => nodes.length; | 94 int get length => nodes.length; |
| 66 | 95 |
| 67 set length(int index) { | 96 set length(int index) { |
| 68 throw new UnsupportedError("Cannot modify an unmodifiable List"); | 97 throw new UnsupportedError("Cannot modify an unmodifiable List"); |
| 69 } | 98 } |
| 70 | 99 |
| 71 /// Users of the library should not construct [YamlList]s manually. | 100 /// Creates an empty YamlList. |
| 72 YamlList(List<YamlNode> nodes, this.span) | 101 /// |
| 102 /// This list's [span] won't have useful location information. However, it |
| 103 /// will have a reasonable implementation of [Span.getLocationMessage]. If |
| 104 /// [sourceName] is passed, it's used as the [Span.sourceUrl]. |
| 105 factory YamlList({String sourceName}) => |
| 106 new YamlListWrapper(const [], sourceName); |
| 107 |
| 108 /// Wraps a Dart list so that it can be accessed (recursively) like a |
| 109 /// [YamlList]. |
| 110 /// |
| 111 /// Any [Span]s returned by this list or its children will be dummies without |
| 112 /// useful location information. However, they will have a reasonable |
| 113 /// implementation of [Span.getLocationMessage]. If [sourceName] is passed, |
| 114 /// it's used as the [Span.sourceUrl]. |
| 115 factory YamlList.wrap(List dartList, {String sourceName}) => |
| 116 new YamlListWrapper(dartList, sourceName); |
| 117 |
| 118 /// Users of the library should not use this constructor. |
| 119 YamlList.internal(List<YamlNode> nodes, this.span) |
| 73 : nodes = new UnmodifiableListView<YamlNode>(nodes); | 120 : nodes = new UnmodifiableListView<YamlNode>(nodes); |
| 74 | 121 |
| 75 operator [](int index) => nodes[index].value; | 122 operator [](int index) => nodes[index].value; |
| 76 | 123 |
| 77 operator []=(int index, value) { | 124 operator []=(int index, value) { |
| 78 throw new UnsupportedError("Cannot modify an unmodifiable List"); | 125 throw new UnsupportedError("Cannot modify an unmodifiable List"); |
| 79 } | 126 } |
| 80 } | 127 } |
| 81 | 128 |
| 82 /// A wrapped scalar value parsed from YAML. | 129 /// A wrapped scalar value parsed from YAML. |
| 83 class YamlScalar extends YamlNode { | 130 class YamlScalar extends YamlNode { |
| 84 final Span span; | 131 final Span span; |
| 85 | 132 |
| 86 final value; | 133 final value; |
| 87 | 134 |
| 88 /// Users of the library should not construct [YamlScalar]s manually. | 135 /// Wraps a Dart value in a [YamlScalar]. |
| 89 YamlScalar(this.value, this.span); | 136 /// |
| 137 /// This scalar's [span] won't have useful location information. However, it |
| 138 /// will have a reasonable implementation of [Span.getLocationMessage]. If |
| 139 /// [sourceName] is passed, it's used as the [Span.sourceUrl]. |
| 140 YamlScalar.wrap(this.value, {String sourceName}) |
| 141 : span = new NullSpan(sourceName); |
| 142 |
| 143 /// Users of the library should not use this constructor. |
| 144 YamlScalar.internal(this.value, this.span); |
| 90 | 145 |
| 91 String toString() => value.toString(); | 146 String toString() => value.toString(); |
| 92 } | 147 } |
| OLD | NEW |