Skip to content

Commit

Permalink
Generate Objective C NS_ENUM/NS_OPTIONS for enums and bitmaps. (#15493)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple authored Mar 1, 2022
1 parent 29db0f4 commit 6adeb23
Show file tree
Hide file tree
Showing 4 changed files with 1,609 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ function hasSpecificAttributes(options)
function asLowerCamelCase(label)
{
let str = string.toCamelCase(label, true);
// Check for the case when were:
// Check for the case when we're:
// 1. A single word (that's the regexp at the beginning, which matches the
// word-splitting regexp in string.toCamelCase).
// 2. Starting with multiple capital letters in a row.
Expand Down
19 changes: 19 additions & 0 deletions src/darwin/Framework/CHIP/templates/CHIPClustersObjc.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ subscriptionEstablished:(SubscriptionEstablishedHandler _Nullable)subscriptionEs

{{/chip_client_clusters}}

{{#zcl_clusters}}
{{#zcl_enums}}
typedef NS_ENUM(NSInteger, {{objCEnumName ../name label}}) {
{{#zcl_enum_items}}
{{objCEnumName ../../name ../label}}{{objCEnumItemLabel label}} = {{asHex value 2}},
{{/zcl_enum_items}}
};

{{/zcl_enums}}
{{#zcl_bitmaps}}
typedef NS_OPTIONS(NSUInteger, {{objCEnumName ../name label}}) {
{{#zcl_bitmap_items}}
{{objCEnumName ../../name ../label}}{{objCEnumItemLabel label}} = {{asHex mask}},
{{/zcl_bitmap_items}}
};

{{/zcl_bitmaps}}
{{/zcl_clusters}}

NS_ASSUME_NONNULL_END

#endif /* CHIP_CLUSTERS_H */
Expand Down
46 changes: 46 additions & 0 deletions src/darwin/Framework/CHIP/templates/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,50 @@ function commandHasRequiredField(command)
return command.arguments.some(arg => !arg.isOptional);
}

/**
* Produce a reasonable name for an Objective C enum for the given cluster name
* and enum label. Because a lot of our enum labels already have the cluster
* name prefixed (e.g. NetworkCommissioning*, or the IdentifyIdentifyType that
* has it prefixed _twice_) just concatenating the two gives overly verbose
* names in a few cases (e.g. "IdentifyIdentifyIdentifyType").
*
* This function strips out the redundant cluster names, and strips off trailing
* "Enum" bits on the enum names while we're here.
*/
function objCEnumName(clusterName, enumLabel)
{
clusterName = appHelper.asUpperCamelCase(clusterName);
enumLabel = appHelper.asUpperCamelCase(enumLabel);
// Some enum names have one or more copies of the cluster name at the
// beginning.
while (enumLabel.startsWith(clusterName)) {
enumLabel = enumLabel.substring(clusterName.length);
}

if (enumLabel.endsWith("Enum")) {
// Strip that off; it'll clearly be an enum anyway.
enumLabel = enumLabel.substring(0, enumLabel.length - "Enum".length);
}

return "CHIP" + clusterName + enumLabel;
}

function objCEnumItemLabel(itemLabel)
{
// Check for the case when we're:
// 1. A single word (that's the regexp at the beginning, which matches the
// word-splitting regexp in string.toCamelCase).
// 2. All upper-case.
//
// This will get converted to lowercase except the first letter by
// asUpperCamelCase, which is not really what we want.
if (!/ |_|-|\//.test(itemLabel) && itemLabel.toUpperCase() == itemLabel) {
return itemLabel.replace(/[\.:]/g, '');
}

return appHelper.asUpperCamelCase(itemLabel);
}

//
// Module exports
//
Expand All @@ -155,3 +199,5 @@ exports.asObjectiveCType = asObjectiveCType;
exports.asStructPropertyName = asStructPropertyName;
exports.asGetterName = asGetterName;
exports.commandHasRequiredField = commandHasRequiredField;
exports.objCEnumName = objCEnumName;
exports.objCEnumItemLabel = objCEnumItemLabel;
Loading

0 comments on commit 6adeb23

Please sign in to comment.