Change CommandParser::parse to return StatusOr#45752
Conversation
Signed-off-by: Yan Avlasov <yavlasov@google.com>
kyessenov
left a comment
There was a problem hiding this comment.
Thanks, this is a good change!
botengyao
left a comment
There was a problem hiding this comment.
could you fix the CI?
/wait
Signed-off-by: yavlasov <yavlasov@google.com>
| */ | ||
| virtual FormatterProviderPtr parse(absl::string_view command, absl::string_view command_arg, | ||
| absl::optional<size_t> max_length) const PURE; | ||
| virtual FormatterProviderResult parse(absl::string_view command, absl::string_view command_arg, |
There was a problem hiding this comment.
I think this would be more readable as a straight absl::StatusOr<FormatterProviderPtr> return type. At the call-sites using FormatterProviderResult sounds like a result from a FormatterProvider, not a result that may be a FormatterProvider.
Alternatively, MaybeFormatterProvider or FormatterProviderOrStatus would also work, but just doing it straight with the StatusOr and FormatterProviderPtr helper to avoid double-nesting seems simple and self-explanatory.
| // Try built-in command parsers to create the formatter. | ||
| for (const auto& parser : BuiltInCommandParserFactoryHelper::commandParsers()) { | ||
| auto formatter = parser->parse(command, param, max_length); | ||
| auto formatter_result = parser->parse(command, param, max_length); |
There was a problem hiding this comment.
I usually get berated for auto and I think this one is a good case for not doing it since it's again not clear without reading more of the function for context what a formatter_result is.
(The auto below is fine since it's explained by this one being a StatusOr)
There was a problem hiding this comment.
Fixed, including other places
| auto formatter = std::move(formatter_result).value(); | ||
| if (formatter != nullptr) { | ||
| return formatter; | ||
| } |
There was a problem hiding this comment.
Should this be just ASSERT that it's not nullptr, or is there a valid case where a non-error-status is returned, but also no pointer?
There was a problem hiding this comment.
Currently, it can return nullptr. Not sure if this is an error or not. I will be looking into these case then removing exceptions from parse overrides.
| auto provider = commands[0]->parse("COMMAND_EXTENSION", "", max_length).value(); | ||
| ASSERT_TRUE(provider != nullptr); |
There was a problem hiding this comment.
That's a test-crash in the case of parse returning an error status.
Recommend instead
| auto provider = commands[0]->parse("COMMAND_EXTENSION", "", max_length).value(); | |
| ASSERT_TRUE(provider != nullptr); | |
| auto status_or_provider = commands[0]->parse("COMMAND_EXTENSION", "", max_length); | |
| ASSERT_THAT(status_or_provider, IsOkAndHolds(NotNull()); |
(With #include "test/test_common/status_utility.h" and appropriate usings)
Same comment twice more in this file, once in test/common/formatter/substitution_formatter_test.cc, and many times in test/extensions/filters/udp/dns_filter/dns_filter_access_log_test.cc (arguably not in the helper function), and many in cel_test.
For these later 'many' cases I would suggest bumping the provider-collection up into the test class's initialization or a helper function, to cut down on repetition and make it clearer that the tests aren't really testing the return value of the parse function.
There was a problem hiding this comment.
Fixed, including other cases. Thanks for suggestion.
| auto provider = parser->parse("NOT_SECRET", "my-token", absl::nullopt).value(); | ||
| EXPECT_EQ(nullptr, provider); |
There was a problem hiding this comment.
Hm, so it is expected to sometimes return a non-error null?
Signed-off-by: yavlasov <yavlasov@google.com>
- Change CommandParser::parse to return absl::StatusOr<FormatterProviderPtr> - Update all formatter implementations and extensions - Update all callers and tests to use new StatusOr return type - Preserve CVE-2026-47220 fix (null checks in getHostFromHeaders) - Keep CVE tests for empty headers in substitution_formatter_test.cc Co-authored-by: yanavlasov <6360027+yanavlasov@users.noreply.github.com>
Change CommandParser::parse return value to StatusOr<>. This is preparation for removing exceptions from this method. Presently no parse overrides return error status and C++ exceptions still in use.
Risk Level: low, noop
Testing: unit tests
Docs Changes: no
Release Notes: no
Platform Specific Features: no