Skip to content

Commit

Permalink
add json/data/datatxt interop
Browse files Browse the repository at this point in the history
  • Loading branch information
ebwood committed Mar 16, 2024
1 parent be41050 commit 9fceba0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
18 changes: 18 additions & 0 deletions packages/rfw_builder/lib/rfw_txt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ Uint8List txt2rfw(String txt) => encodeLibraryBlob(txt2rfl(txt));

// Parse .rfwtxt to RemoteWidgetLibrary
RemoteWidgetLibrary txt2rfl(String txt) => parseLibraryFile(txt);

// Parse json to rfw data txt
String json2dataTxt(Map<String, dynamic> map) => value2DataTxt(map).toString();

// Parse rfw data txt to json
DynamicMap dataTxt2json(String txt) => parseDataFile(txt);

// Parse json to rfw data
Uint8List json2data(DynamicMap map) => encodeDataBlob(map);

// Parse rfw data to json
DynamicMap data2json(Uint8List data) => decodeDataBlob(data) as DynamicMap;

// Parse rfw data to rfw data txt
String data2dataTxt(Uint8List data) => json2dataTxt(data2json(data));

// Parse rfw data txt to rfw data
Uint8List dataTxt2data(String txt) => encodeDataBlob(parseDataFile(txt));
32 changes: 32 additions & 0 deletions packages/rfw_builder/lib/src/json_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ extension DynamicMapEx on DynamicMap {
}
}

// convert value to rfw data txt
StringBuffer value2DataTxt(Object? value, [StringBuffer? buffer]) {
buffer ??= StringBuffer();
if (value == null) {
// nothing
return buffer;
}

if (value is String) {
buffer.write('"$value"');
} else if (value is DynamicMap) {
buffer.write('{');
for (var element in value.entries) {
buffer.write(element.key);
buffer.write(':');
value2DataTxt(element.value, buffer);
buffer.write(',');
}
buffer.write('}');
} else if (value is DynamicList) {
buffer.write('[');
for (var element in value) {
value2DataTxt(element, buffer);
buffer.write(',');
}
buffer.write(']');
} else {
buffer.write(value);
}
return buffer;
}

class JsonVisitor extends SpecVisitor<dynamic> {
@override
DynamicMap visitArgsReference(ArgsReference spec, [context]) {
Expand Down
7 changes: 3 additions & 4 deletions packages/rfw_builder/lib/src/txt_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ class TxtVisitor extends SpecVisitor<StringSink> {
StringSink visitDynamicList(DynamicList spec, [StringSink? context]) {
final output = context ?? StringBuffer();
output.write('[');
_depth++;
spec.forEachIndexed((index, element) {
output.write(' ');
visitObject(element, output);
output.write(index != spec.length - 1 ? ',' : ' ');
if (index != spec.length - 1) output.write(', ');
});
_depth--;
output.write(']');
return output;
}
Expand Down Expand Up @@ -160,7 +161,6 @@ class TxtVisitor extends SpecVisitor<StringSink> {
@override
StringSink visitLoop(Loop spec, [StringSink? context]) {
final output = context ?? StringBuffer();
_depth++;
output.write('\n');
output.write(_leadingIndent);
_loopVariableLength++;
Expand All @@ -172,7 +172,6 @@ class TxtVisitor extends SpecVisitor<StringSink> {
visitObject(spec.output, output);
_loopVariableLength--;
_depth--;
_depth--;
return output;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/rfw_builder/test/txt_visitor_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:rfw/formats.dart';
import 'package:rfw_builder/rfw_builder.dart';
import 'package:test/test.dart';

void main() {
test('TxtVisitor', () {
TxtVisitor txtVisitor = TxtVisitor();
// print(txtVisitor.visitLoop(Loop([1, 2, 3], )));
expect('loop0',
txtVisitor.visitLoopReference(LoopReference(0, [])).toString());
expect('loop0.name',
txtVisitor.visitLoopReference(LoopReference(0, ['name'])).toString());
});
}

0 comments on commit 9fceba0

Please sign in to comment.