Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Unified Diff: pkg/docgen/lib/dart2yaml.dart

Issue 20162006: Fixed YAML output for a list of maps (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Sorted the keys for consistency. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/docgen/lib/dart2yaml.dart
diff --git a/pkg/docgen/lib/dart2yaml.dart b/pkg/docgen/lib/dart2yaml.dart
index cccb8eb084cec52be21ec3ee46de633dbd7f6e61..a4a18f057b84d30304c5d3e34e20db51833e299d 100644
--- a/pkg/docgen/lib/dart2yaml.dart
+++ b/pkg/docgen/lib/dart2yaml.dart
@@ -17,31 +17,44 @@ String getYamlString(Map documentData) {
}
/**
- * This recursive function adds to its input StringBuffer and builds
- * a YAML string from the input Map.
+ * This recursive function builds a YAML string from [documentData] and
+ * adds it to [yaml].
+ * The [level] input determines the indentation of the block being processed.
+ * The [isList] input determines whether [documentData] is a member of an outer
+ * lists of maps. A map must be preceeded with a '-' if it is to exist at the
+ * same level of indentation in the YAML output as other members of the list.
*/
-// TODO(tmandel): Fix quotes with String objects.
-void _addLevel(StringBuffer yaml, Map documentData, int level) {
- documentData.keys.forEach( (key) {
+void _addLevel(StringBuffer yaml, Map documentData, int level,
+ {bool isList: false}) {
+ // Since the ordering of the keys could be non-deterministic, the keys
+ // are sorted to ensure consistency in the output.
+ var keys = documentData.keys.toList();
+ keys.sort((String first, String second) => first.compareTo(second));
Alan Knight 2013/07/26 20:51:03 Real nit. You can omit the argument to sort and th
+ keys.forEach((key) {
_calcSpaces(level, yaml);
+ // Only the first entry of the map should be preceeded with a '-' since
+ // the map is a member of an outer list and the map as a whole must be
+ // marked as a single member of that list. See example 2.4 at
+ // http://www.yaml.org/spec/1.2/spec.html#id2759963
+ if (isList && key == keys.first) {
+ yaml.write("- ");
+ level++;
+ }
yaml.write("\"$key\" : ");
-
if (documentData[key] is Map) {
yaml.write("\n");
_addLevel(yaml, documentData[key], level + 1);
-
} else if (documentData[key] is List) {
var elements = documentData[key];
yaml.write("\n");
elements.forEach( (element) {
if (element is Map) {
- _addLevel(yaml, element, level + 1);
+ _addLevel(yaml, element, level + 1, isList: true);
} else {
_calcSpaces(level + 1, yaml);
yaml.write("- ${_processElement(element)}");
}
});
-
} else {
yaml.write(_processElement(documentData[key]));
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698