Skip to content

Commit

Permalink
Implement init methods for Darwin structs (#11986)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 10, 2023
1 parent 69acc4b commit 1190024
Show file tree
Hide file tree
Showing 10 changed files with 5,029 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

#import "CHIPCommandPayloadsObjc.h"

NS_ASSUME_NONNULL_BEGIN

{{#zcl_clusters}}
{{#zcl_commands}}
@implementation CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Payload
- (instancetype)init
{
if (self = [super init]) {
{{#zcl_command_arguments}}
{{>init_struct_member label=label type=type cluster=parent.parent.name}}
{{/zcl_command_arguments}}
}
return self;
}
@end

{{/zcl_commands}}
{{/zcl_clusters}}

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#import <Foundation/Foundation.h>
#import <CHIP/CHIPStructsObjc.h>

NS_ASSUME_NONNULL_BEGIN

{{#zcl_clusters}}
{{#zcl_commands}}
@interface CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Payload : NSObject
Expand All @@ -14,8 +16,12 @@
{{#zcl_command_arguments}}
@property (strong, nonatomic{{#unless (isStrEqual (asGetterName label) (asStructPropertyName label))}}, getter={{asGetterName label}}{{/unless}}) {{asObjectiveCType type parent.parent.name}} {{asStructPropertyName label}};
{{/zcl_command_arguments}}
@end
- (instancetype)init;
@end

{{/zcl_commands}}
{{/zcl_clusters}}

NS_ASSUME_NONNULL_END

#endif /* CHIP_COMMAND_PAYLOADS_H */
13 changes: 13 additions & 0 deletions src/darwin/Framework/CHIP/templates/CHIPStructsObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

#import "CHIPStructsObjc.h"

NS_ASSUME_NONNULL_BEGIN

{{#zcl_clusters}}
{{#zcl_structs}}
@implementation CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}
- (instancetype)init
{
if (self = [super init]) {
{{#zcl_struct_items}}
{{>init_struct_member label=label type=type cluster=parent.parent.name}}
{{/zcl_struct_items}}
}
return self;
}
@end

{{/zcl_structs}}
{{/zcl_clusters}}

NS_ASSUME_NONNULL_END
5 changes: 5 additions & 0 deletions src/darwin/Framework/CHIP/templates/CHIPStructsObjc.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

{{#zcl_clusters}}
{{#zcl_structs}}
@interface CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}} : NSObject
Expand All @@ -13,9 +15,12 @@
{{#zcl_struct_items}}
@property (strong, nonatomic{{#unless (isStrEqual (asGetterName label) (asStructPropertyName label))}}, getter={{asGetterName label}}{{/unless}}) {{asObjectiveCType type parent.parent.name}} {{asStructPropertyName label}};
{{/zcl_struct_items}}
- (instancetype)init;
@end

{{/zcl_structs}}
{{/zcl_clusters}}

NS_ASSUME_NONNULL_END

#endif /* CHIP_STRUCTS_H */
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{#*inline "memberName"}}_{{asStructPropertyName label}}{{/inline}}
{{#if isOptional}}
{{>memberName}} = nil;
{{else if isNullable}}
{{>memberName}} = nil;
{{else if isArray}}
{{>memberName}} = [NSArray array];
{{else if (isOctetString type)}}
{{>memberName}} = [NSData data];
{{else if (isCharString type)}}
{{>memberName}} = @"";
{{else}}
{{! Our memberName inline partial won't work inside the async if_is_struct }}
{{#if_is_struct type}}
_{{asStructPropertyName label}} = [{{asObjectiveCClass type cluster}} new];
{{else}}
_{{asStructPropertyName label}} = @(0);
{{/if_is_struct}}
{{/if}}
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
{
"name": "check_test_value",
"path": "partials/check_test_value.zapt"
},
{
"name": "init_struct_member",
"path": "partials/init_struct_member.zapt"
}
],
"templates": [
Expand Down
Loading

0 comments on commit 1190024

Please sign in to comment.