Created
March 26, 2020 07:50
-
-
Save masterashu/253f10860b910e83ef7409409ad7ceec to your computer and use it in GitHub Desktop.
dumpYaml utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'yaml_writer.dart'; | |
getObjects() sync* { | |
//---------------------------------------------------------------------------- | |
var obj = Map<String, dynamic>(); | |
obj['Hello'] = 'World'; | |
obj['Users'] = [ | |
'192.168.13.25', | |
'192.168.1.3', | |
<String>['192.168.13.25', '192.168.1.3', '192.168.1.3'] | |
]; | |
obj['Home'] = <String>['localhost', '127.0.0.1']; | |
yield obj; | |
//---------------------------------------------------------------------------- | |
var obj2 = Map<String, dynamic>(); | |
obj2['Hello'] = 'World'; | |
obj2['Users'] = [ | |
'192.168.13.25', | |
'192.168.1.3', | |
Map<String, dynamic>.fromIterables(["ABCD", "EFGH"], [132, 3353.3]), | |
]; | |
obj2['Home'] = Map<String, dynamic>.fromIterables( | |
['localhost', 'subnet mask'], ['127.0.0.1', '255.255.255.0']); | |
yield obj2; | |
//---------------------------------------------------------------------------- | |
var obj3 = Map<dynamic, dynamic>(); | |
obj3[<String>['localhost', '127.0.0.1']] = 'World'; | |
obj3[<String>['192.168.13.25', '192.168.1.3', '192.168.1.3']] = [ | |
<String>['192.168.13.25', '192.168.1.3', '192.168.1.3'], | |
]; | |
obj3[Map<dynamic, dynamic>.fromIterables([ | |
["ABCD", "EFGH"] | |
], [ | |
[132, 3353.3] | |
])] = ['localhost', '127.0.0.1', 1324, 4636343]; | |
yield obj3; | |
} | |
main(List<String> args) { | |
var buffer = StringBuffer(); | |
var yamlWriter = YamlWriter(outputStream: buffer); | |
for (var object in getObjects()) { | |
yamlWriter.writeDocument(object); | |
} | |
if (args.contains('file')) { | |
var file = File('data.yaml'); | |
file.writeAsStringSync(buffer.toString()); | |
} else { | |
print(buffer.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YamlWriter { | |
static bool isScalar(node) => (node is String || node is num || node is bool); | |
/// Maximum no. of columns before line break occurs | |
final int maxColumns; | |
/// Output File stream where to write | |
StringBuffer outputStream; | |
/// List of indentations | |
var _indents = <int>[0]; | |
/// current starting indentation | |
int get indent => _indents.last; | |
/// Increases the indentation of the writer | |
void addIndent() => _indents.add(indent + 2); | |
/// Decreses the indentation of the writer | |
void popIndent() => _indents.removeLast(); | |
/// Resets the indentation to Zero | |
void resetIndent() => _indents.removeWhere((n) => n > 0); | |
/// Position of current writing column | |
var column = 0; | |
/// write an Object to the output stream | |
void writeDocument(Object document) { | |
resetIndent(); | |
writeScalar("---"); | |
writeNewLine(); | |
if (document is String || document is num || document is bool) { | |
writeScalar(document); | |
} else { | |
writeNode(document); | |
} | |
resetIndent(); | |
writeScalar("..."); | |
writeNewLine(); | |
} | |
void writeMap(Map map) { | |
for (var key in map.keys) { | |
bool complexKey = false; | |
writeIndent(); | |
if (isScalar(key)) { | |
outputStream.write('${key.toString()}'); | |
} else if (key is Map) { | |
writeKeyEntry(); | |
writeMap(key); | |
complexKey = true; | |
} else if (key is List) { | |
writeKeyEntry(); | |
writeList(key); | |
complexKey = true; | |
} else { | |
print('Unknown type of object'); | |
} | |
if (complexKey) popIndent(); | |
writeValueEntry(complexKey); | |
var value = map[key]; | |
if (isScalar(value)) { | |
outputStream.write('${value.toString()}'); | |
} else if (value is Map) { | |
writeNewLine(); | |
writeMap(value); | |
} else if (value is List) { | |
writeNewLine(); | |
writeList(value); | |
} else { | |
print('Unknown type of object'); | |
} | |
popIndent(); | |
if (column != 0) writeNewLine(); | |
} | |
} | |
void writeList(List list) { | |
for (var node in list) { | |
writeIndent(); | |
writeListEntry(); | |
if (isScalar(node)) { | |
outputStream.write('${node.toString()}'); | |
} else if (node is Map) { | |
addIndent(); | |
writeNewLine(); | |
writeMap(node); | |
popIndent(); | |
} else if (node is List) { | |
addIndent(); | |
writeNewLine(); | |
writeList(node); | |
popIndent(); | |
} else { | |
print('Unknown type of object'); | |
} | |
if (column != 0) writeNewLine(); | |
} | |
} | |
void writeNode(Object node) { | |
if (node is Map) { | |
writeMap(node); | |
} else if (node is List) { | |
writeList(node); | |
} else { | |
writeNode(node); | |
} | |
} | |
void writeScalar(Object scalar) { | |
var value = scalar.toString(); | |
if (value.contains(RegExp(r'(\r)|(\n)|(\r\n)'))) { | |
value.replaceAll(RegExp(r'(\r)|(\n)|(\r\n)'), '\$'); | |
} | |
outputStream.write('$value'); | |
column += value.length; | |
} | |
void writeIndent() { | |
outputStream.write(' ' * indent); | |
column += indent; | |
} | |
void writeListEntry() { | |
outputStream.write('- '); | |
column += 2; | |
} | |
void writeKeyEntry() { | |
outputStream.write('? '); | |
addIndent(); | |
writeNewLine(); | |
} | |
void writeValueEntry([bool complexKey = false]) { | |
if (complexKey) writeIndent(); | |
outputStream.write(': '); | |
column += 2; | |
addIndent(); | |
} | |
void writeNewLine() { | |
outputStream.write('\n'); | |
column = 0; | |
} | |
YamlWriter({ | |
this.outputStream, | |
this.maxColumns = 80, | |
}) { | |
assert(outputStream != null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment