[pigeon] fixed null support for java enums (#528)

This commit is contained in:
gaaclarke
2021-12-02 09:22:22 -08:00
committed by GitHub
parent 2fd81ca609
commit 62c9db672b
7 changed files with 42 additions and 9 deletions

View File

@ -1,3 +1,7 @@
## 1.0.12
* [java] Fixes enum support for null values.
## 1.0.11
* [ci] Starts transition to a Dart test runner, adds windows support.

View File

@ -8,7 +8,7 @@ import 'dart:mirrors';
import 'ast.dart';
/// The current version of pigeon. This must match the version in pubspec.yaml.
const String pigeonVersion = '1.0.11';
const String pigeonVersion = '1.0.12';
/// Read all the content from [stdin] to a String.
String readStdin() {

View File

@ -474,13 +474,13 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
root.enums,
(NamedType x) => _javaTypeForBuiltinDartType(x.type));
String toWriteValue = '';
final String fieldName = field.name;
if (!hostDatatype.isBuiltin &&
rootClassNameSet.contains(field.type.baseName)) {
final String fieldName = field.name;
toWriteValue = '($fieldName == null) ? null : $fieldName.toMap()';
} else if (!hostDatatype.isBuiltin &&
rootEnumNameSet.contains(field.type.baseName)) {
toWriteValue = '${field.name}.index';
toWriteValue = '$fieldName == null ? null : $fieldName.index';
} else {
toWriteValue = field.name;
}
@ -492,13 +492,14 @@ void generateJava(JavaOptions options, Root root, StringSink sink) {
indent.scoped('{', '}', () {
indent.writeln('${klass.name} fromMapResult = new ${klass.name}();');
for (final NamedType field in klass.fields) {
indent.writeln('Object ${field.name} = map.get("${field.name}");');
final String fieldVariable = field.name;
indent.writeln('Object $fieldVariable = map.get("${field.name}");');
if (rootEnumNameSet.contains(field.type.baseName)) {
indent.writeln(
'fromMapResult.${field.name} = ${field.type.baseName}.values()[(int)${field.name}];');
'fromMapResult.${field.name} = $fieldVariable == null ? null : ${field.type.baseName}.values()[(int)$fieldVariable];');
} else {
indent.writeln(
'fromMapResult.${field.name} = ${_castObject(field, root.classes, root.enums, field.name)};');
'fromMapResult.${field.name} = ${_castObject(field, root.classes, root.enums, fieldVariable)};');
}
}
indent.writeln('return fromMapResult;');

View File

@ -0,0 +1,21 @@
// Copyright 2013 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.
package com.example.android_unit_tests;
import static org.junit.Assert.*;
import java.util.Map;
import org.junit.Test;
public class EnumTest {
@Test
public void nullValue() {
Enum.Data value = new Enum.Data();
value.setState(null);
Map<String, Object> map = value.toMap();
Enum.Data readValue = Enum.Data.fromMap(map);
assertEquals(value.getState(), readValue.getState());
}
}

View File

@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/master/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
version: 1.0.11 # This must match the version in lib/generator_tools.dart
version: 1.0.12 # This must match the version in lib/generator_tools.dart
environment:
sdk: '>=2.12.0 <3.0.0'

View File

@ -320,6 +320,7 @@ run_android_unittests() {
gen_android_unittests_code ./pigeons/all_void.dart AllVoid
gen_android_unittests_code ./pigeons/android_unittests.dart Pigeon
gen_android_unittests_code ./pigeons/async_handlers.dart AsyncHandlers
gen_android_unittests_code ./pigeons/enum.dart Enum
gen_android_unittests_code ./pigeons/host2flutter.dart Host2Flutter
gen_android_unittests_code ./pigeons/java_double_host_api.dart JavaDoubleHostApi
gen_android_unittests_code ./pigeons/list.dart PigeonList

View File

@ -609,8 +609,14 @@ void main() {
expect(code, contains('private Enum1(final int index) {'));
expect(code, contains(' this.index = index;'));
expect(code, contains('toMapResult.put("enum1", enum1.index);'));
expect(code, contains('fromMapResult.enum1 = Enum1.values()[(int)enum1];'));
expect(
code,
contains(
'toMapResult.put("enum1", enum1 == null ? null : enum1.index);'));
expect(
code,
contains(
'fromMapResult.enum1 = enum1 == null ? null : Enum1.values()[(int)enum1];'));
});
Iterable<String> _makeIterable(String string) sync* {