Skip to content

Commit

Permalink
OpaqueValue serialization
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 591327512
  • Loading branch information
jcking authored and copybara-github committed Dec 15, 2023
1 parent cba1eb2 commit fa959cd
Show file tree
Hide file tree
Showing 69 changed files with 3,933 additions and 1,088 deletions.
10 changes: 9 additions & 1 deletion common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ cc_library(

cc_library(
name = "any",
srcs = ["any.cc"],
hdrs = ["any.h"],
deps = [
"//internal:strings",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
Expand Down Expand Up @@ -191,7 +193,7 @@ cc_library(
deps = [
":data",
":memory",
":type_testing",
":memory_testing",
"@com_google_absl//absl/types:optional",
],
)
Expand Down Expand Up @@ -286,6 +288,7 @@ cc_library(
) + [
"type_factory.cc",
"value_factory.cc",
"value_provider.cc",
],
hdrs = glob(
[
Expand All @@ -307,6 +310,7 @@ cc_library(
"value_provider.h",
],
deps = [
":any",
":casting",
":json",
":memory",
Expand All @@ -320,6 +324,7 @@ cc_library(
"//internal:names",
"//internal:no_destructor",
"//internal:overloaded",
"//internal:serialize",
"//internal:status_macros",
"//internal:strings",
"//internal:time",
Expand Down Expand Up @@ -355,9 +360,11 @@ cc_test(
"type_factory_test.cc",
"type_test.cc",
"value_factory_test.cc",
"value_provider_test.cc",
"value_test.cc",
],
deps = [
":any",
":casting",
":data",
":json",
Expand All @@ -366,6 +373,7 @@ cc_test(
":sized_input_view",
":value_kind",
":value_testing",
"//internal:status_macros",
"//internal:testing",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_set",
Expand Down
40 changes: 40 additions & 0 deletions common/any.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/any.h"

#include <string>

#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "internal/strings.h"

namespace cel {

std::string Any::DebugString() const {
std::string value_scratch;
absl::string_view value_view;
if (auto flat = value().TryFlat(); flat.has_value()) {
value_view = *flat;
} else {
value_scratch = static_cast<std::string>(value());
value_view = value_scratch;
}
return absl::StrCat("google.protobuf.Any{type_url: ",
internal::FormatStringLiteral(type_url()),
", value: ", internal::FormatBytesLiteral(value_view),
"}");
}

} // namespace cel
15 changes: 15 additions & 0 deletions common/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef THIRD_PARTY_CEL_CPP_COMMON_ANY_H_
#define THIRD_PARTY_CEL_CPP_COMMON_ANY_H_

#include <ostream>
#include <string>
#include <utility>

Expand Down Expand Up @@ -47,11 +48,25 @@ class Any final {
return value_;
}

std::string DebugString() const;

private:
std::string type_url_;
absl::Cord value_;
};

inline std::ostream& operator<<(std::ostream& out, const Any& any) {
return out << any.DebugString();
}

inline bool operator==(const Any& lhs, const Any& rhs) {
return lhs.type_url() == rhs.type_url() && lhs.value() == rhs.value();
}

inline bool operator!=(const Any& lhs, const Any& rhs) {
return !operator==(lhs, rhs);
}

inline Any MakeAny(std::string type_url, absl::Cord value) {
Any any;
any.set_type_url(std::move(type_url));
Expand Down
11 changes: 7 additions & 4 deletions common/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,13 @@ class ABSL_ATTRIBUTE_TRIVIAL_ABI Unique final {
other.ptr_ = nullptr;
}

template <typename U, typename = std::enable_if_t<std::conjunction_v<
std::negation<std::is_same<U, T>>,
std::is_same<U, std::remove_const_t<T>>,
std::negation<std::is_const<U>>>>>
template <typename U,
typename = std::enable_if_t<std::conjunction_v<
std::negation<std::is_same<U, T>>, std::is_convertible<U*, T*>,
std::is_polymorphic<std::remove_const_t<U>>,
std::is_polymorphic<std::remove_const_t<T>>,
std::is_base_of<std::remove_const_t<T>, std::remove_const_t<U>>,
std::has_virtual_destructor<std::remove_const_t<T>>>>>
// NOLINTNEXTLINE(google-explicit-constructor)
Unique(Unique<U>&& other) noexcept
: ptr_(other.ptr_), memory_management_(other.memory_management_) {
Expand Down
14 changes: 11 additions & 3 deletions common/type_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace cel {

class TypeFactory;

// `TypeProvider` is an interface which allows querying type-related
// information. It handles type introspection, but not type reflection. That is,
// it is not capable of instantiating new values or understanding values. Its
Expand All @@ -38,21 +40,27 @@ class TypeProvider {

// `FindType` find the type corresponding to name `name`.
virtual absl::StatusOr<TypeView> FindType(
absl::string_view name, Type& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) = 0;
TypeFactory& type_factory, absl::string_view name,
Type& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) = 0;

// `FindStructTypeFieldByName` find the name, number, and type of the field
// `name` in type `type`.
virtual absl::StatusOr<StructTypeFieldView> FindStructTypeFieldByName(
absl::string_view type, absl::string_view name,
TypeFactory& type_factory, absl::string_view type, absl::string_view name,
StructTypeField& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) = 0;

// `FindStructTypeFieldByName` find the name, number, and type of the field
// `name` in struct type `type`.
virtual absl::StatusOr<StructTypeFieldView> FindStructTypeFieldByName(
StructTypeView type, absl::string_view name,
TypeFactory& type_factory, StructTypeView type, absl::string_view name,
StructTypeField& scratch ABSL_ATTRIBUTE_LIFETIME_BOUND) = 0;
};

Shared<TypeProvider> NewThreadCompatibleTypeProvider(
MemoryManagerRef memory_manager);

Shared<TypeProvider> NewThreadSafeTypeProvider(MemoryManagerRef memory_manager);

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPE_PROVIDER_H_
6 changes: 6 additions & 0 deletions common/type_testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "common/memory.h"
#include "common/memory_testing.h"
#include "common/type_factory.h"
#include "common/type_provider.h"

namespace cel::common_internal {

Expand All @@ -31,17 +32,22 @@ class ThreadCompatibleTypeTest : public ThreadCompatibleMemoryTest<Ts...> {
void SetUp() override {
Base::SetUp();
type_factory_ = NewThreadCompatibleTypeFactory(this->memory_manager());
type_provider_ = NewThreadCompatibleTypeProvider(this->memory_manager());
}

void TearDown() override {
type_provider_.reset();
type_factory_.reset();
Base::TearDown();
}

TypeFactory& type_factory() const { return **type_factory_; }

TypeProvider& type_provider() const { return **type_provider_; }

private:
absl::optional<Shared<TypeFactory>> type_factory_;
absl::optional<Shared<TypeProvider>> type_provider_;
};

} // namespace cel::common_internal
Expand Down
13 changes: 6 additions & 7 deletions common/types/thread_compatible_type_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@

namespace cel::common_internal {

class ThreadCompatibleTypeFactory final : public TypeFactory {
class ThreadCompatibleTypeFactory : public virtual TypeFactory {
public:
explicit ThreadCompatibleTypeFactory(MemoryManagerRef memory_manager)
: memory_manager_(memory_manager) {}

MemoryManagerRef GetMemoryManager() const override { return memory_manager_; }
MemoryManagerRef GetMemoryManager() const final { return memory_manager_; }

private:
ListType CreateListTypeImpl(TypeView element) override;
ListType CreateListTypeImpl(TypeView element) final;

MapType CreateMapTypeImpl(TypeView key, TypeView value) override;
MapType CreateMapTypeImpl(TypeView key, TypeView value) final;

StructType CreateStructTypeImpl(absl::string_view name) override;
StructType CreateStructTypeImpl(absl::string_view name) final;

OpaqueType CreateOpaqueTypeImpl(
absl::string_view name,
const SizedInputView<TypeView>& parameters) override;
absl::string_view name, const SizedInputView<TypeView>& parameters) final;

MemoryManagerRef memory_manager_;
ListTypeCacheMap list_types_;
Expand Down
50 changes: 50 additions & 0 deletions common/types/thread_compatible_type_provider.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// IWYU pragma: private

#include "common/types/thread_compatible_type_provider.h"

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

namespace cel::common_internal {

absl::StatusOr<TypeView> ThreadCompatibleTypeProvider::FindType(
TypeFactory&, absl::string_view name, Type&) {
return absl::NotFoundError(absl::StrCat("no such type: ", name));
}

absl::StatusOr<StructTypeFieldView>
ThreadCompatibleTypeProvider::FindStructTypeFieldByName(TypeFactory&,
absl::string_view type,
absl::string_view,
StructTypeField&) {
return absl::NotFoundError(absl::StrCat("no such struct type: ", type));
}

absl::StatusOr<StructTypeFieldView>
ThreadCompatibleTypeProvider::FindStructTypeFieldByName(TypeFactory&,
StructTypeView type,
absl::string_view,
StructTypeField&) {
return absl::NotFoundError(
absl::StrCat("no such struct type: ", type.DebugString()));
}

} // namespace cel::common_internal
56 changes: 56 additions & 0 deletions common/types/thread_compatible_type_provider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// IWYU pragma: private

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPES_THREAD_COMPATIBLE_TYPE_PROVIDER_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPES_THREAD_COMPATIBLE_TYPE_PROVIDER_H_

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

namespace cel::common_internal {

// `ThreadCompatibleTypeProvider` is a basic implementation of `TypeProvider`
// which is thread compatible. By default this implementation just returns
// `NOT_FOUND` for most methods.
class ThreadCompatibleTypeProvider : public virtual TypeProvider {
public:
explicit ThreadCompatibleTypeProvider(MemoryManagerRef memory_manager)
: memory_manager_(memory_manager) {}

MemoryManagerRef GetMemoryManager() const final { return memory_manager_; }

absl::StatusOr<TypeView> FindType(TypeFactory& type_factory,
absl::string_view name,
Type& scratch) override;

absl::StatusOr<StructTypeFieldView> FindStructTypeFieldByName(
TypeFactory& type_factory, absl::string_view type, absl::string_view name,
StructTypeField& scratch) override;

absl::StatusOr<StructTypeFieldView> FindStructTypeFieldByName(
TypeFactory& type_factory, StructTypeView type, absl::string_view name,
StructTypeField& scratch) override;

private:
MemoryManagerRef memory_manager_;
};

} // namespace cel::common_internal

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPES_THREAD_COMPATIBLE_TYPE_PROVIDER_H_
13 changes: 6 additions & 7 deletions common/types/thread_safe_type_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,22 @@

namespace cel::common_internal {

class ThreadSafeTypeFactory final : public TypeFactory {
class ThreadSafeTypeFactory : public virtual TypeFactory {
public:
explicit ThreadSafeTypeFactory(MemoryManagerRef memory_manager)
: memory_manager_(memory_manager) {}

MemoryManagerRef GetMemoryManager() const override { return memory_manager_; }
MemoryManagerRef GetMemoryManager() const final { return memory_manager_; }

private:
ListType CreateListTypeImpl(TypeView element) override;
ListType CreateListTypeImpl(TypeView element) final;

MapType CreateMapTypeImpl(TypeView key, TypeView value) override;
MapType CreateMapTypeImpl(TypeView key, TypeView value) final;

StructType CreateStructTypeImpl(absl::string_view name) override;
StructType CreateStructTypeImpl(absl::string_view name) final;

OpaqueType CreateOpaqueTypeImpl(
absl::string_view name,
const SizedInputView<TypeView>& parameters) override;
absl::string_view name, const SizedInputView<TypeView>& parameters) final;

MemoryManagerRef memory_manager_;
absl::Mutex list_types_mutex_;
Expand Down
Loading

0 comments on commit fa959cd

Please # to comment.