Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 10.1.3

* Adds generic `Object` field support to data classes.

## 10.1.2

* [swift] Fixes a crash when passing `null` for nested nullable classes.
Expand Down
5 changes: 5 additions & 0 deletions packages/pigeon/lib/cpp_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ class CppSourceGenerator extends StructuredGenerator<CppOptions> {
.map((Class x) => x.name)
.contains(field.type.baseName)) {
return '${hostDatatype.datatype}::FromEncodableList(std::get<EncodableList>($encodable))';
} else if (field.type.baseName == 'Object') {
Comment thread
tarrinneal marked this conversation as resolved.
Outdated
return encodable;
} else {
return 'std::get<${hostDatatype.datatype}>($encodable)';
}
Expand Down Expand Up @@ -1186,6 +1188,9 @@ ${prefix}reply(EncodableValue(std::move(wrapped)));''';
final String nonNullValue =
hostType.isNullable ? '(*$variableName)' : variableName;
encodableValue = 'EncodableValue((int)$nonNullValue)';
} else if (dartType.baseName == 'Object') {
final String operator = hostType.isNullable ? '*' : '';
encodableValue = '$operator$variableName';
} else {
final String operator = hostType.isNullable ? '*' : '';
encodableValue = 'EncodableValue($operator$variableName)';
Expand Down
29 changes: 14 additions & 15 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class DartGenerator extends StructuredGenerator<DartOptions> {
) {
void writeValueDecode(NamedType field, int index) {
final String resultAt = 'result[$index]';
String castCallPrefix = field.type.isNullable ? '?' : '!';
Comment thread
tarrinneal marked this conversation as resolved.
Outdated
final String genericType = _makeGenericTypeArguments(field.type);
final String castCall = _makeGenericCastCall(field.type);
final String nullableTag = field.type.isNullable ? '?' : '';
if (customClassNames.contains(field.type.baseName)) {
final String nonNullValue =
'${field.type.baseName}.decode($resultAt! as List<Object?>)';
Expand All @@ -227,23 +231,18 @@ $resultAt != null
indent.add(nonNullValue);
}
} else if (field.type.typeArguments.isNotEmpty) {
final String genericType = _makeGenericTypeArguments(field.type);
final String castCall = _makeGenericCastCall(field.type);
final String castCallPrefix = field.type.isNullable ? '?' : '!';
indent.add(
'($resultAt as $genericType?)$castCallPrefix$castCall',
);
} else {
final String genericdType = _addGenericTypesNullable(field.type);
if (field.type.isNullable) {
indent.add(
'$resultAt as $genericdType',
);
} else {
indent.add(
'$resultAt! as $genericdType',
);
}
castCallPrefix = field.type.isNullable ? '' : '!';
final String castString = field.type.baseName == 'Object'
? ''
: ' as $genericType$nullableTag';

indent.add(
'$resultAt$castCallPrefix$castString',
);
}
}

Expand Down Expand Up @@ -757,8 +756,8 @@ String _addGenericTypes(TypeDeclaration type) {
}

String _addGenericTypesNullable(TypeDeclaration type) {
final String genericdType = _addGenericTypes(type);
return type.isNullable ? '$genericdType?' : genericdType;
final String genericType = _addGenericTypes(type);
return type.isNullable ? '$genericType?' : genericType;
}

/// Crawls up the path of [dartFilePath] until it finds a pubspec.yaml in a
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '10.1.2';
const String pigeonVersion = '10.1.3';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
1 change: 1 addition & 0 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ String? _javaTypeForBuiltinDartType(TypeDeclaration type) {
'Int32List': 'int[]',
'Int64List': 'long[]',
'Float64List': 'double[]',
'Object': 'Object',
};
if (javaTypeForDartTypeMap.containsKey(type.baseName)) {
return javaTypeForDartTypeMap[type.baseName];
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/mock_handler_tester/test/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v10.1.1), do not edit directly.
// Autogenerated from Pigeon (v10.1.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import

Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/mock_handler_tester/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Autogenerated from Pigeon (v10.1.1), do not edit directly.
// Autogenerated from Pigeon (v10.1.3), do not edit directly.
// See also: https://pub.dev/packages/pigeon
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
// ignore_for_file: avoid_relative_lib_imports
Expand Down
4 changes: 4 additions & 0 deletions packages/pigeon/pigeons/core_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AllTypes {
this.aMap = const <String?, Object?>{},
this.anEnum = AnEnum.one,
this.aString = '',
this.anObject = 0,
});

bool aBool;
Expand All @@ -41,6 +42,7 @@ class AllTypes {
Map aMap;
AnEnum anEnum;
String aString;
Object anObject;
}

/// A class containing all supported nullable types.
Expand All @@ -61,6 +63,7 @@ class AllNullableTypes {
this.nullableMapWithObject,
this.aNullableEnum,
this.aNullableString,
this.aNullableObject,
);

bool? aNullableBool;
Expand All @@ -80,6 +83,7 @@ class AllNullableTypes {
Map<String?, Object?>? nullableMapWithObject;
AnEnum? aNullableEnum;
String? aNullableString;
Object? aNullableObject;
}

/// A class for testing nested class handling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,19 @@ public void setAString(@NonNull String setterArg) {
this.aString = setterArg;
}

private @NonNull Object anObject;

public @NonNull Object getAnObject() {
return anObject;
}

public void setAnObject(@NonNull Object setterArg) {
if (setterArg == null) {
throw new IllegalStateException("Nonnull field \"anObject\" is null.");
}
this.anObject = setterArg;
}

/** Constructor is non-public to enforce null safety; use Builder. */
AllTypes() {}

Expand Down Expand Up @@ -322,6 +335,13 @@ public static final class Builder {
return this;
}

private @Nullable Object anObject;

public @NonNull Builder setAnObject(@NonNull Object setterArg) {
this.anObject = setterArg;
return this;
}

public @NonNull AllTypes build() {
AllTypes pigeonReturn = new AllTypes();
pigeonReturn.setABool(aBool);
Expand All @@ -336,13 +356,14 @@ public static final class Builder {
pigeonReturn.setAMap(aMap);
pigeonReturn.setAnEnum(anEnum);
pigeonReturn.setAString(aString);
pigeonReturn.setAnObject(anObject);
return pigeonReturn;
}
}

@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(12);
ArrayList<Object> toListResult = new ArrayList<Object>(13);
toListResult.add(aBool);
toListResult.add(anInt);
toListResult.add(anInt64);
Expand All @@ -355,6 +376,7 @@ ArrayList<Object> toList() {
toListResult.add(aMap);
toListResult.add(anEnum == null ? null : anEnum.index);
toListResult.add(aString);
toListResult.add(anObject);
return toListResult;
}

Expand Down Expand Up @@ -388,6 +410,8 @@ ArrayList<Object> toList() {
pigeonResult.setAnEnum(anEnum == null ? null : AnEnum.values()[(int) anEnum]);
Object aString = list.get(11);
pigeonResult.setAString((String) aString);
Object anObject = list.get(12);
pigeonResult.setAnObject(anObject);
return pigeonResult;
}
}
Expand Down Expand Up @@ -548,6 +572,16 @@ public void setANullableString(@Nullable String setterArg) {
this.aNullableString = setterArg;
}

private @Nullable Object aNullableObject;

public @Nullable Object getANullableObject() {
return aNullableObject;
}

public void setANullableObject(@Nullable Object setterArg) {
this.aNullableObject = setterArg;
}

public static final class Builder {

private @Nullable Boolean aNullableBool;
Expand Down Expand Up @@ -656,6 +690,13 @@ public static final class Builder {
return this;
}

private @Nullable Object aNullableObject;

public @NonNull Builder setANullableObject(@Nullable Object setterArg) {
this.aNullableObject = setterArg;
return this;
}

public @NonNull AllNullableTypes build() {
AllNullableTypes pigeonReturn = new AllNullableTypes();
pigeonReturn.setANullableBool(aNullableBool);
Expand All @@ -673,13 +714,14 @@ public static final class Builder {
pigeonReturn.setNullableMapWithObject(nullableMapWithObject);
pigeonReturn.setANullableEnum(aNullableEnum);
pigeonReturn.setANullableString(aNullableString);
pigeonReturn.setANullableObject(aNullableObject);
return pigeonReturn;
}
}

@NonNull
ArrayList<Object> toList() {
ArrayList<Object> toListResult = new ArrayList<Object>(15);
ArrayList<Object> toListResult = new ArrayList<Object>(16);
toListResult.add(aNullableBool);
toListResult.add(aNullableInt);
toListResult.add(aNullableInt64);
Expand All @@ -695,6 +737,7 @@ ArrayList<Object> toList() {
toListResult.add(nullableMapWithObject);
toListResult.add(aNullableEnum == null ? null : aNullableEnum.index);
toListResult.add(aNullableString);
toListResult.add(aNullableObject);
return toListResult;
}

Expand Down Expand Up @@ -739,6 +782,8 @@ ArrayList<Object> toList() {
aNullableEnum == null ? null : AnEnum.values()[(int) aNullableEnum]);
Object aNullableString = list.get(14);
pigeonResult.setANullableString((String) aNullableString);
Object aNullableObject = list.get(15);
pigeonResult.setANullableObject(aNullableObject);
return pigeonResult;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void compareAllTypes(AllTypes firstTypes, AllTypes secondTypes) {
assertArrayEquals(
firstTypes.getAMap().values().toArray(), secondTypes.getAMap().values().toArray());
assertEquals(firstTypes.getAnEnum(), secondTypes.getAnEnum());
assertEquals(firstTypes.getAString(), secondTypes.getAString());
assertEquals(firstTypes.getAnObject(), secondTypes.getAnObject());
}

void compareAllNullableTypes(AllNullableTypes firstTypes, AllNullableTypes secondTypes) {
Expand Down Expand Up @@ -68,6 +68,7 @@ void compareAllNullableTypes(AllNullableTypes firstTypes, AllNullableTypes secon
assertArrayEquals(
firstTypes.getNullableMapWithObject().values().toArray(),
secondTypes.getNullableMapWithObject().values().toArray());
assertEquals(firstTypes.getANullableObject(), secondTypes.getANullableObject());
}

@Test
Expand Down Expand Up @@ -151,6 +152,7 @@ public void hasValues() {
.setAList(Arrays.asList(new int[] {1, 2, 3}))
.setAMap(makeMap("hello", 1234))
.setAnEnum(CoreTests.AnEnum.ONE)
.setAnObject(0)
.build();

AllNullableTypes everything =
Expand All @@ -166,6 +168,7 @@ public void hasValues() {
.setANullableList(Arrays.asList(new int[] {1, 2, 3}))
.setANullableMap(makeMap("hello", 1234))
.setNullableMapWithObject(makeStringMap("hello", 1234))
.setANullableObject(0)
.build();

BinaryMessenger binaryMessenger = mock(BinaryMessenger.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
aList:(NSArray *)aList
aMap:(NSDictionary *)aMap
anEnum:(AnEnum)anEnum
aString:(NSString *)aString;
aString:(NSString *)aString
anObject:(id)anObject;
@property(nonatomic, strong) NSNumber *aBool;
@property(nonatomic, strong) NSNumber *anInt;
@property(nonatomic, strong) NSNumber *anInt64;
Expand All @@ -53,6 +54,7 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
@property(nonatomic, strong) NSDictionary *aMap;
@property(nonatomic, assign) AnEnum anEnum;
@property(nonatomic, copy) NSString *aString;
@property(nonatomic, strong) id anObject;
@end

/// A class containing all supported nullable types.
Expand All @@ -72,7 +74,8 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
(nullable NSDictionary<NSString *, NSString *> *)nullableMapWithAnnotations
nullableMapWithObject:(nullable NSDictionary<NSString *, id> *)nullableMapWithObject
aNullableEnum:(AnEnum)aNullableEnum
aNullableString:(nullable NSString *)aNullableString;
aNullableString:(nullable NSString *)aNullableString
aNullableObject:(id)aNullableObject;
@property(nonatomic, strong, nullable) NSNumber *aNullableBool;
@property(nonatomic, strong, nullable) NSNumber *aNullableInt;
@property(nonatomic, strong, nullable) NSNumber *aNullableInt64;
Expand All @@ -89,6 +92,7 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
@property(nonatomic, strong, nullable) NSDictionary<NSString *, id> *nullableMapWithObject;
@property(nonatomic, assign) AnEnum aNullableEnum;
@property(nonatomic, copy, nullable) NSString *aNullableString;
@property(nonatomic, strong) id aNullableObject;
@end

/// A class for testing nested class handling.
Expand Down
Loading