[Pigeon] Added support for lists and maps in java and objc targets. (#186)

This commit is contained in:
gaaclarke
2020-07-31 13:07:04 -07:00
committed by GitHub
parent a4c2106d3b
commit c9053f1fea
8 changed files with 85 additions and 1 deletions

View File

@ -1,3 +1,7 @@
## 0.1.3
* Added support for List and Map datatypes in Java and Objective-C targets.
## 0.1.2+1 ## 0.1.2+1
* Updated the Readme.md. * Updated the Readme.md.

View File

@ -14,6 +14,8 @@ const Map<String, String> _javaTypeForDartTypeMap = <String, String>{
'Int32List': 'int[]', 'Int32List': 'int[]',
'Int64List': 'long[]', 'Int64List': 'long[]',
'Float64List': 'double[]', 'Float64List': 'double[]',
'List': 'ArrayList',
'Map': 'HashMap',
}; };
/// Options that control how Java code will be generated. /// Options that control how Java code will be generated.
@ -195,6 +197,7 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
indent.writeln('import io.flutter.plugin.common.BasicMessageChannel;'); indent.writeln('import io.flutter.plugin.common.BasicMessageChannel;');
indent.writeln('import io.flutter.plugin.common.BinaryMessenger;'); indent.writeln('import io.flutter.plugin.common.BinaryMessenger;');
indent.writeln('import io.flutter.plugin.common.StandardMessageCodec;'); indent.writeln('import io.flutter.plugin.common.StandardMessageCodec;');
indent.writeln('import java.util.ArrayList;');
indent.writeln('import java.util.HashMap;'); indent.writeln('import java.util.HashMap;');
indent.addln(''); indent.addln('');

View File

@ -41,6 +41,8 @@ const Map<String, String> _objcTypeForDartTypeMap = <String, String>{
'Int32List': 'FlutterStandardTypedData *', 'Int32List': 'FlutterStandardTypedData *',
'Int64List': 'FlutterStandardTypedData *', 'Int64List': 'FlutterStandardTypedData *',
'Float64List': 'FlutterStandardTypedData *', 'Float64List': 'FlutterStandardTypedData *',
'List': 'NSArray *',
'Map': 'NSDictionary *',
}; };
const Map<String, String> _propertyTypeForDartTypeMap = <String, String>{ const Map<String, String> _propertyTypeForDartTypeMap = <String, String>{
@ -52,6 +54,8 @@ const Map<String, String> _propertyTypeForDartTypeMap = <String, String>{
'Int32List': 'strong', 'Int32List': 'strong',
'Int64List': 'strong', 'Int64List': 'strong',
'Float64List': 'strong', 'Float64List': 'strong',
'List': 'strong',
'Map': 'strong',
}; };
String _objcTypeForDartType(String type) { String _objcTypeForDartType(String type) {

View File

@ -0,0 +1,15 @@
// Copyright 2020 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:pigeon/pigeon.dart';
class TestMessage {
// ignore: always_specify_types
List testList;
}
@HostApi()
abstract class TestApi {
void test(TestMessage msg);
}

View File

@ -1,5 +1,5 @@
name: pigeon name: pigeon
version: 0.1.2+1 version: 0.1.3
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
homepage: https://github.com/flutter/packages/tree/master/packages/pigeon homepage: https://github.com/flutter/packages/tree/master/packages/pigeon
dependencies: dependencies:

View File

@ -55,12 +55,14 @@ test_pigeon_android ./pigeons/host2flutter.dart
test_pigeon_android ./pigeons/message.dart test_pigeon_android ./pigeons/message.dart
test_pigeon_android ./pigeons/void_arg_host.dart test_pigeon_android ./pigeons/void_arg_host.dart
test_pigeon_android ./pigeons/void_arg_flutter.dart test_pigeon_android ./pigeons/void_arg_flutter.dart
test_pigeon_android ./pigeons/list.dart
test_pigeon_ios ./pigeons/message.dart test_pigeon_ios ./pigeons/message.dart
test_pigeon_ios ./pigeons/host2flutter.dart test_pigeon_ios ./pigeons/host2flutter.dart
test_pigeon_ios ./pigeons/voidhost.dart test_pigeon_ios ./pigeons/voidhost.dart
test_pigeon_ios ./pigeons/voidflutter.dart test_pigeon_ios ./pigeons/voidflutter.dart
test_pigeon_ios ./pigeons/void_arg_host.dart test_pigeon_ios ./pigeons/void_arg_host.dart
test_pigeon_ios ./pigeons/void_arg_flutter.dart test_pigeon_ios ./pigeons/void_arg_flutter.dart
test_pigeon_ios ./pigeons/list.dart
pushd $PWD pushd $PWD
pub run pigeon \ pub run pigeon \

View File

@ -199,4 +199,34 @@ void main() {
expect(code, contains('doSomething(Reply<Output>')); expect(code, contains('doSomething(Reply<Output>'));
expect(code, contains('channel.send(null')); expect(code, contains('channel.send(null'));
}); });
test('gen list', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Foobar',
fields: <Field>[Field(name: 'field1', dataType: 'List')]),
]);
final StringBuffer sink = StringBuffer();
final JavaOptions javaOptions = JavaOptions();
javaOptions.className = 'Messages';
generateJava(javaOptions, root, sink);
final String code = sink.toString();
expect(code, contains('public static class Foobar'));
expect(code, contains('private ArrayList field1;'));
});
test('gen map', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Foobar',
fields: <Field>[Field(name: 'field1', dataType: 'Map')]),
]);
final StringBuffer sink = StringBuffer();
final JavaOptions javaOptions = JavaOptions();
javaOptions.className = 'Messages';
generateJava(javaOptions, root, sink);
final String code = sink.toString();
expect(code, contains('public static class Foobar'));
expect(code, contains('private HashMap field1;'));
});
} }

View File

@ -405,4 +405,30 @@ void main() {
'(void)doSomething:(void(^)(ABCOutput*, NSError*))completion')); '(void)doSomething:(void(^)(ABCOutput*, NSError*))completion'));
expect(code, contains('channel sendMessage:nil')); expect(code, contains('channel sendMessage:nil'));
}); });
test('gen list', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Foobar',
fields: <Field>[Field(name: 'field1', dataType: 'List')]),
]);
final StringBuffer sink = StringBuffer();
generateObjcHeader(ObjcOptions(), root, sink);
final String code = sink.toString();
expect(code, contains('@interface Foobar'));
expect(code, matches('@property.*NSArray.*field1'));
});
test('gen map', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Foobar',
fields: <Field>[Field(name: 'field1', dataType: 'Map')]),
]);
final StringBuffer sink = StringBuffer();
generateObjcHeader(ObjcOptions(), root, sink);
final String code = sink.toString();
expect(code, contains('@interface Foobar'));
expect(code, matches('@property.*NSDictionary.*field1'));
});
} }