Skip to content
Open
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
169 changes: 169 additions & 0 deletions common/typedef/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# Copyright 2026 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.

load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

cc_library(
name = "typedef",
srcs = ["typedef.cc"],
hdrs = ["typedef.h"],
deps = [
"//common:ast",
"//common:constant",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
],
)

cc_test(
name = "typedef_test",
srcs = ["typedef_test.cc"],
deps = [
":typedef",
"//common:ast",
"//common:constant",
"//internal:testing",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
],
)

# NOTE: The YAML targets below depend on yaml-cpp (//third_party/yamlcpp),
# which is used for open-source CEL compatibility (and therefore cannot be
# trivially migrated to Crubit/Rust YAML parsers) and requires C++ exceptions.
# The implementation using //third_party/yamlcpp carefully catches and handles
# exceptions, so code using these targets does not need the -fexceptions flag.

cc_library(
name = "yaml_helpers",
srcs = ["yaml_helpers.cc"],
hdrs = ["yaml_helpers.h"],
copts = ["-fexceptions"],
features = ["-use_header_modules"],
deps = [
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@yaml-cpp",
],
)

cc_library(
name = "schema_yaml",
srcs = ["schema_yaml.cc"],
hdrs = ["schema_yaml.h"],
deps = [
":yaml_helpers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
],
)

cc_test(
name = "schema_yaml_test",
srcs = ["schema_yaml_test.cc"],
copts = ["-fexceptions"],
features = ["-use_header_modules"],
deps = [
":schema_yaml",
"//internal:testing",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/strings:string_view",
"@yaml-cpp",
],
)

cc_library(
name = "typedef_yaml",
srcs = ["typedef_yaml.cc"],
hdrs = [
"typedef_yaml.h",
],
copts = ["-fexceptions"],
features = ["-use_header_modules"],
deps = [
":schema_yaml",
":typedef",
":yaml_helpers",
"//common:ast",
"//common:constant",
"//common:signature",
"//internal:status_macros",
"//internal:strings",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
"@yaml-cpp",
],
)

cc_test(
name = "typedef_yaml_test",
srcs = ["typedef_yaml_test.cc"],
copts = ["-fexceptions"],
features = ["-use_header_modules"],
deps = [
":schema_yaml",
":typedef",
":typedef_yaml",
":yaml_helpers",
":yaml_test_helpers",
"//common:ast",
"//common:constant",
"//internal:status_macros",
"//internal:testing",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@yaml-cpp",
],
)

cc_library(
name = "yaml_test_helpers",
testonly = True,
srcs = ["yaml_test_helpers.cc"],
hdrs = ["yaml_test_helpers.h"],
deps = [
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
],
)
118 changes: 118 additions & 0 deletions common/typedef/schema_yaml.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2026 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/typedef/schema_yaml.h"

#include <any>
#include <memory>
#include <utility>
#include <vector>

#include "absl/base/nullability.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/typedef/yaml_helpers.h"

namespace cel {

absl::StatusOr<std::any> SchemaYaml::DecodeStructProperties(
absl::string_view yaml, const YAML::Node& node) const {
if (typedef_internal::IsEmptyYamlNode(node)) {
return std::any();
}
return typedef_internal::YamlError(
yaml, node,
absl::StrCat("Custom struct properties are not supported for schema '",
name(), "'"));
}

absl::StatusOr<std::any> SchemaYaml::DecodeFieldProperties(
absl::string_view yaml, const YAML::Node& node) const {
if (typedef_internal::IsEmptyYamlNode(node)) {
return std::any();
}
return typedef_internal::YamlError(
yaml, node,
absl::StrCat("Custom field properties are not supported for schema '",
name(), "'"));
}

absl::StatusOr<std::any> SchemaYaml::DecodeEnumProperties(
absl::string_view yaml, const YAML::Node& node) const {
if (typedef_internal::IsEmptyYamlNode(node)) {
return std::any();
}
return typedef_internal::YamlError(
yaml, node,
absl::StrCat("Custom enum properties are not supported for schema '",
name(), "'"));
}

absl::StatusOr<std::any> SchemaYaml::DecodeEnumConstantProperties(
absl::string_view yaml, const YAML::Node& node) const {
if (typedef_internal::IsEmptyYamlNode(node)) {
return std::any();
}
return typedef_internal::YamlError(
yaml, node,
absl::StrCat(
"Custom enum constant properties are not supported for schema '",
name(), "'"));
}

absl::Status SchemaYaml::EncodeStructProperties(const std::any& properties,
YAML::Emitter& out) const {
return absl::UnimplementedError(absl::StrCat(
"Custom struct properties are not supported for schema '", name(), "'"));
}

absl::Status SchemaYaml::EncodeFieldProperties(const std::any& properties,
YAML::Emitter& out) const {
return absl::UnimplementedError(absl::StrCat(
"Custom field properties are not supported for schema '", name(), "'"));
}

absl::Status SchemaYaml::EncodeEnumProperties(const std::any& properties,
YAML::Emitter& out) const {
return absl::UnimplementedError(absl::StrCat(
"Custom enum properties are not supported for schema '", name(), "'"));
}

absl::Status SchemaYaml::EncodeEnumConstantProperties(
const std::any& properties, YAML::Emitter& out) const {
return absl::UnimplementedError(absl::StrCat(
"Custom enum constant properties are not supported for schema '", name(),
"'"));
}

SchemaYamlRegistry::SchemaYamlRegistry(
std::vector<std::unique_ptr<const SchemaYaml>> schemas) {
schemas_.reserve(schemas.size());
for (std::unique_ptr<const SchemaYaml>& schema : schemas) {
Add(std::move(schema));
}
}

const SchemaYaml* absl_nullable SchemaYamlRegistry::Find(
absl::string_view schema_name) const {
auto it = schemas_.find(schema_name);
if (it == schemas_.end()) {
return nullptr;
}
return it->second.get();
}

} // namespace cel
109 changes: 109 additions & 0 deletions common/typedef/schema_yaml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2026 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.

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_SCHEMA_YAML_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_SCHEMA_YAML_H_

#include <any>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "absl/base/nullability.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"

namespace YAML {
class Emitter;
class Node;
} // namespace YAML

namespace cel {

// Interface representing a YAML codec for a schema (e.g. "json",
// "proto2", "proto3") capable of decoding and encoding schema-specific property
// objects.
class SchemaYaml {
public:
virtual ~SchemaYaml() = default;

// Returns the unique schema identifier (e.g. "json", "proto2", "proto3").
virtual absl::string_view name() const = 0;

virtual absl::StatusOr<std::any> DecodeStructProperties(
absl::string_view yaml, const YAML::Node& node) const;

virtual absl::StatusOr<std::any> DecodeFieldProperties(
absl::string_view yaml, const YAML::Node& node) const;

virtual absl::StatusOr<std::any> DecodeEnumProperties(
absl::string_view yaml, const YAML::Node& node) const;

virtual absl::StatusOr<std::any> DecodeEnumConstantProperties(
absl::string_view yaml, const YAML::Node& node) const;

virtual absl::Status EncodeStructProperties(const std::any& properties,
YAML::Emitter& out) const;

virtual absl::Status EncodeFieldProperties(const std::any& properties,
YAML::Emitter& out) const;

virtual absl::Status EncodeEnumProperties(const std::any& properties,
YAML::Emitter& out) const;

virtual absl::Status EncodeEnumConstantProperties(const std::any& properties,
YAML::Emitter& out) const;
};

// Read-only registry of SchemaYaml codecs keyed by schema name.
class SchemaYamlRegistry {
public:
SchemaYamlRegistry() = default;

explicit SchemaYamlRegistry(
std::vector<std::unique_ptr<const SchemaYaml>> schemas);

template <
typename... Schemas,
typename = std::enable_if_t<
(sizeof...(Schemas) > 0) &&
(std::is_convertible_v<Schemas, std::unique_ptr<const SchemaYaml>> &&
...)>>
explicit SchemaYamlRegistry(Schemas&&... schemas) {
schemas_.reserve(sizeof...(Schemas));
(Add(std::forward<Schemas>(schemas)), ...);
}

// Finds a registered SchemaYaml by name, or returns nullptr if not
// found.
const SchemaYaml* absl_nullable Find(absl::string_view schema_name) const;

private:
void Add(std::unique_ptr<const SchemaYaml> schema) {
if (schema != nullptr) {
absl::string_view name = schema->name();
schemas_[name] = std::move(schema);
}
}

absl::flat_hash_map<std::string, std::unique_ptr<const SchemaYaml>> schemas_;
};

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_SCHEMA_YAML_H_
Loading
Loading