[pigeon] support nested value in @FlutterApi for --java_out, just like what objc does. (#204)

This commit is contained in:
orangetangerine
2020-09-23 23:17:50 +08:00
committed by GitHub
parent 32d0f564db
commit 8b9e8eee88
3 changed files with 47 additions and 1 deletions

View File

@ -2,6 +2,7 @@
* Fixed Dart compilation for later versions that support null safety, opting out * Fixed Dart compilation for later versions that support null safety, opting out
of it for now. of it for now.
* Fixed nested types in the Java runtime.
## 0.1.6 ## 0.1.6

View File

@ -178,6 +178,9 @@ String _castObject(Field field, List<Class> classes, String varName) {
getHostDatatype(field, classes, _javaTypeForDartType); getHostDatatype(field, classes, _javaTypeForDartType);
if (field.dataType == 'int') { if (field.dataType == 'int') {
return '($varName == null) ? null : (($varName instanceof Integer) ? (Integer)$varName : (${hostDatatype.datatype})$varName)'; return '($varName == null) ? null : (($varName instanceof Integer) ? (Integer)$varName : (${hostDatatype.datatype})$varName)';
} else if (!hostDatatype.isBuiltin &&
classes.map((Class x) => x.name).contains(field.dataType)) {
return '${hostDatatype.datatype}.fromMap((HashMap)$varName)';
} else { } else {
return '(${hostDatatype.datatype})$varName'; return '(${hostDatatype.datatype})$varName';
} }
@ -186,6 +189,8 @@ String _castObject(Field field, List<Class> classes, String varName) {
/// Generates the ".java" file for the AST represented by [root] to [sink] with the /// Generates the ".java" file for the AST represented by [root] to [sink] with the
/// provided [options]. /// provided [options].
void generateJava(JavaOptions options, Root root, StringSink sink) { void generateJava(JavaOptions options, Root root, StringSink sink) {
final Set<String> rootClassNameSet =
root.classes.map((Class x) => x.name).toSet();
final Indent indent = Indent(sink); final Indent indent = Indent(sink);
indent.writeln('// $generatedCodeWarning'); indent.writeln('// $generatedCodeWarning');
indent.writeln('// $seeAlsoWarning'); indent.writeln('// $seeAlsoWarning');
@ -227,7 +232,16 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
indent.writeln( indent.writeln(
'HashMap<String, Object> toMapResult = new HashMap<>();'); 'HashMap<String, Object> toMapResult = new HashMap<>();');
for (Field field in klass.fields) { for (Field field in klass.fields) {
indent.writeln('toMapResult.put("${field.name}", ${field.name});'); final HostDatatype hostDatatype =
getHostDatatype(field, root.classes, _javaTypeForDartType);
String toWriteValue = '';
if (!hostDatatype.isBuiltin &&
rootClassNameSet.contains(field.dataType)) {
toWriteValue = '${field.name}.toMap()';
} else {
toWriteValue = field.name;
}
indent.writeln('toMapResult.put("${field.name}", $toWriteValue);');
} }
indent.writeln('return toMapResult;'); indent.writeln('return toMapResult;');
}); });

View File

@ -229,4 +229,35 @@ void main() {
expect(code, contains('public static class Foobar')); expect(code, contains('public static class Foobar'));
expect(code, contains('private HashMap field1;')); expect(code, contains('private HashMap field1;'));
}); });
test('gen nested', () {
final Class klass = Class()
..name = 'Outer'
..fields = <Field>[
Field()
..name = 'nested'
..dataType = 'Nested'
];
final Class nestedClass = Class()
..name = 'Nested'
..fields = <Field>[
Field()
..name = 'data'
..dataType = 'int'
];
final Root root = Root()
..apis = <Api>[]
..classes = <Class>[klass, nestedClass];
final StringBuffer sink = StringBuffer();
final JavaOptions javaOptions = JavaOptions();
javaOptions.className = 'Messages';
generateJava(javaOptions, root, sink);
final String code = sink.toString();
expect(code, contains('public class Messages'));
expect(code, contains('public static class Outer'));
expect(code, contains('public static class Nested'));
expect(code, contains('private Nested nested;'));
expect(code, contains('Nested.fromMap((HashMap)nested);'));
expect(code, contains('put("nested", nested.toMap());'));
});
} }