Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
| 6 * This library is used to convert data from a map to a YAML string. | 6 * This library is used to convert data from a map to a YAML string. |
| 7 */ | 7 */ |
| 8 library dart2yaml; | 8 library dart2yaml; |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Gets a String representing the input Map in YAML format. | 11 * Gets a String representing the input Map in YAML format. |
| 12 */ | 12 */ |
| 13 String getYamlString(Map documentData) { | 13 String getYamlString(Map documentData) { |
| 14 StringBuffer yaml = new StringBuffer(); | 14 StringBuffer yaml = new StringBuffer(); |
| 15 _addLevel(yaml, documentData, 0); | 15 _addLevel(yaml, documentData, 0); |
| 16 return yaml.toString(); | 16 return yaml.toString(); |
| 17 } | 17 } |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * This recursive function builds a YAML string from [documentData] and | 20 * This recursive function builds a YAML string from [documentData] and |
| 21 * adds it to [yaml]. | 21 * adds it to [yaml]. |
| 22 * The [level] input determines the indentation of the block being processed. | 22 * The [level] input determines the indentation of the block being processed. |
| 23 * The [isList] input determines whether [documentData] is a member of an outer | 23 * The [isList] input determines whether [documentData] is a member of an outer |
| 24 * lists of maps. A map must be preceeded with a '-' if it is to exist at the | 24 * lists of maps. A map must be preceeded with a '-' if it is to exist at the |
| 25 * same level of indentation in the YAML output as other members of the list. | 25 * same level of indentation in the YAML output as other members of the list. |
| 26 */ | 26 */ |
| 27 void _addLevel(StringBuffer yaml, Map documentData, int level, | 27 void _addLevel(StringBuffer yaml, Map documentData, int level, |
| 28 {bool isList: false}) { | 28 {bool isList: false}) { |
| 29 // Since the ordering of the keys could be non-deterministic, the keys | 29 // TODO(tmandel): The order of the keys could be nondeterministic, but it |
| 30 // are sorted to ensure consistency in the output. | 30 // is insufficient to just sort the keys, as their order could be significant |
| 31 // (i.e. parameters to a method). This should have a check that the map's key | |
| 32 // order is deterministic or something similar. | |
|
Jennifer Messerly
2013/08/16 21:43:32
maybe check that documentData is LinkedHashMap or
Tate Mandel
2013/08/16 21:53:41
Are these the only two that are ordered consistent
Jennifer Messerly
2013/08/16 22:05:49
hmm, it's tricky. I guess it depends on who calls
Tate Mandel
2013/08/16 22:09:45
I'd say it should be enforced elsewhere. We probab
| |
| 31 var keys = documentData.keys.toList(); | 33 var keys = documentData.keys.toList(); |
| 32 keys.sort(); | |
| 33 keys.forEach((key) { | 34 keys.forEach((key) { |
| 34 _calcSpaces(level, yaml); | 35 _calcSpaces(level, yaml); |
| 35 // Only the first entry of the map should be preceeded with a '-' since | 36 // Only the first entry of the map should be preceeded with a '-' since |
| 36 // the map is a member of an outer list and the map as a whole must be | 37 // the map is a member of an outer list and the map as a whole must be |
| 37 // marked as a single member of that list. See example 2.4 at | 38 // marked as a single member of that list. See example 2.4 at |
| 38 // http://www.yaml.org/spec/1.2/spec.html#id2759963 | 39 // http://www.yaml.org/spec/1.2/spec.html#id2759963 |
| 39 if (isList && key == keys.first) { | 40 if (isList && key == keys.first) { |
| 40 yaml.write("- "); | 41 yaml.write("- "); |
| 41 level++; | 42 level++; |
| 42 } | 43 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 | 75 |
| 75 /** | 76 /** |
| 76 * Based on the depth in the file, this function returns the correct spacing | 77 * Based on the depth in the file, this function returns the correct spacing |
| 77 * for an element in the YAML output. | 78 * for an element in the YAML output. |
| 78 */ | 79 */ |
| 79 void _calcSpaces(int spaceLevel, StringBuffer yaml) { | 80 void _calcSpaces(int spaceLevel, StringBuffer yaml) { |
| 80 for (int i = 0; i < spaceLevel; i++) { | 81 for (int i = 0; i < spaceLevel; i++) { |
| 81 yaml.write(" "); | 82 yaml.write(" "); |
| 82 } | 83 } |
| 83 } | 84 } |
| OLD | NEW |