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
18 changes: 18 additions & 0 deletions checker/optional.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class OptionalNames {
static constexpr char kOptionalOrValue[] = "orValue";
static constexpr char kOptionalSelect[] = "_?._";
static constexpr char kOptionalIndex[] = "_[?_]";
static constexpr char kOptionalFirst[] = "first";
static constexpr char kOptionalLast[] = "last";
};

class OptionalOverloads {
Expand All @@ -107,6 +109,8 @@ class OptionalOverloads {
"map_optindex_optional_value";
static constexpr char kOptionalMapOptionalIndexValue[] =
"optional_map_optindex_optional_value";
static constexpr char kListFirst[] = "list_first";
static constexpr char kListLast[] = "list_last";
// Syntactic sugar for chained indexing.
static constexpr char kOptionalListIndexInt[] = "optional_list_index_int";
static constexpr char kOptionalMapIndexValue[] = "optional_map_index_value";
Expand Down Expand Up @@ -181,6 +185,18 @@ absl::Status RegisterOptionalDecls(TypeCheckerBuilder& builder) {
OptionalOfV(), OptionalMapOfKV(),
TypeParamType("K"))));

CEL_ASSIGN_OR_RETURN(
auto first,
MakeFunctionDecl(OptionalNames::kOptionalFirst,
MakeMemberOverloadDecl(OptionalOverloads::kListFirst,
OptionalOfV(), ListOfV())));

CEL_ASSIGN_OR_RETURN(
auto last,
MakeFunctionDecl(OptionalNames::kOptionalLast,
MakeMemberOverloadDecl(OptionalOverloads::kListLast,
OptionalOfV(), ListOfV())));

CEL_ASSIGN_OR_RETURN(
auto index,
MakeFunctionDecl(
Expand All @@ -203,6 +219,8 @@ absl::Status RegisterOptionalDecls(TypeCheckerBuilder& builder) {
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(or_value)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(opt_index)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(select)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(first)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(last)));
CEL_RETURN_IF_ERROR(builder.MergeFunction(std::move(index)));

return absl::OkStatus();
Expand Down
4 changes: 4 additions & 0 deletions checker/optional_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ INSTANTIATE_TEST_SUITE_P(
IsOptionalType(TypeSpec(PrimitiveType::kInt64))},
TestCase{"{0: {0: 1}}[?1]['']", _, "no matching overload for '_[_]'"},
TestCase{"{0: {0: 1}}[?1][?'']", _, "no matching overload for '_[?_]'"},
TestCase{"[1, 2, 3].first()",
IsOptionalType(TypeSpec(PrimitiveType::kInt64))},
TestCase{"[1, 2, 3].last()",
IsOptionalType(TypeSpec(PrimitiveType::kInt64))},
TestCase{"optional.of('abc').optMap(x, x + 'def')",
IsOptionalType(TypeSpec(PrimitiveType::kString))},
TestCase{"optional.of('abc').optFlatMap(x, optional.of(x + 'def'))",
Expand Down
37 changes: 37 additions & 0 deletions runtime/optional_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,33 @@ absl::StatusOr<Value> OptionalOptIndexOptionalValue(
return ErrorValue{runtime_internal::CreateNoMatchingOverloadError("_[?_]")};
}

absl::StatusOr<Value> ListFirst(const cel::ListValue& list,
const google::protobuf::DescriptorPool* descriptor_pool,
google::protobuf::MessageFactory* message_factory,
google::protobuf::Arena* arena) {
CEL_ASSIGN_OR_RETURN(size_t size, list.Size());
if (size == 0) {
return Value(OptionalValue::None());
}
CEL_ASSIGN_OR_RETURN(Value value,
list.Get(0, descriptor_pool, message_factory, arena));
return Value(OptionalValue::Of(std::move(value), arena));
}

absl::StatusOr<Value> ListLast(const cel::ListValue& list,
const google::protobuf::DescriptorPool* descriptor_pool,
google::protobuf::MessageFactory* message_factory,
google::protobuf::Arena* arena) {
CEL_ASSIGN_OR_RETURN(size_t size, list.Size());
if (size == 0) {
return Value(OptionalValue::None());
}
CEL_ASSIGN_OR_RETURN(Value value,
list.Get(static_cast<int64_t>(size) - 1, descriptor_pool,
message_factory, arena));
return Value(OptionalValue::Of(std::move(value), arena));
}

absl::StatusOr<Value> ListUnwrapOpt(
const ListValue& list,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
Expand Down Expand Up @@ -332,6 +359,16 @@ absl::Status RegisterOptionalTypeFunctions(FunctionRegistry& registry,
"unwrapOpt", true),
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::WrapFunction(
&ListUnwrapOpt)));
CEL_RETURN_IF_ERROR(registry.Register(
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::CreateDescriptor(
"first", true),
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::WrapFunction(
&ListFirst)));
CEL_RETURN_IF_ERROR(registry.Register(
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::CreateDescriptor(
"last", true),
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::WrapFunction(
&ListLast)));
return absl::OkStatus();
}

Expand Down
4 changes: 4 additions & 0 deletions runtime/optional_types_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ INSTANTIATE_TEST_SUITE_P(
{"list_unwrapOpt_no_none",
"[optional.of(42), optional.of(\"a\")].unwrapOpt() == [42, \"a\"]",
BoolValueIs(true)},
{"list_first", "[1, 2, 3].first()", OptionalValueIs(IntValueIs(1))},
{"list_first_empty", "[].first()", OptionalValueIsEmpty()},
{"list_last", "[1, 2, 3].last()", OptionalValueIs(IntValueIs(3))},
{"list_last_empty", "[].last()", OptionalValueIsEmpty()},
}),
/*enable_short_circuiting*/ testing::Bool()));

Expand Down