Skip to content

Commit 9e23d47

Browse files
authored
Cleanup zap templates (#37080)
* Cleanup zap templates * Update whitespace
1 parent c389092 commit 9e23d47

File tree

5 files changed

+736
-758
lines changed

5 files changed

+736
-758
lines changed

src/controller/java/templates/partials/decode_value.zapt

+44-50
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,54 @@ if ({{source}}.IsNull()) {
2525
{{>decode_value target=(concat "newElement_" depth) source=(concat "entry_" depth) cluster=cluster depth=(incrementDepth depth) isArray=false forceNotList=true omitDeclaration=false earlyReturn=earlyReturn}}
2626
chip::JniReferences::GetInstance().AddToList({{target}}, newElement_{{depth}});
2727
}
28-
{{else}}
29-
{{#if_is_struct type}}
30-
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
31-
{{>decode_value target=(concat ../target "_" (asLowerCamelCase label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster depth=(incrementDepth ../depth) omitDeclaration=false earlyReturn=../earlyReturn}}
32-
{{/zcl_struct_items_by_struct_and_cluster_name}}
28+
{{else if_is_struct type}}
29+
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
30+
{{>decode_value target=(concat ../target "_" (asLowerCamelCase label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster depth=(incrementDepth ../depth) omitDeclaration=false earlyReturn=../earlyReturn}}
31+
{{/zcl_struct_items_by_struct_and_cluster_name}}
3332

34-
{
35-
jclass {{asLowerCamelCase type}}StructClass_{{depth}};
36-
err = chip::JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}", {{asLowerCamelCase type}}StructClass_{{depth}});
37-
if (err != CHIP_NO_ERROR) {
38-
ChipLogError(Zcl, "Could not find class ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}");
39-
return {{earlyReturn}};
40-
}
33+
{
34+
jclass {{asLowerCamelCase type}}StructClass_{{depth}};
35+
err = chip::JniReferences::GetInstance().GetLocalClassRef(env, "chip/devicecontroller/ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}", {{asLowerCamelCase type}}StructClass_{{depth}});
36+
if (err != CHIP_NO_ERROR) {
37+
ChipLogError(Zcl, "Could not find class ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}}");
38+
return {{earlyReturn}};
39+
}
4140

42-
jmethodID {{asLowerCamelCase type}}StructCtor_{{depth}};
43-
err = chip::JniReferences::GetInstance().FindMethod(
44-
env, {{asLowerCamelCase type}}StructClass_{{depth}}, "<init>",
45-
"({{#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",
46-
&{{asLowerCamelCase type}}StructCtor_{{depth}});
47-
if (err != CHIP_NO_ERROR || {{asLowerCamelCase type}}StructCtor_{{depth}} == nullptr) {
48-
ChipLogError(Zcl, "Could not find ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}} constructor");
49-
return {{earlyReturn}};
50-
}
41+
jmethodID {{asLowerCamelCase type}}StructCtor_{{depth}};
42+
err = chip::JniReferences::GetInstance().FindMethod(
43+
env, {{asLowerCamelCase type}}StructClass_{{depth}}, "<init>",
44+
"({{#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",
45+
&{{asLowerCamelCase type}}StructCtor_{{depth}});
46+
if (err != CHIP_NO_ERROR || {{asLowerCamelCase type}}StructCtor_{{depth}} == nullptr) {
47+
ChipLogError(Zcl, "Could not find ChipStructs${{asUpperCamelCase cluster}}Cluster{{asUpperCamelCase type}} constructor");
48+
return {{earlyReturn}};
49+
}
5150

52-
{{target}} = env->NewObject({{asLowerCamelCase type}}StructClass_{{depth}}, {{asLowerCamelCase type}}StructCtor_{{depth}}
53-
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
54-
, {{../target}}_{{asLowerCamelCase label}}
51+
{{target}} = env->NewObject({{asLowerCamelCase type}}StructClass_{{depth}}, {{asLowerCamelCase type}}StructCtor_{{depth}}
52+
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
53+
, {{../target}}_{{asLowerCamelCase label}}
5554
{{/zcl_struct_items_by_struct_and_cluster_name}}
5655
);
57-
}
56+
}
57+
{{else if_is_strongly_typed_chip_enum type}}
58+
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
59+
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
60+
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
61+
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
62+
{{else if (isOctetString type)}}
63+
jbyteArray {{target}}ByteArray = env->NewByteArray(static_cast<jsize>({{source}}.size()));
64+
env->SetByteArrayRegion({{target}}ByteArray, 0, static_cast<jsize>({{source}}.size()), reinterpret_cast<const jbyte *>({{source}}.data()));
65+
{{target}} = {{target}}ByteArray;
66+
{{else if (isCharString type)}}
67+
LogErrorOnFailure(chip::JniReferences::GetInstance().CharToStringUTF({{source}}, {{target}}));
68+
{{else}}
69+
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
70+
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
71+
{{#if_is_strongly_typed_bitmap type}}
72+
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}}.Raw());
73+
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
5874
{{else}}
59-
{{#if_is_strongly_typed_chip_enum type}}
60-
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
61-
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
62-
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
63-
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
64-
{{else}}
65-
{{#if (isOctetString type)}}
66-
jbyteArray {{target}}ByteArray = env->NewByteArray(static_cast<jsize>({{source}}.size()));
67-
env->SetByteArrayRegion({{target}}ByteArray, 0, static_cast<jsize>({{source}}.size()), reinterpret_cast<const jbyte *>({{source}}.data()));
68-
{{target}} = {{target}}ByteArray;
69-
{{else if (isCharString type)}}
70-
LogErrorOnFailure(chip::JniReferences::GetInstance().CharToStringUTF({{source}}, {{target}}));
71-
{{else}}
72-
std::string {{target}}ClassName = "{{asJniClassName type null (asUpperCamelCase cluster)}}";
73-
std::string {{target}}CtorSignature = "({{asJniSignature type null (asUpperCamelCase cluster) false}})V";
74-
{{#if_is_strongly_typed_bitmap type}}
75-
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}}.Raw());
76-
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
77-
{{else}}
78-
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
79-
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
80-
{{/if_is_strongly_typed_bitmap}}
81-
{{/if}}
82-
{{/if_is_strongly_typed_chip_enum}}
83-
{{/if_is_struct}}
75+
{{asJniBasicType type false}} jni{{target}} = static_cast<{{asJniBasicType type false}}>({{source}});
76+
chip::JniReferences::GetInstance().CreateBoxedObject<{{asJniBasicType type false}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), jni{{target}}, {{target}});
77+
{{/if_is_strongly_typed_bitmap}}
8478
{{/if}}

src/controller/java/templates/templates.json

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
"name": "cluster_header",
2525
"path": "../../../app/zap-templates/partials/cluster_header.zapt"
2626
},
27-
{
28-
"name": "encode_value",
29-
"path": "partials/encode_value.zapt"
30-
},
3127
{
3228
"name": "decode_value",
3329
"path": "partials/decode_value.zapt"

src/darwin/Framework/CHIP/templates/partials/decode_value.zapt

+21-27
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,26 @@
2626
}
2727
{{target}} = array_{{depth}};
2828
}
29+
{{else if_is_struct type}}
30+
{{target}} = [{{asObjectiveCClass type cluster forceNotList=true}} new];
31+
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
32+
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
33+
{{>decode_value target=(concat ../target "." (asStructPropertyName label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth) }}
34+
{{/if}}
35+
{{/zcl_struct_items_by_struct_and_cluster_name}}
36+
{{else if_is_strongly_typed_chip_enum type}}
37+
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:chip::to_underlying({{source}})];
38+
{{else if_is_strongly_typed_bitmap type}}
39+
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}.Raw()];
40+
{{else if (isOctetString type)}}
41+
{{target}} = AsData({{source}});
42+
{{else if (isCharString type)}}
43+
{{target}} = AsString({{source}});
44+
if ({{target}} == nil) {
45+
{{! Invalid UTF-8. Just make up an error for now. }}
46+
CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT;
47+
{{errorCode}}
48+
}
2949
{{else}}
30-
{{#if_is_struct type}}
31-
{{target}} = [{{asObjectiveCClass type cluster forceNotList=true}} new];
32-
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
33-
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
34-
{{>decode_value target=(concat ../target "." (asStructPropertyName label)) source=(concat ../source "." (asLowerCamelCase label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth) }}
35-
{{/if}}
36-
{{/zcl_struct_items_by_struct_and_cluster_name}}
37-
{{else}}
38-
{{#if_is_strongly_typed_chip_enum type}}
39-
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:chip::to_underlying({{source}})];
40-
{{else}}
41-
{{#if_is_strongly_typed_bitmap type}}
42-
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}.Raw()];
43-
{{else if (isOctetString type)}}
44-
{{target}} = AsData({{source}});
45-
{{else if (isCharString type)}}
46-
{{target}} = AsString({{source}});
47-
if ({{target}} == nil) {
48-
{{! Invalid UTF-8. Just make up an error for now. }}
49-
CHIP_ERROR err = CHIP_ERROR_INVALID_ARGUMENT;
50-
{{errorCode}}
51-
}
52-
{{else}}
53-
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}];
54-
{{/if_is_strongly_typed_bitmap}}
55-
{{/if_is_strongly_typed_chip_enum}}
56-
{{/if_is_struct}}
50+
{{target}} = [NSNumber numberWith{{asObjectiveCNumberType "" type false}}:{{source}}];
5751
{{/if}}

src/darwin/Framework/CHIP/templates/partials/encode_value.zapt

+11-17
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,16 @@
4444
{{target}} = AsByteSpan({{source}});
4545
{{else if (isCharString type)}}
4646
{{target}} = AsCharSpan({{source}});
47+
{{else if_is_struct type}}
48+
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
49+
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
50+
{{>encode_value target=(concat ../target "." (asLowerCamelCase label)) source=(concat ../source "." (asStructPropertyName label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth)}}
51+
{{/if}}
52+
{{/zcl_struct_items_by_struct_and_cluster_name}}
53+
{{else if_is_strongly_typed_chip_enum type}}
54+
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
55+
{{else if_is_strongly_typed_bitmap type}}
56+
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
4757
{{else}}
48-
{{#if_is_struct type}}
49-
{{#zcl_struct_items_by_struct_and_cluster_name type cluster}}
50-
{{#if (isSupported (asUpperCamelCase ../cluster preserveAcronyms=true) struct=(asUpperCamelCase ../type preserveAcronyms=true) structField=(asStructPropertyName label))}}
51-
{{>encode_value target=(concat ../target "." (asLowerCamelCase label)) source=(concat ../source "." (asStructPropertyName label)) cluster=../cluster errorCode=../errorCode depth=(incrementDepth ../depth)}}
52-
{{/if}}
53-
{{/zcl_struct_items_by_struct_and_cluster_name}}
54-
{{else}}
55-
{{#if_is_strongly_typed_chip_enum type}}
56-
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
57-
{{else}}
58-
{{#if_is_strongly_typed_bitmap type}}
59-
{{target}} = static_cast<std::remove_reference_t<decltype({{target}})>>({{source}}.{{asObjectiveCNumberType source type true}}Value);
60-
{{else}}
61-
{{target}} = {{source}}.{{asObjectiveCNumberType source type true}}Value;
62-
{{/if_is_strongly_typed_bitmap}}
63-
{{/if_is_strongly_typed_chip_enum}}
64-
{{/if_is_struct}}
58+
{{target}} = {{source}}.{{asObjectiveCNumberType source type true}}Value;
6559
{{/if}}

0 commit comments

Comments
 (0)