Skip to content

Commit

Permalink
Handle saveAs in YAML for an optional value. (#18763)
Browse files Browse the repository at this point in the history
* Store the optional value as-is, except for strings where we either store a
  NullOptional or emplace a value using our buffer.
* Adjust constraint checks (MinValue, MaxValue, NotValue) to handle an optional
  expected value.

Fixes #18591
  • Loading branch information
bzbarsky-apple authored May 24, 2022
1 parent 8c37a84 commit 2b02a50
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
{{#*inline "size"}}
{{#if isNullable}}{{asPropertyValue}}.Value().size(){{else}}{{asPropertyValue}}.size(){{/if}}
{{/inline}}
{{#*inline "saveAsTarget"~}}
{{saveAs}}{{#if isOptional}}.Emplace(){{/if}}
{{~/inline}}
{{#if saveAs}}
{{#if (isString type)}}
{{#if isOptional}}
if ({{asPropertyValue dontUnwrapValue=true}}.HasValue()) {
{{/if}}
{{#if isNullable}}
if ({{asPropertyValue}}.IsNull()){
{{saveAs}}.SetNull();
{{> saveAsTarget}}.SetNull();
}
else {
{{/if}}
Expand All @@ -19,12 +25,19 @@
{{saveAs}}Buffer = static_cast<{{#if (isOctetString type)}}uint8_t{{else}}char{{/if}} *>(chip::Platform::MemoryAlloc({{>size}}));
memcpy({{saveAs}}Buffer, {{>data}}, {{>size}});
{{#if isNullable}}
{{saveAs}}.SetNonNull({{saveAs}}Buffer, {{>size}});
{{> saveAsTarget}}.SetNonNull({{saveAs}}Buffer, {{>size}});
}
{{else}}
{{saveAs}} = {{chipType}}({{saveAs}}Buffer, {{>size}});
{{> saveAsTarget}} = {{chipType}}({{saveAs}}Buffer, {{>size}});
{{/if}}
{{#if isOptional}}
}
else
{
{{saveAs}} = NullOptional;
}
{{/if}}
{{else}}
{{saveAs}} = {{asPropertyValue}};
{{saveAs}} = {{asPropertyValue dontUnwrapValue=true}};
{{/if}}
{{/if}}
33 changes: 33 additions & 0 deletions src/app/tests/suites/include/ConstraintsChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ class ConstraintsChecker
return CheckConstraintMinValue(itemName, current.Value(), static_cast<T>(expected));
}

template <typename T, typename U>
bool CheckConstraintMinValue(const char * itemName, const T & current, const chip::Optional<U> & expected)
{
if (!expected.HasValue())
{
Exit(std::string(itemName) + ": expected min value does not have a value");
return false;
}
return CheckConstraintMinValue(itemName, current, expected.Value());
}

template <typename T, typename U, std::enable_if_t<!std::is_enum<T>::value, int> = 0>
bool CheckConstraintMaxValue(const char * itemName, T current, U expected)
{
Expand Down Expand Up @@ -269,6 +280,17 @@ class ConstraintsChecker
return CheckConstraintMaxValue(itemName, current.Value(), static_cast<T>(expected));
}

template <typename T, typename U>
bool CheckConstraintMaxValue(const char * itemName, const T & current, const chip::Optional<U> & expected)
{
if (!expected.HasValue())
{
Exit(std::string(itemName) + ": expected max value does not have a value");
return false;
}
return CheckConstraintMaxValue(itemName, current, expected.Value());
}

template <typename T>
bool CheckConstraintNotValue(const char * itemName, const chip::app::DataModel::Nullable<T> & current,
const chip::app::DataModel::Nullable<T> & expected)
Expand Down Expand Up @@ -367,4 +389,15 @@ class ConstraintsChecker

return true;
}

template <typename T, typename U>
bool CheckConstraintNotValue(const char * itemName, const T & current, const chip::Optional<U> & expected)
{
if (!expected.HasValue())
{
Exit(std::string(itemName) + ": expected disallowed value does not have a value");
return false;
}
return CheckConstraintNotValue(itemName, current, expected.Value());
}
};

0 comments on commit 2b02a50

Please sign in to comment.