Skip to content

Commit

Permalink
Implement support for renaming fields in Darwin codegen. (#24154)
Browse files Browse the repository at this point in the history
* Implement support for renaming fields in Darwin codegen.

This adds support for renaming struct, event, and command fields.

Also fixes a bug that was introduced where the getter for newPosition was mis-named.

* Add support for renaming structs and events too.

* Add support for renaming enums and bitmaps as well.

* Add support for renaming enum and bitmap values as well.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 18, 2023
1 parent 207536e commit 1183088
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 112 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
62 changes: 50 additions & 12 deletions src/darwin/Framework/CHIP/templates/MTRBaseClusters.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,70 @@ subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptio
{{#zcl_clusters}}
{{#zcl_enums}}
{{#*inline "enumDef"}}
typedef NS_ENUM({{asUnderlyingZclType name}}, {{objCEnumName clusterName label}}) {
typedef NS_ENUM({{asUnderlyingZclType name}}, {{objCEnumName clusterName enumName}}) {
{{#zcl_enum_items}}
{{objCEnumName ../clusterName ../label}}{{objCEnumItemLabel label}} {{availability ../clusterName enum=(asUpperCamelCase ../label preserveAcronyms=true) enumValue=(objCEnumItemLabel label) deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../../name preserveAcronyms=true) ../label) (objCEnumItemLabel label))}} = {{asHex value 2}},
{{objCEnumName ../clusterName ../enumName}}{{objCEnumItemLabel label}} {{availability ../clusterName enum=../enumName enumValue=(objCEnumItemLabel label) deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../../name preserveAcronyms=true) ../label) (objCEnumItemLabel label))}} = {{asHex value 2}},
{{#*inline "oldNameItemDecl"}}
{{#if oldItemName}}
{{objCEnumName ../clusterName ../enumName}}{{objCEnumItemLabel oldItemName}} {{availability ../clusterName enum=../enumName enumValue=oldItemName deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../../name preserveAcronyms=true) ../label) (objCEnumItemLabel label))}} = {{asHex value 2}},
{{/if}}
{{/inline}}
{{> oldNameItemDecl oldItemName=(oldName ../clusterName enum=../enumName enumValue=(objCEnumItemLabel label))}}
{{/zcl_enum_items}}
}
{{/inline}}
{{> enumDef name=name clusterName=(asUpperCamelCase ../name preserveAcronyms=true) label=label}} {{availability (asUpperCamelCase ../name preserveAcronyms=true) enum=(asUpperCamelCase label preserveAcronyms=true)}};
{{#unless (isStrEqual (asUpperCamelCase ../name preserveAcronyms=true) (compatClusterNameRemapping ../name))}}
{{> enumDef name=name clusterName=(asUpperCamelCase ../name preserveAcronyms=true) enumName=(asUpperCamelCase label preserveAcronyms=true)}} {{availability (asUpperCamelCase ../name preserveAcronyms=true) enum=(asUpperCamelCase label preserveAcronyms=true)}};
{{! Takes the name of the enum to use as enumName. }}
{{#*inline "oldNameDecl"}}

{{> enumDef name=name clusterName=(compatClusterNameRemapping ../name) label=label}} {{availability (compatClusterNameRemapping ../name) enum=(asUpperCamelCase label preserveAcronyms=true) deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../name preserveAcronyms=true) label))}};
{{/unless}}
{{> enumDef name=name clusterName=(compatClusterNameRemapping ../name) enumName=enumName}} {{availability (compatClusterNameRemapping ../name) enum=enumName deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../name preserveAcronyms=true) label))}};
{{/inline}}
{{! Takes the old name of the enum, if any, as oldEnumName. }}
{{#*inline "oldNameCheck"}}
{{#if (or oldEnumName
(hasOldName (asUpperCamelCase ../name preserveAcronyms=true)))}}
{{#if oldEnumName}}
{{> oldNameDecl enumName=oldEnumName}}
{{else}}
{{> oldNameDecl enumName=(asUpperCamelCase label preserveAcronyms=true)}}
{{/if}}
{{/if}}
{{/inline}}
{{> oldNameCheck oldEnumName=(oldName (asUpperCamelCase ../name preserveAcronyms=true) enum=(asUpperCamelCase label preserveAcronyms=true))}}

{{/zcl_enums}}
{{#zcl_bitmaps}}
{{#*inline "bitmapDef"}}
typedef NS_OPTIONS({{asUnderlyingZclType name}}, {{objCEnumName clusterName label}}) {
typedef NS_OPTIONS({{asUnderlyingZclType name}}, {{objCEnumName clusterName bitmapName}}) {
{{#zcl_bitmap_items}}
{{objCEnumName ../clusterName ../label}}{{objCEnumItemLabel label}} {{availability ../clusterName bitmap=(asUpperCamelCase ../label preserveAcronyms=true) bitmapValue=(objCEnumItemLabel label) deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../../name preserveAcronyms=true) ../label) (objCEnumItemLabel label))}} = {{asHex mask}},
{{objCEnumName ../clusterName ../bitmapName}}{{objCEnumItemLabel label}} {{availability ../clusterName bitmap=../bitmapName bitmapValue=(objCEnumItemLabel label) deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../../name preserveAcronyms=true) ../label) (objCEnumItemLabel label))}} = {{asHex mask}},
{{#*inline "oldNameItemDecl"}}
{{#if oldItemName}}
{{objCEnumName ../clusterName ../bitmapName}}{{objCEnumItemLabel oldItemName}} {{availability ../clusterName bitmap=../enumName bitmapValue=oldItemName deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../../name preserveAcronyms=true) ../label) (objCEnumItemLabel label))}} = {{asHex value 2}},
{{/if}}
{{/inline}}
{{> oldNameItemDecl oldItemName=(oldName ../clusterName bitmap=../bitmapName bitmapValue=(objCEnumItemLabel label))}}
{{/zcl_bitmap_items}}
}
{{/inline}}
{{> bitmapDef name=name clusterName=(asUpperCamelCase ../name preserveAcronyms=true) label=label}} {{availability (asUpperCamelCase ../name preserveAcronyms=true) bitmap=(asUpperCamelCase label preserveAcronyms=true)}};
{{#unless (isStrEqual (asUpperCamelCase ../name preserveAcronyms=true) (compatClusterNameRemapping ../name))}}
{{> bitmapDef name=name clusterName=(asUpperCamelCase ../name preserveAcronyms=true) bitmapName=(asUpperCamelCase label preserveAcronyms=true)}} {{availability (asUpperCamelCase ../name preserveAcronyms=true) bitmap=(asUpperCamelCase label preserveAcronyms=true)}};
{{! Takes the name of the bitmap to use as bitmapName. }}
{{#*inline "oldNameDecl"}}

{{> bitmapDef name=name clusterName=(compatClusterNameRemapping ../name) label=label}} {{availability (compatClusterNameRemapping ../name) bitmap=(asUpperCamelCase label preserveAcronyms=true) deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../name preserveAcronyms=true) label))}};
{{/unless}}
{{> bitmapDef name=name clusterName=(compatClusterNameRemapping ../name) bitmapName=bitmapName}} {{availability (compatClusterNameRemapping ../name) bitmap=bitmapName deprecationMessage=(concat "Please use " (objCEnumName (asUpperCamelCase ../name preserveAcronyms=true) label))}};
{{/inline}}
{{! Takes the old name of the bitmap, if any, as oldBitmapName. }}
{{#*inline "oldNameCheck"}}
{{#if (or oldBitmapName
(hasOldName (asUpperCamelCase ../name preserveAcronyms=true)))}}
{{#if oldBitmapName}}
{{> oldNameDecl bitmapName=oldBitmapName}}
{{else}}
{{> oldNameDecl bitmapName=(asUpperCamelCase label preserveAcronyms=true)}}
{{/if}}
{{/if}}
{{/inline}}
{{> oldNameCheck oldBitmapName=(oldName (asUpperCamelCase ../name preserveAcronyms=true) bitmap=(asUpperCamelCase label preserveAcronyms=true))}}


{{/zcl_bitmaps}}
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}}Params (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}}Params (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
66 changes: 41 additions & 25 deletions src/darwin/Framework/CHIP/templates/MTRStructsObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,35 @@ 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}}

{{> interfaceImpl interfaceName=(concat "MTR" (asUpperCamelCase parent.name preserveAcronyms=true) "Cluster" (asUpperCamelCase name preserveAcronyms=true))}}

{{!Backwards compat for now: Add a DeviceType thing that is API-compatible with DeviceTypeStruct. }}
{{#if (isStrEqual (asUpperCamelCase parent.name) "Descriptor")}}
{{#if (isStrEqual (asUpperCamelCase name) "DeviceTypeStruct")}}
{{> interfaceImpl interfaceName="MTRDescriptorClusterDeviceType"}}
{{/if}}
{{else}}
{{#unless (isStrEqual (asUpperCamelCase parent.name preserveAcronyms=true) (compatClusterNameRemapping parent.name))}}
@implementation MTR{{compatClusterNameRemapping parent.name}}Cluster{{asUpperCamelCase name}} : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}
{{! Takes the name of the struct to use as structName. }}
{{#*inline "oldNameImpl"}}
@implementation MTR{{compatClusterNameRemapping parent.name}}Cluster{{structName}} : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}
@end
{{/unless}}
{{/inline}}
{{! Takes the old name of the struct, if any, as oldStructName. }}
{{#*inline "oldNameCheck"}}
{{#if (or oldStructName
(hasOldName (asUpperCamelCase parent.name preserveAcronyms=true)))}}
{{#if oldStructName}}
{{> oldNameImpl structName=oldStructName}}
{{else}}
{{> oldNameImpl structName=(asUpperCamelCase name preserveAcronyms=true)}}
{{/if}}
{{/if}}
{{/inline}}
{{> oldNameCheck oldStructName=(oldName (asUpperCamelCase parent.name preserveAcronyms=true) struct=(asUpperCamelCase name preserveAcronyms=true))}}

{{/zcl_structs}}

Expand Down Expand Up @@ -83,27 +95,31 @@ 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))}}
{{! Takes the name of the event to use as eventName. }}
{{#*inline "oldNameDecl"}}

@implementation MTR{{compatClusterNameRemapping parent.name}}Cluster{{asUpperCamelCase name}}Event : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Event
@implementation MTR{{compatClusterNameRemapping parent.name}}Cluster{{eventName}}Event : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Event
@end
{{/unless}}
{{/inline}}
{{! Takes the old name of the event, if any, as oldEventName. }}
{{#*inline "oldNameCheck"}}
{{#if (or oldEventName
(hasOldName (asUpperCamelCase parent.name preserveAcronyms=true)))}}
{{#if oldEventName}}
{{> oldNameDecl eventName=oldEventName}}
{{else}}
{{> oldNameDecl eventName=(asUpperCamelCase name preserveAcronyms=true)}}
{{/if}}
{{/if}}
{{/inline}}
{{> oldNameCheck oldEventName=(oldName (asUpperCamelCase parent.name preserveAcronyms=true) event=(asUpperCamelCase name preserveAcronyms=true))}}

{{/zcl_events}}

Expand Down
Loading

0 comments on commit 1183088

Please sign in to comment.