Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Adds non-null object fields
  • Loading branch information
ailtonvivaz committed Mar 12, 2022
commit 9a0a3485349f34e096bb4dc86ad1c141e90ef68e
34 changes: 26 additions & 8 deletions packages/pigeon/lib/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -470,15 +470,23 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
'final Map<Object$nullTag, Object$nullTag> pigeonMap = <Object$nullTag, Object$nullTag>{};',
);
for (final NamedType field in klass.fields) {
final String nullsafe = field.type.isNullable ? '?' : '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere else in the code we use isNullSafe to declare operators, then switch on wether something is nullable or not. That's reversed here which is a bit harder to follow.

The name "nullsafe" isn't helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made some changes about it. Could be that?

indent.write('pigeonMap[\'${field.name}\'] = ');
if (customClassNames.contains(field.type.baseName)) {
indent.addln(
'${field.name} == null ? null : ${field.name}$unwrapOperator.encode();',
);
if (opt.isNullSafe) {
indent.addln('${field.name}$nullsafe.encode();');
} else {
indent.addln(
'${field.name} == null ? null : ${field.name}.encode();');
}
} else if (customEnumNames.contains(field.type.baseName)) {
indent.addln(
'${field.name} == null ? null : ${field.name}$unwrapOperator.index;',
);
if (opt.isNullSafe) {
indent.addln('${field.name}$nullsafe.index;');
} else {
indent.addln(
'${field.name} == null ? null : ${field.name}$unwrapOperator.index;',
);
}
} else {
indent.addln('${field.name};');
}
Expand All @@ -490,15 +498,25 @@ void generateDart(DartOptions opt, Root root, StringSink sink) {
void writeDecode() {
void writeValueDecode(NamedType field) {
if (customClassNames.contains(field.type.baseName)) {
indent.format('''
if (field.type.isNullable) {
indent.format('''
pigeonMap['${field.name}'] != null
\t\t? ${field.type.baseName}.decode(pigeonMap['${field.name}']$unwrapOperator)
\t\t: null''', leadingSpace: false, trailingNewline: false);
} else {
indent.add(
'${field.type.baseName}.decode(pigeonMap[\'${field.name}\']$unwrapOperator)');
}
} else if (customEnumNames.contains(field.type.baseName)) {
indent.format('''
if (field.type.isNullable) {
indent.format('''
pigeonMap['${field.name}'] != null
\t\t? ${field.type.baseName}.values[pigeonMap['${field.name}']$unwrapOperator as int]
\t\t: null''', leadingSpace: false, trailingNewline: false);
} else {
indent.add(
'${field.type.baseName}.values[pigeonMap[\'${field.name}\']$unwrapOperator as int]');
}
} else if (field.type.typeArguments.isNotEmpty) {
final String genericType =
_makeGenericTypeArguments(field.type, nullTag);
Expand Down
15 changes: 14 additions & 1 deletion packages/pigeon/pigeons/non_null_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,24 @@ class SearchRequest {
String query;
}

enum SearchReplyType {
success,
error,
}

class SearchReply {
SearchReply(this.result, this.error, this.indices);
SearchReply(
this.result,
this.error,
this.indices,
this.request,
this.type,
);
String result;
String error;
List<int?> indices;
SearchRequest request;
SearchReplyType type;
}

@HostApi()
Expand Down
167 changes: 163 additions & 4 deletions packages/pigeon/test/dart_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,168 @@ void main() {
);
});

test('nested null class nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Input',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'String',
isNullable: true,
),
name: 'input',
offset: null)
],
),
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: true,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested?.encode()',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: pigeonMap[\'nested\'] != null ? Input.decode(pigeonMap[\'nested\']!) : null',
),
);
});

test('nested non-null class nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Input',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'String',
isNullable: true,
),
name: 'input',
offset: null)
],
),
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: false,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested.encode()',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: Input.decode(pigeonMap[\'nested\']!)',
),
);
});

test('nested null enum nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: true,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[
Enum(
name: 'Input',
members: <String>['A', 'B'],
)
]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested?.index',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: pigeonMap[\'nested\'] != null ? Input.values[pigeonMap[\'nested\']! as int] : null',
),
);
});

test('nested non-null enum nullsafe', () {
final Root root = Root(apis: <Api>[], classes: <Class>[
Class(
name: 'Nested',
fields: <NamedType>[
NamedType(
type: const TypeDeclaration(
baseName: 'Input',
isNullable: false,
),
name: 'nested',
offset: null)
],
)
], enums: <Enum>[
Enum(
name: 'Input',
members: <String>['A', 'B'],
)
]);
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'nested\'] = nested.index',
),
);
expect(
code.replaceAll('\n', ' ').replaceAll(' ', ''),
contains(
'nested: Input.values[pigeonMap[\'nested\']! as int]',
),
);
});

test('flutterapi', () {
final Root root = Root(apis: <Api>[
Api(name: 'Api', location: ApiLocation.flutter, methods: <Method>[
Expand Down Expand Up @@ -443,10 +605,7 @@ void main() {
final StringBuffer sink = StringBuffer();
generateDart(const DartOptions(isNullSafe: true), root, sink);
final String code = sink.toString();
expect(
code,
contains(
'pigeonMap[\'enum1\'] = enum1 == null ? null : enum1!.index;'));
expect(code, contains('pigeonMap[\'enum1\'] = enum1?.index;'));
expect(code, contains('? Enum.values[pigeonMap[\'enum1\']! as int]'));
expect(code, contains('EnumClass doSomething(EnumClass arg0);'));
});
Expand Down