Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions eval/eval/container_access_step.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "eval/eval/container_access_step.h"

#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
Expand All @@ -8,6 +9,7 @@
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
Expand Down Expand Up @@ -159,6 +161,17 @@ void LookupInMap(const MapValue& cel_map, const Value& key,
ABSL_DCHECK(!result.IsUnknown());
}

absl::Status CreateNoSuchIndexError(int64_t index, size_t size) {
return absl::UnknownError(
absl::StrCat("Index error: index=", index, " size=", size));
}

[[nodiscard]]
bool IsNoSuchIndexError(const absl::Status& status) {
return status.code() == absl::StatusCode::kUnknown &&
absl::StartsWith(status.message(), "Index error: index=");
}

void LookupInList(const ListValue& cel_list, const Value& key,
ExecutionFrameBase& frame, Value& result) {
absl::optional<int64_t> maybe_idx;
Expand Down Expand Up @@ -187,10 +200,8 @@ void LookupInList(const ListValue& cel_list, const Value& key,
return;
}
if (idx < 0 || idx >= *size) {
result =
cel::ErrorValue::From(absl::UnknownError(absl::StrCat(
"Index error: index=", idx, " size=", *size)),
frame.arena());
result = cel::ErrorValue::From(CreateNoSuchIndexError(idx, *size),
frame.arena());
return;
}

Expand Down Expand Up @@ -270,7 +281,9 @@ void PerformLookup(ExecutionFrameBase& frame, const Value& container,
optional_value.Value(&value);
LookupInContainer(value, key, frame, result);
if (auto error_value = result.AsError();
error_value && cel::IsNoSuchKey(*error_value)) {
error_value.has_value() &&
(cel::IsNoSuchKey(*error_value) ||
IsNoSuchIndexError(error_value->ToStatus()))) {
result = cel::OptionalValue::None();
return;
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/optional_types_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ using ::cel::test::BoolValueIs;
using ::cel::test::IntValueIs;
using ::cel::test::OptionalValueIs;
using ::cel::test::OptionalValueIsEmpty;
using ::cel::test::StringValueIs;
using ::cel::expr::ParsedExpr;
using ::google::api::expr::parser::Parse;
using ::google::api::expr::parser::ParserOptions;
Expand Down Expand Up @@ -264,6 +265,8 @@ INSTANTIATE_TEST_SUITE_P(
{"list_first_empty", "[].first()", OptionalValueIsEmpty()},
{"list_last", "[1, 2, 3].last()", OptionalValueIs(IntValueIs(3))},
{"list_last_empty", "[].last()", OptionalValueIsEmpty()},
{"list_empty_index_orValue", "optional.of([])[0].orValue(\"test\")",
StringValueIs("test")},
}),
/*enable_short_circuiting*/ testing::Bool()));

Expand Down
Loading