Skip to content

Commit

Permalink
Add default empty list and map implementation
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 597858004
  • Loading branch information
jcking authored and copybara-github committed Jan 12, 2024
1 parent 575c3cf commit f29078c
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 111 deletions.
13 changes: 12 additions & 1 deletion common/type_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,22 @@ TypeProvider::FindStructTypeFieldByName(TypeFactory& type_factory,
const auto& well_known_types = GetWellKnownTypesMap();
if (auto it = well_known_types.find(type); it != well_known_types.end()) {
return it->second.FieldByName(name);
;
}
return FindStructTypeFieldByNameImpl(type_factory, type, name, scratch);
}

absl::StatusOr<absl::optional<TypeView>> TypeProvider::FindTypeImpl(
TypeFactory&, absl::string_view, Type&) const {
return absl::nullopt;
}

absl::StatusOr<absl::optional<StructTypeFieldView>>
TypeProvider::FindStructTypeFieldByNameImpl(TypeFactory&, absl::string_view,
absl::string_view,
StructTypeField&) const {
return absl::nullopt;
}

Shared<TypeProvider> NewThreadCompatibleTypeProvider(
MemoryManagerRef memory_manager) {
return memory_manager
Expand Down
40 changes: 0 additions & 40 deletions common/types/legacy_type_provider.cc

This file was deleted.

14 changes: 0 additions & 14 deletions common/types/legacy_type_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPES_LEGACY_TYPE_PROVIDER_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPES_LEGACY_TYPE_PROVIDER_H_

#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "common/type.h"
#include "common/type_provider.h"

namespace cel::common_internal {
Expand All @@ -31,16 +27,6 @@ namespace cel::common_internal {
class LegacyTypeProvider : public virtual TypeProvider {
public:
LegacyTypeProvider() = default;

protected:
absl::StatusOr<absl::optional<TypeView>> FindTypeImpl(
TypeFactory& type_factory, absl::string_view name,
Type& scratch) const override;

absl::StatusOr<absl::optional<StructTypeFieldView>>
FindStructTypeFieldByNameImpl(TypeFactory& type_factory,
absl::string_view type, absl::string_view name,
StructTypeField& scratch) const override;
};

} // namespace cel::common_internal
Expand Down
11 changes: 11 additions & 0 deletions common/value_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "common/casting.h"
#include "common/json.h"
#include "common/memory.h"
#include "common/type.h"
#include "common/value.h"
#include "common/value_factory.h"
#include "common/values/thread_compatible_value_provider.h"
Expand Down Expand Up @@ -933,6 +934,16 @@ absl::StatusOr<absl::optional<Value>> ValueProvider::DeserializeValueImpl(
return absl::nullopt;
}

absl::StatusOr<absl::optional<Unique<StructValueBuilder>>>
ValueProvider::NewStructValueBuilder(ValueFactory&, StructTypeView) const {
return absl::nullopt;
}

absl::StatusOr<absl::optional<ValueView>> ValueProvider::FindValue(
ValueFactory&, absl::string_view, Value&) const {
return absl::nullopt;
}

Shared<ValueProvider> NewThreadCompatibleValueProvider(
MemoryManagerRef memory_manager) {
return memory_manager
Expand Down
5 changes: 2 additions & 3 deletions common/value_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class ValueProvider : public virtual TypeProvider {
// `NewStructValueBuilder` returns a new `StructValueBuilder` for the
// corresponding `StructType` `type`.
virtual absl::StatusOr<absl::optional<Unique<StructValueBuilder>>>
NewStructValueBuilder(ValueFactory& value_factory,
StructTypeView type) const = 0;
NewStructValueBuilder(ValueFactory& value_factory, StructTypeView type) const;

// `NewValueBuilder` returns a new `ValueBuilder` for the corresponding type
// `name`. It is primarily used to handle wrapper types which sometimes show
Expand All @@ -58,7 +57,7 @@ class ValueProvider : public virtual TypeProvider {
// can be used to translate enum names to numeric values.
virtual absl::StatusOr<absl::optional<ValueView>> FindValue(
ValueFactory& value_factory, absl::string_view name,
Value& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) const = 0;
Value& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) const;

// `DeserializeValue` deserializes the bytes of `value` according to
// `type_url`. Returns `NOT_FOUND` if `type_url` is unrecognized.
Expand Down
38 changes: 0 additions & 38 deletions common/values/legacy_value_provider.cc

This file was deleted.

15 changes: 0 additions & 15 deletions common/values/legacy_value_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,14 @@
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_LEGACY_VALUE_PROVIDER_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_LEGACY_VALUE_PROVIDER_H_

#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "common/memory.h"
#include "common/type.h"
#include "common/types/legacy_type_provider.h"
#include "common/value.h"
#include "common/value_factory.h"
#include "common/value_provider.h"

namespace cel::common_internal {

class LegacyValueProvider : public LegacyTypeProvider, public ValueProvider {
public:
LegacyValueProvider() : LegacyTypeProvider() {}

absl::StatusOr<absl::optional<Unique<StructValueBuilder>>>
NewStructValueBuilder(ValueFactory& value_factory,
StructTypeView type) const override;

absl::StatusOr<absl::optional<ValueView>> FindValue(
ValueFactory& value_factory, absl::string_view name,
Value& scratch) const override;
};

} // namespace cel::common_internal
Expand Down
1 change: 1 addition & 0 deletions eval/public/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ cc_library(
"//eval/public/structs:legacy_type_info_apis",
"//extensions/protobuf:memory_manager",
"//internal:casts",
"//internal:no_destructor",
"//internal:status_macros",
"//internal:utf8",
"@com_google_absl//absl/base:core_headers",
Expand Down
52 changes: 52 additions & 0 deletions eval/public/cel_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "eval/public/cel_value_internal.h"
#include "eval/public/structs/legacy_type_info_apis.h"
#include "extensions/protobuf/memory_manager.h"
#include "internal/no_destructor.h"

namespace google::api::expr::runtime {

Expand Down Expand Up @@ -228,6 +229,57 @@ const std::string CelValue::DebugString() const {
InternalVisit<std::string>(DebugStringVisitor{&arena}));
}

namespace {

class EmptyCelList final : public CelList {
public:
static const EmptyCelList* Get() {
static const cel::internal::NoDestructor<EmptyCelList> instance;
return &*instance;
}

CelValue operator[](int index) const override {
static const CelError* invalid_argument =
new CelError(absl::InvalidArgumentError("index out of bounds"));
return CelValue::CreateError(invalid_argument);
}

int size() const override { return 0; }

bool empty() const override { return true; }
};

class EmptyCelMap final : public CelMap {
public:
static const EmptyCelMap* Get() {
static const cel::internal::NoDestructor<EmptyCelMap> instance;
return &*instance;
}

absl::optional<CelValue> operator[](CelValue key) const override {
return absl::nullopt;
}

absl::StatusOr<bool> Has(const CelValue& key) const override {
CEL_RETURN_IF_ERROR(CelValue::CheckMapKeyType(key));
return false;
}

int size() const override { return 0; }

bool empty() const override { return true; }

absl::StatusOr<const CelList*> ListKeys() const override {
return EmptyCelList::Get();
}
};

} // namespace

CelValue CelValue::CreateList() { return CreateList(EmptyCelList::Get()); }

CelValue CelValue::CreateMap() { return CreateMap(EmptyCelMap::Get()); }

CelValue CreateErrorValue(cel::MemoryManagerRef manager,
absl::string_view message,
absl::StatusCode error_code) {
Expand Down
6 changes: 6 additions & 0 deletions eval/public/cel_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,17 @@ class CelValue {
return CelValue(value);
}

// Creates a CelValue backed by an empty immutable list.
static CelValue CreateList();

static CelValue CreateMap(const CelMap* value) {
CheckNullPointer(value, Type::kMap);
return CelValue(value);
}

// Creates a CelValue backed by an empty immutable map.
static CelValue CreateMap();

static CelValue CreateUnknownSet(const UnknownSet* value) {
CheckNullPointer(value, Type::kUnknownSet);
return CelValue(value);
Expand Down

0 comments on commit f29078c

Please # to comment.