Skip to content

Commit

Permalink
Enable client-side accessor generation for strongly typed enum attrib…
Browse files Browse the repository at this point in the history
…utes. (#12731)
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Apr 20, 2022
1 parent f8cd5f9 commit 4427107
Show file tree
Hide file tree
Showing 26 changed files with 7,672 additions and 56 deletions.
3 changes: 3 additions & 0 deletions src/app/data-model/Nullable.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct Nullable : protected Optional<T>
// Optional.
using Optional<T>::Value;

// Some consumers need an easy way to determine our underlying type.
using UnderlyingType = T;

constexpr void SetNull() { Optional<T>::ClearValue(); }
constexpr bool IsNull() const { return !Optional<T>::HasValue(); }

Expand Down
6 changes: 0 additions & 6 deletions src/app/zap-templates/common/ClustersHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,6 @@ function asChipCallback(item)
return { name : 'List', type : null };
}

if (item.isEnum) {
// Unsupported or now, until we figure out what to do for callbacks for
// strongly typed enums.
return { name : 'Unsupported', type : null };
}

const basicType = ChipTypesHelper.asBasicType(item.chipType);
switch (basicType) {
case 'int8_t':
Expand Down
4 changes: 2 additions & 2 deletions src/controller/java/templates/CHIPClusters-JNI.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ JNI_METHOD(void, {{asUpperCamelCase ../name}}Cluster, {{asLowerCamelCase name}})
chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type request;

{{#chip_cluster_command_arguments}}
{{>encode_value target=(concat "request." (asLowerCamelCase label)) source=(asLowerCamelCase label)}}
{{>encode_value target=(concat "request." (asLowerCamelCase label)) source=(asLowerCamelCase label) cluster=parent.parent.name}}
{{/chip_cluster_command_arguments}}

{{#*inline "callbackName"}}{{#if hasSpecificResponse}}{{asUpperCamelCase parent.name false}}Cluster{{asUpperCamelCase responseName false}}{{else}}DefaultSuccess{{/if}}{{/inline}}
Expand Down Expand Up @@ -93,7 +93,7 @@ JNI_METHOD(void, {{asUpperCamelCase ../name}}Cluster, write{{asUpperCamelCase na
using TypeInfo = chip::app::Clusters::{{asUpperCamelCase parent.name}}::Attributes::{{asUpperCamelCase name}}::TypeInfo;
TypeInfo::Type cppValue;

{{>encode_value target="cppValue" source="value"}}
{{>encode_value target="cppValue" source="value" cluster=parent.name}}

std::unique_ptr<CHIPDefaultSuccessCallback, void (*)(CHIPDefaultSuccessCallback *)> onSuccess(Platform::New<CHIPDefaultSuccessCallback>(callback), Platform::Delete<CHIPDefaultSuccessCallback>);
VerifyOrReturn(onSuccess.get() != nullptr, chip::AndroidClusterExceptions::GetInstance().ReturnIllegalStateException(env, callback, "Error creating native success callback", CHIP_ERROR_NO_MEMORY));
Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/templates/CHIPInvokeCallbacks-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void CHIP{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}Callbac
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Error invoking Java callback: %s", ErrorStr(err)));

{{#chip_cluster_response_arguments}}
{{>decode_value source=(concat "dataResponse." (asLowerCamelCase name)) target=(asSymbol label)}}
{{>decode_value source=(concat "dataResponse." (asLowerCamelCase name)) target=(asSymbol label) cluster=parent.parent.name}}
{{/chip_cluster_response_arguments}}

env->CallVoidMethod(javaCallbackRef, javaMethod{{#chip_cluster_response_arguments}}, {{asSymbol label}}{{/chip_cluster_response_arguments}});
Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/templates/CHIPReadCallbacks-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallb
err = chip::JniReferences::GetInstance().FindMethod(env, javaCallbackRef, "onSuccess", "({{#if isArray}}{{else if isStruct}}{{else if isOptional}}Ljava/util/Optional;{{else if (isOctetString type)}}[B{{else if (isCharString type)}}Ljava/lang/String;{{else}}{{asJniSignature type true}}{{/if}})V", &javaMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Could not find onSuccess() method"));

{{>decode_value source="value" target="javaValue"}}
{{>decode_value source="value" target="javaValue" cluster=parent.name}}

env->CallVoidMethod(javaCallbackRef, javaMethod, javaValue);
}
Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/templates/partials/decode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ chip::UtfString {{target}}UtfString(env, {{>item}});
{{else}}
std::string {{target}}ClassName = "java/lang/{{asJavaBasicTypeForZclType type true}}";
std::string {{target}}CtorSignature = "({{asJniSignature type false}})V";
chip::JniReferences::GetInstance().CreateBoxedObject<{{zapTypeToDecodableClusterObjectType type ns=parent.parent.name forceNotNullable=true forceNotOptional=true}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), {{>item}}, {{target}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{zapTypeToDecodableClusterObjectType type ns=cluster forceNotNullable=true forceNotOptional=true}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), {{>item}}, {{target}});
{{/if}}
{{#if isOptional}}
chip::JniReferences::GetInstance().CreateOptional({{target}}, {{target}});
Expand Down
12 changes: 6 additions & 6 deletions src/controller/java/templates/partials/encode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ chip::JniReferences::GetInstance().GetOptionalValue({{source}}, {{source}});
{{/if}}
{{#if isNullable}}
{{#if_chip_enum type}}
decltype({{target}}) {{source}}Value;
decltype({{target}})::UnderlyingType {{source}}Value;
if ({{source}} != nullptr) {
{{source}}Value = static_cast<decltype({{target}})>(chip::JniReferences::GetInstance().{{asJavaBoxedType type}}ToPrimitive({{source}}));
{{source}}Value = static_cast<decltype({{source}}Value)>(chip::JniReferences::GetInstance().{{asJavaBoxedType type}}ToPrimitive({{source}}));
}
{{else}}
{{#if isArray}}
Expand Down Expand Up @@ -41,9 +41,9 @@ chip::JniReferences::GetInstance().GetOptionalValue({{source}}, {{source}});
{{#*inline "value"}}
{{! TODO Implement complex types parsing in order to properly set the request parameters }}
{{#if isArray}}
{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}()
{{zapTypeToEncodableClusterObjectType type ns=cluster}}()
{{else if isStruct}}
{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}()
{{zapTypeToEncodableClusterObjectType type ns=cluster}}()
{{else if (isOctetString type)}}
chip::JniByteArray(env, static_cast<jbyteArray>({{source}})).byteSpan()
{{else if (isCharString type)}}
Expand All @@ -62,10 +62,10 @@ chip::JniUtfString(env, static_cast<jstring>({{source}})).charSpan()
{{/inline}}
{{target}} =
{{#if isOptional}}
{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}(
{{zapTypeToEncodableClusterObjectType type ns=cluster}}(
{{/if}}
{{#if isNullable}}
{{source}} == nullptr ? chip::app::DataModel::Nullable<{{chipType}}>() : chip::app::DataModel::Nullable<{{chipType}}>({{source}}Value)
{{source}} == nullptr ? {{zapTypeToEncodableClusterObjectType type ns=cluster forceNotOptional=true}}() : {{zapTypeToEncodableClusterObjectType type ns=cluster forceNotOptional=true}}({{source}}Value)
{{else}}
{{! TODO If the inline partial is indented, generation fails with "result.split is not a function". }}
{{>value}}
Expand Down
Loading

0 comments on commit 4427107

Please sign in to comment.