Skip to content

Commit

Permalink
Flag unknown struct fields in chip-tool. (#25297)
Browse files Browse the repository at this point in the history
This helps catch cases when someone mis-types the name of an optional struct
field.

Fixes #21892
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Nov 6, 2023
1 parent e2e34e8 commit 3385032
Show file tree
Hide file tree
Showing 3 changed files with 493 additions and 59 deletions.
16 changes: 16 additions & 0 deletions examples/chip-tool/commands/clusters/ComplexArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ class ComplexArgumentParser
return CHIP_ERROR_INVALID_ARGUMENT;
}

static CHIP_ERROR EnsureNoMembersRemaining(const char * label, const Json::Value & value)
{
auto remainingFields = value.getMemberNames();
if (remainingFields.size() == 0)
{
return CHIP_NO_ERROR;
}
#if CHIP_ERROR_LOGGING
for (auto & field : remainingFields)
{
ChipLogError(chipTool, "Unexpected field name: '%s.%s'", label, field.c_str());
}
#endif // CHIP_ERROR_LOGGING
return CHIP_ERROR_INVALID_ARGUMENT;
}

template <typename T>
static void Finalize(T & request)
{
Expand Down
7 changes: 6 additions & 1 deletion examples/chip-tool/templates/ComplexArgumentParser-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CHIP_ERROR ComplexArgumentParser::Setup(const char * label, chip::app::Clusters:
{
VerifyOrReturnError(value.isObject(), CHIP_ERROR_INVALID_ARGUMENT);

// Copy to track which members we already processed.
Json::Value valueCopy(value);

{{#zcl_struct_items}}
{{#unless isOptional}}
{{~! Fabric index fields are not sent on writes, so don't force people to
Expand All @@ -33,9 +36,11 @@ CHIP_ERROR ComplexArgumentParser::Setup(const char * label, chip::app::Clusters:
{{else if (is_num_equal fieldIdentifier 254)}}
}
{{/if}}
valueCopy.removeMember("{{asLowerCamelCase label}}");

{{/zcl_struct_items}}
return CHIP_NO_ERROR;

return ComplexArgumentParser::EnsureNoMembersRemaining(label, valueCopy);
}

void ComplexArgumentParser::Finalize(chip::app::Clusters::{{#unless (is_number_greater_than structClusterCount 1)}}{{as_camel_cased clusterName false}}{{else}}detail{{/unless}}::Structs::{{name}}::Type & request)
Expand Down

0 comments on commit 3385032

Please sign in to comment.