Skip to content

Commit

Permalink
Make the Darwin compat story for struct/event renames better (#28012)
Browse files Browse the repository at this point in the history
* Refactor struct/event codegen in Darwin a bit.

This makes the cluster and struct/event names into inline partial parameters.
This changeset has been tested to not modify the generated code at all.

* Make our renamed struct/event codegen more backwards compatible.

When we just moved all the members to the super-class, it left the subclass in a
situation where none of its properties are available in the release where the
subclass itself is available.

This change restores those properties on the subclasses.

The @dynamic in implementations is needed to avoid compiler errors.
  • Loading branch information
bzbarsky-apple authored Jul 18, 2023
1 parent e6f610b commit 4554b38
Show file tree
Hide file tree
Showing 4 changed files with 726 additions and 23 deletions.
36 changes: 28 additions & 8 deletions src/darwin/Framework/CHIP/templates/MTRStructsObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,18 @@ NS_ASSUME_NONNULL_BEGIN

{{! Takes the name of the struct to use as structName. }}
{{#*inline "oldNameImpl"}}
{{#if (isSupported (compatClusterNameRemapping parent.name) struct=structName)}}
@implementation MTR{{compatClusterNameRemapping parent.name}}Cluster{{structName}} : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}
{{#if (isSupported cluster struct=structName)}}
@implementation MTR{{cluster}}Cluster{{structName}} : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}
{{#zcl_struct_items}}
{{#if (isSupported ../cluster struct=../structName structField=(asStructPropertyName label))}}
@dynamic {{asStructPropertyName label}};
{{/if}}
{{#if (hasOldName ../cluster struct=../structName structField=(asStructPropertyName label))}}
{{#if (isSupported ../cluster struct=../structName structField=(oldName ../cluster struct=../structName structField=(asStructPropertyName label)))}}
@dynamic {{oldName ../cluster struct=../structName structField=(asStructPropertyName label)}};
{{/if}}
{{/if}}
{{/zcl_struct_items}}
@end
{{/if}}
{{/inline}}
Expand All @@ -73,9 +83,9 @@ NS_ASSUME_NONNULL_BEGIN
{{#if (or oldStructName
(hasOldName (asUpperCamelCase parent.name preserveAcronyms=true)))}}
{{#if oldStructName}}
{{> oldNameImpl structName=oldStructName}}
{{> oldNameImpl cluster=(compatClusterNameRemapping parent.name) structName=oldStructName}}
{{else}}
{{> oldNameImpl structName=(asUpperCamelCase name preserveAcronyms=true)}}
{{> oldNameImpl cluster=(compatClusterNameRemapping parent.name) structName=(asUpperCamelCase name preserveAcronyms=true)}}
{{/if}}
{{/if}}
{{/inline}}
Expand Down Expand Up @@ -136,9 +146,19 @@ NS_ASSUME_NONNULL_BEGIN
{{/if}}
{{! Takes the name of the event to use as eventName. }}
{{#*inline "oldNameImpl"}}
{{#if (isSupported (compatClusterNameRemapping parent.name) event=eventName)}}
{{#if (isSupported cluster event=eventName)}}

@implementation MTR{{compatClusterNameRemapping parent.name}}Cluster{{eventName}}Event : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Event
@implementation MTR{{cluster}}Cluster{{eventName}}Event : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Event
{{#zcl_event_fields}}
{{#if (isSupported ../cluster event=../eventName eventField=(asStructPropertyName name))}}
@dynamic {{asStructPropertyName name}};
{{/if}}
{{#if (hasOldName ../cluster event=../eventName eventField=(asStructPropertyName name))}}
{{#if (isSupported ../cluster event=../eventName eventField=(oldName ../cluster event=../eventName eventField=(asStructPropertyName name)))}}
@dynamic {{oldName ../cluster event=../event eventField=(asStructPropertyName name)}};
{{/if}}
{{/if}}
{{/zcl_event_fields}}
@end
{{/if}}
{{/inline}}
Expand All @@ -147,9 +167,9 @@ NS_ASSUME_NONNULL_BEGIN
{{#if (or oldEventName
(hasOldName (asUpperCamelCase parent.name preserveAcronyms=true)))}}
{{#if oldEventName}}
{{> oldNameImpl eventName=oldEventName}}
{{> oldNameImpl cluster=(compatClusterNameRemapping parent.name) eventName=oldEventName}}
{{else}}
{{> oldNameImpl eventName=(asUpperCamelCase name preserveAcronyms=true)}}
{{> oldNameImpl cluster=(compatClusterNameRemapping parent.name) eventName=(asUpperCamelCase name preserveAcronyms=true)}}
{{/if}}
{{/if}}
{{/inline}}
Expand Down
37 changes: 22 additions & 15 deletions src/darwin/Framework/CHIP/templates/MTRStructsObjc.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ NS_ASSUME_NONNULL_BEGIN
{{#zcl_structs}}
{{#*inline "interfaceDecl"}}
{{#zcl_struct_items}}
{{#if (isSupported (asUpperCamelCase ../../name preserveAcronyms=true) struct=../struct structField=(asStructPropertyName label))}}
{{> 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 (isSupported ../cluster struct=../struct structField=(asStructPropertyName label))}}
{{> struct_field_decl cluster=../cluster type=type label=label}} {{availability (asUpperCamelCase ../cluster preserveAcronyms=true) struct=../struct structField=(asStructPropertyName label) deprecationMessage=(concat "Please use MTR" (asUpperCamelCase ../../name preserveAcronyms=true) "Cluster" (asUpperCamelCase ../name preserveAcronyms=true))}};
{{/if}}
{{#if (hasOldName (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(asStructPropertyName label))}}
{{#if (isSupported (asUpperCamelCase ../../name preserveAcronyms=true) struct=(asUpperCamelCase ../name preserveAcronyms=true) structField=(oldName (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 label)) deprecationMessage=(concat "Please use " (asStructPropertyName label))}};
{{#if (hasOldName ../cluster struct=../struct structField=(asStructPropertyName label))}}
{{#if (isSupported ../cluster struct=../struct structField=(oldName ../cluster struct=../struct structField=(asStructPropertyName label)))}}
{{> struct_field_decl cluster=../cluster type=type label=(oldName ../cluster struct=../struct structField=(asStructPropertyName label))}} {{availability ../cluster struct=../struct structField=(oldName ../cluster struct=../struct structField=(asStructPropertyName label)) deprecationMessage=(concat "Please use " (asStructPropertyName label))}};
{{/if}}
{{/if}}
{{/zcl_struct_items}}
{{/inline}}
{{#if (isSupported (asUpperCamelCase parent.name preserveAcronyms=true) struct=(asUpperCamelCase name preserveAcronyms=true))}}
{{availability (asUpperCamelCase parent.name preserveAcronyms=true) struct=(asUpperCamelCase name preserveAcronyms=true) deprecationMessage="This struct is unused and will be removed"}}
@interface MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}} : NSObject <NSCopying>
{{> interfaceDecl struct=(asUpperCamelCase name preserveAcronyms=true)}}
{{> interfaceDecl cluster=(asUpperCamelCase parent.name preserveAcronyms=true) struct=(asUpperCamelCase name preserveAcronyms=true)}}
@end

{{/if}}
Expand All @@ -30,7 +30,9 @@ NS_ASSUME_NONNULL_BEGIN
{{#if (isSupported (compatClusterNameRemapping parent.name) struct=structName)}}
{{availability (compatClusterNameRemapping parent.name) struct=structName deprecationMessage=(concat "Please use MTR" (asUpperCamelCase parent.name preserveAcronyms=true) "Cluster" (asUpperCamelCase name preserveAcronyms=true))}}
@interface MTR{{compatClusterNameRemapping parent.name}}Cluster{{structName}} : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}
{{> interfaceDecl cluster=(compatClusterNameRemapping parent.name) struct=structName}}
@end

{{/if}}
{{/inline}}
{{! Takes the old name of the struct, if any, as oldStructName. }}
Expand All @@ -48,28 +50,33 @@ NS_ASSUME_NONNULL_BEGIN
{{/zcl_structs}}

{{#zcl_events}}
{{#if (isSupported (asUpperCamelCase parent.name preserveAcronyms=true) event=(asUpperCamelCase name preserveAcronyms=true))}}
{{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>
{{#*inline "interfaceDecl"}}
{{#zcl_event_fields}}
{{#if (isSupported (asUpperCamelCase ../../name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name))}}
{{> 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 (isSupported ../cluster event=../event eventField=(asStructPropertyName name))}}
{{> struct_field_decl cluster=../cluster type=type label=name}} {{availability ../cluster event=../event eventField=(asStructPropertyName name) deprecationMessage=(concat "Please use MTR" (asUpperCamelCase ../../name preserveAcronyms=true) "Cluster" (asUpperCamelCase ../name preserveAcronyms=true) "Event")}};
{{/if}}
{{#if (hasOldName (asUpperCamelCase ../parent.name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(asStructPropertyName name))}}
{{#if (isSupported (asUpperCamelCase ../../name preserveAcronyms=true) event=(asUpperCamelCase ../name preserveAcronyms=true) eventField=(oldName (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 (hasOldName ../cluster event=../event eventField=(asStructPropertyName name))}}
{{#if (isSupported ../cluster event=../event eventField=(oldName ../cluster event=../event eventField=(asStructPropertyName name)))}}
{{> struct_field_decl cluster=../cluster type=type label=(oldName ../cluster event=../event eventField=(asStructPropertyName name))}} {{availability ../cluster event=../event eventField=(oldName ../cluster event=../event eventField=(asStructPropertyName name)) deprecationMessage=(concat "Please use " (asStructPropertyName name))}};
{{/if}}
{{/if}}
{{/zcl_event_fields}}
{{/inline}}
{{#if (isSupported (asUpperCamelCase parent.name preserveAcronyms=true) event=(asUpperCamelCase name preserveAcronyms=true))}}
{{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>
{{> interfaceDecl cluster=(asUpperCamelCase parent.name preserveAcronyms=true) event=(asUpperCamelCase name preserveAcronyms=true)}}
@end

{{/if}}
{{! Takes the name of the event to use as eventName. }}
{{#*inline "oldNameDecl"}}
{{#if (isSupported (compatClusterNameRemapping parent.name) event=eventName)}}

{{availability (compatClusterNameRemapping parent.name) event=eventName deprecationMessage=(concat "Please use MTR" (asUpperCamelCase parent.name preserveAcronyms=true) "Cluster" (asUpperCamelCase name preserveAcronyms=true) "Event")}}
@interface MTR{{compatClusterNameRemapping parent.name}}Cluster{{eventName}}Event : MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Event
{{> interfaceDecl cluster=(compatClusterNameRemapping parent.name) event=eventName}}
@end

{{/if}}
{{/inline}}
{{! Takes the old name of the event, if any, as oldEventName. }}
Expand Down
Loading

0 comments on commit 4554b38

Please sign in to comment.