Skip to content

Commit

Permalink
Implement support for renaming fields in Darwin codegen.
Browse files Browse the repository at this point in the history
This adds support for renaming struct, event, and command fields.
  • Loading branch information
bzbarsky-apple committed Dec 20, 2022
1 parent fb18595 commit 23cccc6
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/zap_regeneration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:

runs-on: ubuntu-20.04
container:
image: connectedhomeip/chip-build:0.6.24
image: connectedhomeip/chip-build:0.6.25
defaults:
run:
shell: sh
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/zap_templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

runs-on: ubuntu-20.04
container:
image: connectedhomeip/chip-build:0.6.24
image: connectedhomeip/chip-build:0.6.25
defaults:
run:
shell: sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,55 @@ NS_ASSUME_NONNULL_BEGIN
NSString *descriptionString = [NSString stringWithFormat:@"<%@: {{#zcl_command_arguments}}{{asStructPropertyName label}}:%@; {{/zcl_command_arguments}}>", NSStringFromClass([self class]) {{#zcl_command_arguments}},{{#if isArray}}_{{asStructPropertyName label}}{{else if (isOctetString type)}}[_{{asStructPropertyName label}} base64EncodedStringWithOptions:0]{{else}}_{{asStructPropertyName label}}{{/if}}{{/zcl_command_arguments}}];
return descriptionString;
}
{{#zcl_command_arguments}}
{{#if (and includeRenamedProperties
(hasOldName ../cluster command=../command commandField=(asStructPropertyName label)))}}

{{> renamed_struct_field_impl cluster=parent.parent.name type=type newName=label oldName=(oldName ../cluster command=../command commandField=(asStructPropertyName label))}}
{{/if}}
{{/zcl_command_arguments}}

@end
{{/unless}}
{{/inline}}
{{#*inline "oldNameImpl"}}

@implementation MTR{{cluster}}Cluster{{command}}Params
{{#zcl_command_arguments}}
{{#if (hasOldName (asUpperCamelCase parent.parent.name preserveAcronyms=true) command=(asUpperCamelCase parent.name preserveAcronyms=true) commandField=(asStructPropertyName label))}}

{{> renamed_struct_field_impl cluster=parent.parent.name type=type newName=label oldName=(oldName (asUpperCamelCase parent.parent.name preserveAcronyms=true) command=(asUpperCamelCase parent.name preserveAcronyms=true) commandField=(asStructPropertyName label))}}
{{/if}}
{{/zcl_command_arguments}}
@end
{{/inline}}
{{#if (not (wasRemoved (asUpperCamelCase parent.name preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true)))}}
{{> completeImpl cluster=(asUpperCamelCase parent.name preserveAcronyms=true)
command=(asUpperCamelCase name preserveAcronyms=true)}}
command=(asUpperCamelCase name preserveAcronyms=true)
includeRenamedProperties=false}}
{{#if (or (not (isStrEqual (asUpperCamelCase parent.name preserveAcronyms=true) (compatClusterNameRemapping parent.name)))
(not (isStrEqual (asUpperCamelCase name preserveAcronyms=true) (compatCommandNameRemapping parent.name name))))}}
{{> oldNameImpl cluster=(compatClusterNameRemapping parent.name)
command=(compatCommandNameRemapping parent.name name)}}
{{else if (hasRenamedFields (asUpperCamelCase parent.name preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true))}}
{{#*inline "deprecatedImpl"}}

@implementation MTR{{cluster}}Cluster{{command}} (Deprecated)
{{#zcl_command_arguments}}
{{#if (hasOldName ../cluster command=../command commandField=(asStructPropertyName label))}}

{{> renamed_struct_field_impl cluster=parent.parent.name type=type newName=label oldName=(oldName ../cluster command=../command commandField=(asStructPropertyName label))}}
{{/if}}
{{/zcl_command_arguments}}
@end
{{/inline}}
{{> deprecatedImpl cluster=(asUpperCamelCase parent.name preserveAcronyms=true)
command=(asUpperCamelCase name preserveAcronyms=true)}}
{{/if}}
{{else}}
{{> completeImpl cluster=(compatClusterNameRemapping parent.name)
command=(compatCommandNameRemapping parent.name name)}}
command=(compatCommandNameRemapping parent.name name)
includeRenamedProperties=true}}
{{/if}}
{{/zcl_commands}}
{{/zcl_clusters}}
Expand Down
44 changes: 39 additions & 5 deletions src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ NS_ASSUME_NONNULL_BEGIN
@interface MTR{{cluster}}Cluster{{command}}Params : NSObject <NSCopying>
{{#zcl_command_arguments}}

{{! Override the getter name because some of our properties start with things
like "new" or "init" }}
@property (nonatomic, copy{{#unless (isStrEqual (asGetterName label) (asStructPropertyName label))}}, getter={{asGetterName label}}{{/unless}}) {{asObjectiveCType type parent.parent.name}} {{asStructPropertyName label}} {{availability ../cluster command=../command commandField=(asStructPropertyName label)}};
{{> struct_field_decl cluster=parent.parent.name type=type label=label}} {{availability ../cluster command=../command commandField=(asStructPropertyName label)}};
{{#*inline "oldNameFieldDecl"}}

{{> struct_field_decl cluster=parent.parent.name type=type label=commandField}} {{availability ../cluster command=../command commandField=commandField deprecationMessage=(concat "Please use " (asStructPropertyName label))}};
{{/inline}}
{{#if (and includeRenamedProperties
(hasOldName ../cluster command=../command commandField=(asStructPropertyName label)))}}
{{> oldNameFieldDecl commandField=(oldName ../cluster command=../command commandField=(asStructPropertyName label))}}
{{/if}}
{{/zcl_command_arguments}}
{{#if (isStrEqual source "client")}}
/**
Expand Down Expand Up @@ -60,20 +66,48 @@ NS_ASSUME_NONNULL_BEGIN

{{availability cluster command=command deprecationMessage=(concat "Please use MTR" (asUpperCamelCase parent.name preserveAcronyms=true) "Cluster" (asUpperCamelCase name preserveAcronyms=true) "Params")}}
@interface MTR{{cluster}}Cluster{{command}}Params : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name}}Params
{{#zcl_command_arguments}}
{{#*inline "oldNameFieldDecl"}}

{{> struct_field_decl cluster=parent.parent.name type=type label=commandField}} {{availability ../cluster command=../command commandField=commandField deprecationMessage=(concat "Please use " (asStructPropertyName label))}};
{{/inline}}
{{#if (hasOldName (asUpperCamelCase parent.parent.name preserveAcronyms=true) command=(asUpperCamelCase parent.name preserveAcronyms=true) commandField=(asStructPropertyName label))}}
{{> oldNameFieldDecl commandField=(oldName (asUpperCamelCase parent.parent.name preserveAcronyms=true) command=(asUpperCamelCase parent.name preserveAcronyms=true) commandField=(asStructPropertyName label))}}
{{/if}}
{{/zcl_command_arguments}}
@end

{{/inline}}
{{#if (not (wasRemoved (asUpperCamelCase parent.name preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true)))}}
{{> completeDecl cluster=(asUpperCamelCase parent.name preserveAcronyms=true)
command=(asUpperCamelCase name preserveAcronyms=true)}}
command=(asUpperCamelCase name preserveAcronyms=true)
includeRenamedProperties=false}}
{{#if (or (not (isStrEqual (asUpperCamelCase parent.name preserveAcronyms=true) (compatClusterNameRemapping parent.name)))
(not (isStrEqual (asUpperCamelCase name preserveAcronyms=true) (compatCommandNameRemapping parent.name name))))}}
{{> oldNameDecl cluster=(compatClusterNameRemapping parent.name)
command=(compatCommandNameRemapping parent.name name)}}
{{else if (hasRenamedFields (asUpperCamelCase parent.name preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true))}}
{{#*inline "deprecatedDecl"}}

@interface MTR{{cluster}}Cluster{{command}} (Deprecated)
{{#zcl_command_arguments}}
{{#*inline "oldNameFieldDecl"}}

{{> struct_field_decl cluster=parent.parent.name type=type label=commandField}} {{availability ../cluster command=../command commandField=commandField deprecationMessage=(concat "Please use " (asStructPropertyName label))}};
{{/inline}}
{{#if (hasOldName ../cluster command=../command commandField=(asStructPropertyName label))}}
{{> oldNameFieldDecl commandField=(oldName ../cluster command=../command commandField=(asStructPropertyName label))}}
{{/if}}
{{/zcl_command_arguments}}
@end
{{/inline}}
{{> deprecatedDecl cluster=(asUpperCamelCase parent.name preserveAcronyms=true)
command=(asUpperCamelCase name preserveAcronyms=true)}}
{{/if}}
{{else}}
{{> completeDecl cluster=(compatClusterNameRemapping parent.name)
command=(compatCommandNameRemapping parent.name name)}}
command=(compatCommandNameRemapping parent.name name)
includeRenamedProperties=true}}
{{/if}}
{{/zcl_commands}}
{{/zcl_clusters}}
Expand Down
23 changes: 10 additions & 13 deletions src/darwin/Framework/CHIP/templates/MTRStructsObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ NS_ASSUME_NONNULL_BEGIN
NSString *descriptionString = [NSString stringWithFormat:@"<%@: {{#zcl_struct_items}}{{asStructPropertyName label}}:%@; {{/zcl_struct_items}}>", NSStringFromClass([self class]){{#zcl_struct_items}},{{#if isArray}}_{{asStructPropertyName label}}{{else if (isOctetString type)}}[_{{asStructPropertyName label}} base64EncodedStringWithOptions:0]{{else}}_{{asStructPropertyName label}}{{/if}}{{/zcl_struct_items}}];
return descriptionString;
}
{{#zcl_struct_items}}
{{#if (hasOldName (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(asStructPropertyName label))}}

{{> renamed_struct_field_impl cluster=../../name type=type newName=label oldName=(oldName (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(asStructPropertyName label))}}
{{/if}}
{{/zcl_struct_items}}

@end
{{/inline}}
Expand Down Expand Up @@ -83,21 +89,12 @@ NS_ASSUME_NONNULL_BEGIN
return descriptionString;
}

{{#if (isStrEqual (asUpperCamelCase parent.name preserveAcronyms=true) "Switch")}}
{{#if (isStrEqual (asUpperCamelCase name preserveAcronyms=true) "MultiPressComplete")}}
{{! Workaround for the name being mis-spelled in XML previously }}
- (void)setNewPosition:(NSNumber * _Nonnull)newPosition
{
self.previousPosition = newPosition;
}

- (NSNumber * _Nonnull)newPosition
{
return self.previousPosition;
}
{{#zcl_event_fields}}
{{#if (hasOldName (asUpperCamelCase ../parent.name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name))}}

{{> renamed_struct_field_impl cluster=../parent.name type=type newName=name oldName=(oldName (asUpperCamelCase ../parent.name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name))}}
{{/if}}
{{/if}}
{{/zcl_event_fields}}
@end
{{#unless (isStrEqual (asUpperCamelCase parent.name preserveAcronyms=true) (compatClusterNameRemapping parent.name))}}

Expand Down
18 changes: 7 additions & 11 deletions src/darwin/Framework/CHIP/templates/MTRStructsObjc.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ NS_ASSUME_NONNULL_BEGIN
{{#zcl_clusters}}
{{#zcl_structs}}
{{#*inline "interfaceDecl"}}
{{! Override the getter name because some of our properties start with things
like "new" or "init" }}
{{#zcl_struct_items}}
@property (nonatomic, copy{{#unless (isStrEqual (asGetterName label) (asStructPropertyName label))}}, getter={{asGetterName label}}{{/unless}}) {{asObjectiveCType type parent.parent.name}} {{asStructPropertyName label}} {{availability (asUpperCamelCase ../../name preserveAcronyms=true) struct=../struct structField=(asStructPropertyName label) deprecationMessage=(concat "Please use MTR" (asUpperCamelCase ../../name preserveAcronyms=true) "Cluster" (asUpperCamelCase ../name preserveAcronyms=true))}};
{{> struct_field_decl cluster=parent.parent.name type=type label=label}} {{availability (asUpperCamelCase ../../name preserveAcronyms=true) struct=../struct structField=(asStructPropertyName label) deprecationMessage=(concat "Please use MTR" (asUpperCamelCase ../../name preserveAcronyms=true) "Cluster" (asUpperCamelCase ../name preserveAcronyms=true))}};
{{#if (hasOldName (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(asStructPropertyName label))}}
{{> struct_field_decl cluster=../../name type=type label=(oldName (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(asStructPropertyName label))}} {{availability (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(oldName (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(asStructPropertyName name)) deprecationMessage=(concat "Please use " (asStructPropertyName name))}};
{{/if}}
{{/zcl_struct_items}}
{{/inline}}
{{availability (asUpperCamelCase parent.name preserveAcronyms=true) struct=(asUpperCamelCase name preserveAcronyms=true)}}
Expand Down Expand Up @@ -39,14 +40,9 @@ NS_ASSUME_NONNULL_BEGIN
{{availability (asUpperCamelCase parent.name preserveAcronyms=true) event=(asUpperCamelCase name preserveAcronyms=true)}}
@interface MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Event : NSObject <NSCopying>
{{#zcl_event_fields}}
@property (nonatomic, copy{{#unless (isStrEqual (asGetterName name) (asStructPropertyName name))}}, getter={{asGetterName name}}{{/unless}}) {{asObjectiveCType type parent.parent.name}} {{asStructPropertyName name}} {{availability (asUpperCamelCase ../../name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name)}};
{{#if (isStrEqual (asUpperCamelCase ../parent.name preserveAcronyms=true) "Switch")}}
{{#if (isStrEqual (asUpperCamelCase ../name preserveAcronyms=true) "MultiPressComplete")}}
{{#if (isStrEqual (asStructPropertyName name) "previousPosition")}}
{{! Workaround for the name being mis-spelled in XML previously }}
@property (nonatomic, copy) NSNumber * _Nonnull newPosition {{availability (asUpperCamelCase ../../name preserveAcronyms=true) event=(asUpperCamelCase ../name) eventField="newPosition" deprecationMessage="Please use previousPosition"}};
{{/if}}
{{/if}}
{{> struct_field_decl cluster=parent.parent.name type=type label=name}} {{availability (asUpperCamelCase ../../name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name)}};
{{#if (hasOldName (asUpperCamelCase ../parent.name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name))}}
{{> struct_field_decl cluster=parent.parent.name type=type label=(oldName (asUpperCamelCase ../parent.name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name))}} {{availability (asUpperCamelCase ../../name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(oldName (asUpperCamelCase ../parent.name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name)) deprecationMessage=(concat "Please use " (asStructPropertyName name))}};
{{/if}}
{{/zcl_event_fields}}
@end
Expand Down
4 changes: 4 additions & 0 deletions src/darwin/Framework/CHIP/templates/availability.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5179,3 +5179,7 @@
PIROccupiedToUnoccupiedDelay: PirOccupiedToUnoccupiedDelay
PIRUnoccupiedToOccupiedDelay: PirUnoccupiedToOccupiedDelay
PIRUnoccupiedToOccupiedThreshold: PirUnoccupiedToOccupiedThreshold
event fields:
Switch:
MultiPressComplete:
previousPosition: newPosition
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- (void)set{{asUpperCamelCase oldName}}:({{asObjectiveCType type cluster}}){{asStructPropertyName oldName}}
{
self.{{asStructPropertyName newName}} = {{asStructPropertyName oldName}};
}

- ({{asObjectiveCType type cluster}}){{asGetterName oldName}}
{
return self.{{asStructPropertyName newName}};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{! Override the getter name because some of our properties start with things
like "new" or "init" }}
@property (nonatomic, copy{{#unless (isStrEqual (asGetterName label) (asStructPropertyName label))}}, getter={{asGetterName label}}{{/unless}}) {{asObjectiveCType type cluster}} {{asStructPropertyName label}} {{! Caller provides availability~}}
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
{
"name": "attribute_data_callback_name",
"path": "partials/attribute_data_callback_name.zapt"
},
{
"name": "struct_field_decl",
"path": "partials/struct_field_decl.zapt"
},
{
"name": "renamed_struct_field_impl",
"path": "partials/renamed_struct_field_impl.zapt"
}
],
"templates": [
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/zap-generated/MTRStructsObjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/darwin/Framework/CHIP/zap-generated/MTRStructsObjc.mm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 23cccc6

Please sign in to comment.