| 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; | 5 library yaml; |
| 6 | 6 |
| 7 import 'src/composer.dart'; | 7 import 'src/composer.dart'; |
| 8 import 'src/constructor.dart'; | 8 import 'src/constructor.dart'; |
| 9 import 'src/parser.dart'; | 9 import 'src/parser.dart'; |
| 10 import 'src/yaml_exception.dart'; | 10 import 'src/yaml_exception.dart'; |
| 11 import 'src/yaml_node.dart'; |
| 11 | 12 |
| 12 export 'src/yaml_exception.dart'; | 13 export 'src/yaml_exception.dart'; |
| 13 export 'src/yaml_map.dart'; | 14 export 'src/yaml_node.dart'; |
| 14 | 15 |
| 15 /// Loads a single document from a YAML string. | 16 /// Loads a single document from a YAML string. |
| 16 /// | 17 /// |
| 17 /// If the string contains more than one document, this throws a | 18 /// If the string contains more than one document, this throws a |
| 18 /// [YamlException]. In future releases, this will become an [ArgumentError]. | 19 /// [YamlException]. In future releases, this will become an [ArgumentError]. |
| 19 /// | 20 /// |
| 20 /// The return value is mostly normal Dart objects. However, since YAML mappings | 21 /// The return value is mostly normal Dart objects. However, since YAML mappings |
| 21 /// support some key types that the default Dart map implementation doesn't | 22 /// support some key types that the default Dart map implementation doesn't |
| 22 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. | 23 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
| 23 /// These have a few small behavioral differences from the default Map | 24 /// These have a few small behavioral differences from the default Map |
| 24 /// implementation; for details, see the [YamlMap] class. | 25 /// implementation; for details, see the [YamlMap] class. |
| 25 /// | 26 /// |
| 26 /// In future versions, maps will instead be [HashMap]s with a custom equality | 27 /// In future versions, maps will instead be [HashMap]s with a custom equality |
| 27 /// operation. | 28 /// operation. |
| 28 /// | 29 /// |
| 29 /// If [sourceName] is passed, it's used as the name of the file or URL from | 30 /// If [sourceName] is passed, it's used as the name of the file or URL from |
| 30 /// which the YAML originated for error reporting. | 31 /// which the YAML originated for error reporting. |
| 31 loadYaml(String yaml, {String sourceName}) { | 32 loadYaml(String yaml, {String sourceName}) => |
| 33 loadYamlNode(yaml, sourceName: sourceName).value; |
| 34 |
| 35 /// Loads a single document from a YAML string as a [YamlNode]. |
| 36 /// |
| 37 /// This is just like [loadYaml], except that where [loadYaml] would return a |
| 38 /// normal Dart value this returns a [YamlNode] instead. This allows the caller |
| 39 /// to be confident that the return value will always be a [YamlNode]. |
| 40 YamlNode loadYamlNode(String yaml, {String sourceName}) { |
| 32 var stream = loadYamlStream(yaml, sourceName: sourceName); | 41 var stream = loadYamlStream(yaml, sourceName: sourceName); |
| 33 if (stream.length != 1) { | 42 if (stream.length != 1) { |
| 34 throw new YamlException("Expected 1 document, were ${stream.length}."); | 43 throw new YamlException("Expected 1 document, were ${stream.length}."); |
| 35 } | 44 } |
| 36 return stream[0]; | 45 return stream.nodes[0]; |
| 37 } | 46 } |
| 38 | 47 |
| 39 /// Loads a stream of documents from a YAML string. | 48 /// Loads a stream of documents from a YAML string. |
| 40 /// | 49 /// |
| 41 /// The return value is mostly normal Dart objects. However, since YAML mappings | 50 /// The return value is mostly normal Dart objects. However, since YAML mappings |
| 42 /// support some key types that the default Dart map implementation doesn't | 51 /// support some key types that the default Dart map implementation doesn't |
| 43 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. | 52 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
| 44 /// These have a few small behavioral differences from the default Map | 53 /// These have a few small behavioral differences from the default Map |
| 45 /// implementation; for details, see the [YamlMap] class. | 54 /// implementation; for details, see the [YamlMap] class. |
| 46 /// | 55 /// |
| 47 /// In future versions, maps will instead be [HashMap]s with a custom equality | 56 /// In future versions, maps will instead be [HashMap]s with a custom equality |
| 48 /// operation. | 57 /// operation. |
| 49 /// | 58 /// |
| 50 /// If [sourceName] is passed, it's used as the name of the file or URL from | 59 /// If [sourceName] is passed, it's used as the name of the file or URL from |
| 51 /// which the YAML originated for error reporting. | 60 /// which the YAML originated for error reporting. |
| 52 List loadYamlStream(String yaml, {String sourceName}) { | 61 YamlList loadYamlStream(String yaml, {String sourceName}) { |
| 53 var stream; | 62 var pair; |
| 54 try { | 63 try { |
| 55 stream = new Parser(yaml, sourceName).l_yamlStream(); | 64 pair = new Parser(yaml, sourceName).l_yamlStream(); |
| 56 } on FormatException catch (error) { | 65 } on FormatException catch (error) { |
| 57 throw new YamlException(error.toString()); | 66 throw new YamlException(error.toString()); |
| 58 } | 67 } |
| 59 | 68 |
| 60 return stream | 69 var nodes = pair.first |
| 61 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) | 70 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) |
| 62 .toList(); | 71 .toList(); |
| 72 return new YamlList(nodes, pair.last); |
| 63 } | 73 } |
| OLD | NEW |