[PATCH] libcamera: yaml-parser: Differentiate between empty and empty string

Barnabás Pőcze pobrn at protonmail.com
Tue Sep 17 15:45:13 CEST 2024


Hi


2024. szeptember 17., kedd 00:28 keltezéssel, Stefan Klug <stefan.klug at ideasonboard.com> írta:

> When accessing a nonexistent key on a dict the YamlObject returns an
> empty element. This element can happily be casted to a string which is
> unexpected. For example the following statement:
> yamlDict["nonexistant"].get<string>("default") is expected to return
> "default" but actually returns "". Fix this by introducing a empty type
> to distinguish between an empty YamlObject and a YamlObject of type
> value containing an empty string. For completeness add an isEmpty()
> function to be able to test for that type.
> 
> Signed-off-by: Stefan Klug <stefan.klug at ideasonboard.com>
> ---
>  include/libcamera/internal/yaml_parser.h |  5 +++++
>  src/libcamera/yaml_parser.cpp            | 20 ++++++++++++++------
>  test/yaml-parser.cpp                     |  6 ++++++
>  3 files changed, 25 insertions(+), 6 deletions(-)
> 
> diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h
> index 16708e488d88..33c3a96fed47 100644
> --- a/include/libcamera/internal/yaml_parser.h
> +++ b/include/libcamera/internal/yaml_parser.h
> @@ -159,6 +159,10 @@ public:
>  	{
>  		return type_ == Type::Dictionary;
>  	}
> +	bool isEmpty() const
> +	{
> +		return type_ == Type::Empty;
> +	}

I think `constexpr explicit operator bool() const` would be a good addition as well.
Then you can just do e.g.

  if (auto y = x["something"]) {
    // ...
  }


> 
>  	std::size_t size() const;
> 
> @@ -212,6 +216,7 @@ private:
>  		Dictionary,
>  		List,
>  		Value,
> +		Empty,
>  	};
> [...]


Regards,
Barnabás Pőcze


More information about the libcamera-devel mailing list