Skip to content

Commit

Permalink
Cleanup zap templates (#37080)
Browse files Browse the repository at this point in the history
* Cleanup zap templates

* Update whitespace
  • Loading branch information
gmarcosb authored Jan 18, 2025
1 parent c389092 commit 9e23d47
Show file tree
Hide file tree
Showing 5 changed files with 736 additions and 758 deletions.
94 changes: 44 additions & 50 deletions src/controller/java/templates/partials/decode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 +25,54 @@ if ({{source}}.IsNull()) {
{{>decode_value target=(concat "newElement_" depth) source=(concat "entry_" depth) cluster=cluster depth=(incrementDepth depth) isArray=false forceNotList=true omitDeclaration=false earlyReturn=earlyReturn}}
chip::JniReferences::GetInstance().AddToList({{target}}, newElement_{{depth}});
}
{{else}}
{{#if_is_struct type}}
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
{{>decode_value target=(concat ../target "_" (asLowerCamelCase label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster depth=(incrementDepth ../depth) omitDeclaration=false earlyReturn=../earlyReturn}}
{{/zcl_struct_items_by_struct_and_cluster_name}}
{{else if_is_struct type}}
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
{{>decode_value target=(concat ../target "_" (asLowerCamelCase label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster depth=(incrementDepth ../depth) omitDeclaration=false earlyReturn=../earlyReturn}}
{{/zcl_struct_items_by_struct_and_cluster_name}}

{
jclass {{asLowerCamelCase type}}StructClass_{{depth}};
err = chip::JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}", {{asLowerCamelCase type}}StructClass_{{depth}});
if (err != CHIP_NO_ERROR) {
ChipLogError(Zcl, "Could not find class ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}");
return {{earlyReturn}};
}
{
jclass {{asLowerCamelCase type}}StructClass_{{depth}};
err = chip::JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}", {{asLowerCamelCase type}}StructClass_{{depth}});
if (err != CHIP_NO_ERROR) {
ChipLogError(Zcl, "Could not find class ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}");
return {{earlyReturn}};
}

jmethodID {{asLowerCamelCase type}}StructCtor_{{depth}};
err = chip::JniReferences::GetInstance().FindMethod(
env, {{asLowerCamelCase type}}StructClass_{{depth}}, "<init>",
"({{#zcl_struct_items_by_struct_and_cluster_name type cluster}}{{asJniSignature type null (asUpperCamelCase ../cluster) true}}{{/zcl_struct_items_by_struct_and_cluster_name}})V",
&{{asLowerCamelCase type}}StructCtor_{{depth}});
if (err != CHIP_NO_ERROR || {{asLowerCamelCase type}}StructCtor_{{depth}} == nullptr) {
ChipLogError(Zcl, "Could not find ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}} constructor");
return {{earlyReturn}};
}
jmethodID {{asLowerCamelCase type}}StructCtor_{{depth}};
err = chip::JniReferences::GetInstance().FindMethod(
env, {{asLowerCamelCase type}}StructClass_{{depth}}, "<init>",
"({{#zcl_struct_items_by_struct_and_cluster_name type cluster}}{{asJniSignature type null (asUpperCamelCase ../cluster) true}}{{/zcl_struct_items_by_struct_and_cluster_name}})V",
&{{asLowerCamelCase type}}StructCtor_{{depth}});
if (err != CHIP_NO_ERROR || {{asLowerCamelCase type}}StructCtor_{{depth}} == nullptr) {
ChipLogError(Zcl, "Could not find ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}} constructor");
return {{earlyReturn}};
}

{{target}} = env->NewObject({{asLowerCamelCase type}}StructClass_{{depth}}, {{asLowerCamelCase type}}StructCtor_{{depth}}
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
, {{../target}}_{{asLowerCamelCase label}}
{{target}} = env->NewObject({{asLowerCamelCase type}}StructClass_{{depth}}, {{asLowerCamelCase type}}StructCtor_{{depth}}
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
, {{../target}}_{{asLowerCamelCase label}}
{{/zcl_struct_items_by_struct_and_cluster_name}}
);
}
}
{{else if_is_strongly_typed_chip_enum type}}
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{else if (isOctetString type)}}
jbyteArray {{target}}ByteArray = env->NewByteArray(static_cast<jsize>({{source}}.size()));
env->SetByteArrayRegion({{target}}ByteArray, 0, static_cast<jsize>({{source}}.size()), reinterpret_cast<const jbyte *>({{source}}.data()));
{{target}} = {{target}}ByteArray;
{{else if (isCharString type)}}
LogErrorOnFailure(chip::JniReferences::GetInstance().CharToStringUTF({{source}}, {{target}}));
{{else}}
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
{{#if_is_strongly_typed_bitmap type}}
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}}.Raw());
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{else}}
{{#if_is_strongly_typed_chip_enum type}}
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{else}}
{{#if (isOctetString type)}}
jbyteArray {{target}}ByteArray = env->NewByteArray(static_cast<jsize>({{source}}.size()));
env->SetByteArrayRegion({{target}}ByteArray, 0, static_cast<jsize>({{source}}.size()), reinterpret_cast<const jbyte *>({{source}}.data()));
{{target}} = {{target}}ByteArray;
{{else if (isCharString type)}}
LogErrorOnFailure(chip::JniReferences::GetInstance().CharToStringUTF({{source}}, {{target}}));
{{else}}
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
{{#if_is_strongly_typed_bitmap type}}
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}}.Raw());
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{else}}
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{/if_is_strongly_typed_bitmap}}
{{/if}}
{{/if_is_strongly_typed_chip_enum}}
{{/if_is_struct}}
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
{{/if_is_strongly_typed_bitmap}}
{{/if}}
4 changes: 0 additions & 4 deletions src/controller/java/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
"name": "cluster_header",
"path": "../../../app/zap-templates/partials/cluster_header.zapt"
},
{
"name": "encode_value",
"path": "partials/encode_value.zapt"
},
{
"name": "decode_value",
"path": "partials/decode_value.zapt"
Expand Down
48 changes: 21 additions & 27 deletions src/darwin/Framework/CHIP/templates/partials/decode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,26 @@
}
{{target}} = array_{{depth}};
}
{{else if_is_struct type}}
{{target}} = [{{asObjectiveCClass type cluster forceNotList=true}} new];
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
{{>decode_value target=(concat ../target "." (asStructPropertyName label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth) }}
{{/if}}
{{/zcl_struct_items_by_struct_and_cluster_name}}
{{else if_is_strongly_typed_chip_enum type}}
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:chip::to_underlying({{source}})];
{{else if_is_strongly_typed_bitmap type}}
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}.Raw()];
{{else if (isOctetString type)}}
{{target}} = AsData({{source}});
{{else if (isCharString type)}}
{{target}} = AsString({{source}});
if ({{target}} == nil) {
{{! Invalid UTF-8. Just make up an error for now. }}
CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT;
{{errorCode}}
}
{{else}}
{{#if_is_struct type}}
{{target}} = [{{asObjectiveCClass type cluster forceNotList=true}} new];
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
{{>decode_value target=(concat ../target "." (asStructPropertyName label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth) }}
{{/if}}
{{/zcl_struct_items_by_struct_and_cluster_name}}
{{else}}
{{#if_is_strongly_typed_chip_enum type}}
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:chip::to_underlying({{source}})];
{{else}}
{{#if_is_strongly_typed_bitmap type}}
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}.Raw()];
{{else if (isOctetString type)}}
{{target}} = AsData({{source}});
{{else if (isCharString type)}}
{{target}} = AsString({{source}});
if ({{target}} == nil) {
{{! Invalid UTF-8. Just make up an error for now. }}
CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT;
{{errorCode}}
}
{{else}}
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}];
{{/if_is_strongly_typed_bitmap}}
{{/if_is_strongly_typed_chip_enum}}
{{/if_is_struct}}
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}];
{{/if}}
28 changes: 11 additions & 17 deletions src/darwin/Framework/CHIP/templates/partials/encode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,16 @@
{{target}} = AsByteSpan({{source}});
{{else if (isCharString type)}}
{{target}} = AsCharSpan({{source}});
{{else if_is_struct type}}
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
{{>encode_value target=(concat ../target "." (asLowerCamelCase label)) source=(concat ../source "." (asStructPropertyName label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth)}}
{{/if}}
{{/zcl_struct_items_by_struct_and_cluster_name}}
{{else if_is_strongly_typed_chip_enum type}}
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
{{else if_is_strongly_typed_bitmap type}}
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
{{else}}
{{#if_is_struct type}}
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
{{>encode_value target=(concat ../target "." (asLowerCamelCase label)) source=(concat ../source "." (asStructPropertyName label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth)}}
{{/if}}
{{/zcl_struct_items_by_struct_and_cluster_name}}
{{else}}
{{#if_is_strongly_typed_chip_enum type}}
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
{{else}}
{{#if_is_strongly_typed_bitmap type}}
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
{{else}}
{{target}} = {{source}}.{{asObjectiveCNumberType source type true}}Value;
{{/if_is_strongly_typed_bitmap}}
{{/if_is_strongly_typed_chip_enum}}
{{/if_is_struct}}
{{target}} = {{source}}.{{asObjectiveCNumberType source type true}}Value;
{{/if}}
Loading

0 comments on commit 9e23d47

Please sign in to comment.