Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@
## 17.3.0

* [swift] Adds `@SwiftClass` annotation to allow choice between `struct` and `class` for data classes.

## 17.2.0

* [dart] Adds implementation for `@ProxyApi`.
Expand Down
7 changes: 7 additions & 0 deletions packages/pigeon/lib/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ class Class extends Node {
Class({
required this.name,
required this.fields,
this.isSwiftClass = false,
this.documentationComments = const <String>[],
});

Expand All @@ -648,6 +649,12 @@ class Class extends Node {
/// All the fields contained in the class.
List<NamedType> fields;

/// Determines whether the defined class should be represented as a struct or
/// a class in Swift generation.
///
/// Defaults to false, which would represent a struct.
bool isSwiftClass;

/// List of documentation comments, separated by line.
///
/// Lines should not include the comment marker itself, but should include any
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 @@ -13,7 +13,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '17.2.0';
const String pigeonVersion = '17.3.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
1 change: 1 addition & 0 deletions packages/pigeon/lib/pigeon_lib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,7 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
_currentClass = Class(
name: node.name.lexeme,
fields: <NamedType>[],
isSwiftClass: _hasMetadata(node.metadata, 'SwiftClass'),
documentationComments:
_documentationCommentsParser(node.documentationComment?.tokens),
);
Expand Down
60 changes: 47 additions & 13 deletions packages/pigeon/lib/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,26 @@ class SwiftGenerator extends StructuredGenerator<SwiftOptions> {
indent, classDefinition.documentationComments, _docCommentSpec,
generatorComments: generatedComments);

indent.write('struct ${classDefinition.name} ');
if (classDefinition.isSwiftClass) {
indent.write('class ${classDefinition.name} ');
} else {
indent.write('struct ${classDefinition.name} ');
}
indent.addScoped('{', '}', () {
getFieldsInSerializationOrder(classDefinition).forEach((NamedType field) {
final Iterable<NamedType> fields =
getFieldsInSerializationOrder(classDefinition);

if (classDefinition.isSwiftClass) {
_writeClassInit(indent, fields.toList());
}

for (final NamedType field in fields) {
addDocumentationComments(
indent, field.documentationComments, _docCommentSpec);
indent.write('var ');
_writeClassField(indent, field);
});
indent.newln();
}

indent.newln();
writeClassDecode(
Expand All @@ -149,6 +164,35 @@ class SwiftGenerator extends StructuredGenerator<SwiftOptions> {
});
}

void _writeClassInit(Indent indent, List<NamedType> fields) {
indent.writeScoped('init(', ')', () {
for (int i = 0; i < fields.length; i++) {
indent.write('');
_writeClassField(indent, fields[i]);
if (i == fields.length - 1) {
indent.newln();
} else {
indent.addln(',');
}
}
}, addTrailingNewline: false);
indent.addScoped(' {', '}', () {
for (final NamedType field in fields) {
_writeClassFieldInit(indent, field);
}
});
}

void _writeClassField(Indent indent, NamedType field) {
indent.add('${field.name}: ${_nullsafeSwiftTypeForDartType(field.type)}');
final String defaultNil = field.type.isNullable ? ' = nil' : '';
indent.add(defaultNil);
}

void _writeClassFieldInit(Indent indent, NamedType field) {
indent.writeln('self.${field.name} = ${field.name}');
}

@override
void writeClassEncode(
SwiftOptions generatorOptions,
Expand Down Expand Up @@ -222,16 +266,6 @@ class SwiftGenerator extends StructuredGenerator<SwiftOptions> {
});
}

void _writeClassField(Indent indent, NamedType field) {
addDocumentationComments(
indent, field.documentationComments, _docCommentSpec);

indent.write(
'var ${field.name}: ${_nullsafeSwiftTypeForDartType(field.type)}');
final String defaultNil = field.type.isNullable ? ' = nil' : '';
indent.addln(defaultNil);
}

@override
void writeApis(
SwiftOptions generatorOptions,
Expand Down
3 changes: 3 additions & 0 deletions packages/pigeon/pigeons/core_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AllTypes {
}

/// A class containing all supported nullable types.
@SwiftClass()
class AllNullableTypes {
AllNullableTypes(
this.aNullableBool,
Expand All @@ -66,6 +67,7 @@ class AllNullableTypes {
this.aNullableEnum,
this.aNullableString,
this.aNullableObject,
this.allNullableTypes,
);

bool? aNullableBool;
Expand All @@ -86,6 +88,7 @@ class AllNullableTypes {
AnEnum? aNullableEnum;
String? aNullableString;
Object? aNullableObject;
AllNullableTypes? allNullableTypes;
}

/// A class for testing nested class handling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,16 @@ public void setANullableObject(@Nullable Object setterArg) {
this.aNullableObject = setterArg;
}

private @Nullable AllNullableTypes allNullableTypes;

public @Nullable AllNullableTypes getAllNullableTypes() {
return allNullableTypes;
}

public void setAllNullableTypes(@Nullable AllNullableTypes setterArg) {
this.allNullableTypes = setterArg;
}

public static final class Builder {

private @Nullable Boolean aNullableBool;
Expand Down Expand Up @@ -743,6 +753,14 @@ public static final class Builder {
return this;
}

private @Nullable AllNullableTypes allNullableTypes;

@CanIgnoreReturnValue
public @NonNull Builder setAllNullableTypes(@Nullable AllNullableTypes setterArg) {
this.allNullableTypes = setterArg;
return this;
}

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

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

Expand Down Expand Up @@ -830,6 +850,11 @@ ArrayList<Object> toList() {
pigeonResult.setANullableString((String) aNullableString);
Object aNullableObject = list.get(15);
pigeonResult.setANullableObject(aNullableObject);
Object allNullableTypes = list.get(16);
pigeonResult.setAllNullableTypes(
(allNullableTypes == null)
? null
: AllNullableTypes.fromList((ArrayList<Object>) allNullableTypes));
return pigeonResult;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ typedef NS_ENUM(NSUInteger, FLTAnEnum) {
nullableMapWithObject:(nullable NSDictionary<NSString *, id> *)nullableMapWithObject
aNullableEnum:(nullable FLTAnEnumBox *)aNullableEnum
aNullableString:(nullable NSString *)aNullableString
aNullableObject:(nullable id)aNullableObject;
aNullableObject:(nullable id)aNullableObject
allNullableTypes:(nullable FLTAllNullableTypes *)allNullableTypes;
@property(nonatomic, strong, nullable) NSNumber *aNullableBool;
@property(nonatomic, strong, nullable) NSNumber *aNullableInt;
@property(nonatomic, strong, nullable) NSNumber *aNullableInt64;
Expand All @@ -101,6 +102,7 @@ typedef NS_ENUM(NSUInteger, FLTAnEnum) {
@property(nonatomic, strong, nullable) FLTAnEnumBox *aNullableEnum;
@property(nonatomic, copy, nullable) NSString *aNullableString;
@property(nonatomic, strong, nullable) id aNullableObject;
@property(nonatomic, strong, nullable) FLTAllNullableTypes *allNullableTypes;
@end

/// A class for testing nested class handling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ + (instancetype)makeWithANullableBool:(nullable NSNumber *)aNullableBool
nullableMapWithObject:(nullable NSDictionary<NSString *, id> *)nullableMapWithObject
aNullableEnum:(nullable FLTAnEnumBox *)aNullableEnum
aNullableString:(nullable NSString *)aNullableString
aNullableObject:(nullable id)aNullableObject {
aNullableObject:(nullable id)aNullableObject
allNullableTypes:(nullable FLTAllNullableTypes *)allNullableTypes {
FLTAllNullableTypes *pigeonResult = [[FLTAllNullableTypes alloc] init];
pigeonResult.aNullableBool = aNullableBool;
pigeonResult.aNullableInt = aNullableInt;
Expand All @@ -178,6 +179,7 @@ + (instancetype)makeWithANullableBool:(nullable NSNumber *)aNullableBool
pigeonResult.aNullableEnum = aNullableEnum;
pigeonResult.aNullableString = aNullableString;
pigeonResult.aNullableObject = aNullableObject;
pigeonResult.allNullableTypes = allNullableTypes;
return pigeonResult;
}
+ (FLTAllNullableTypes *)fromList:(NSArray *)list {
Expand All @@ -203,6 +205,8 @@ + (FLTAllNullableTypes *)fromList:(NSArray *)list {
pigeonResult.aNullableEnum = aNullableEnum;
pigeonResult.aNullableString = GetNullableObjectAtIndex(list, 14);
pigeonResult.aNullableObject = GetNullableObjectAtIndex(list, 15);
pigeonResult.allNullableTypes =
[FLTAllNullableTypes nullableFromList:(GetNullableObjectAtIndex(list, 16))];
return pigeonResult;
}
+ (nullable FLTAllNullableTypes *)nullableFromList:(NSArray *)list {
Expand All @@ -227,6 +231,7 @@ - (NSArray *)toList {
: [NSNumber numberWithInteger:self.aNullableEnum.value]),
self.aNullableString ?: [NSNull null],
self.aNullableObject ?: [NSNull null],
(self.allNullableTypes ? [self.allNullableTypes toList] : [NSNull null]),
];
}
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
nullableMapWithObject:(nullable NSDictionary<NSString *, id> *)nullableMapWithObject
aNullableEnum:(nullable AnEnumBox *)aNullableEnum
aNullableString:(nullable NSString *)aNullableString
aNullableObject:(nullable id)aNullableObject;
aNullableObject:(nullable id)aNullableObject
allNullableTypes:(nullable AllNullableTypes *)allNullableTypes;
@property(nonatomic, strong, nullable) NSNumber *aNullableBool;
@property(nonatomic, strong, nullable) NSNumber *aNullableInt;
@property(nonatomic, strong, nullable) NSNumber *aNullableInt64;
Expand All @@ -101,6 +102,7 @@ typedef NS_ENUM(NSUInteger, AnEnum) {
@property(nonatomic, strong, nullable) AnEnumBox *aNullableEnum;
@property(nonatomic, copy, nullable) NSString *aNullableString;
@property(nonatomic, strong, nullable) id aNullableObject;
@property(nonatomic, strong, nullable) AllNullableTypes *allNullableTypes;
@end

/// A class for testing nested class handling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ + (instancetype)makeWithANullableBool:(nullable NSNumber *)aNullableBool
nullableMapWithObject:(nullable NSDictionary<NSString *, id> *)nullableMapWithObject
aNullableEnum:(nullable AnEnumBox *)aNullableEnum
aNullableString:(nullable NSString *)aNullableString
aNullableObject:(nullable id)aNullableObject {
aNullableObject:(nullable id)aNullableObject
allNullableTypes:(nullable AllNullableTypes *)allNullableTypes {
AllNullableTypes *pigeonResult = [[AllNullableTypes alloc] init];
pigeonResult.aNullableBool = aNullableBool;
pigeonResult.aNullableInt = aNullableInt;
Expand All @@ -178,6 +179,7 @@ + (instancetype)makeWithANullableBool:(nullable NSNumber *)aNullableBool
pigeonResult.aNullableEnum = aNullableEnum;
pigeonResult.aNullableString = aNullableString;
pigeonResult.aNullableObject = aNullableObject;
pigeonResult.allNullableTypes = allNullableTypes;
return pigeonResult;
}
+ (AllNullableTypes *)fromList:(NSArray *)list {
Expand All @@ -203,6 +205,8 @@ + (AllNullableTypes *)fromList:(NSArray *)list {
pigeonResult.aNullableEnum = aNullableEnum;
pigeonResult.aNullableString = GetNullableObjectAtIndex(list, 14);
pigeonResult.aNullableObject = GetNullableObjectAtIndex(list, 15);
pigeonResult.allNullableTypes =
[AllNullableTypes nullableFromList:(GetNullableObjectAtIndex(list, 16))];
return pigeonResult;
}
+ (nullable AllNullableTypes *)nullableFromList:(NSArray *)list {
Expand All @@ -227,6 +231,7 @@ - (NSArray *)toList {
: [NSNumber numberWithInteger:self.aNullableEnum.value]),
self.aNullableString ?: [NSNull null],
self.aNullableObject ?: [NSNull null],
(self.allNullableTypes ? [self.allNullableTypes toList] : [NSNull null]),
];
}
@end
Expand Down
Loading