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
196 changes: 196 additions & 0 deletions common/typedef/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# 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("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
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",
],
)

proto_library(
name = "typedef_protos",
srcs = ["typedef.proto"],
deps = [
"@com_google_cel_spec//proto/cel/expr:checked_proto",
"@com_google_cel_spec//proto/cel/expr:syntax_proto",
],
)

cc_proto_library(
name = "typedef_cc_proto",
deps = [":typedef_protos"],
)

cc_library(
name = "schema_proto",
srcs = ["schema_proto.cc"],
hdrs = ["schema_proto.h"],
deps = [
":typedef_cc_proto",
"@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/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
],
)

cc_test(
name = "schema_proto_test",
srcs = ["schema_proto_test.cc"],
deps = [
":schema_proto",
":typedef_cc_proto",
"//internal:testing",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/strings:string_view",
],
)

cc_library(
name = "typedef_proto",
srcs = ["typedef_proto.cc"],
hdrs = ["typedef_proto.h"],
deps = [
":schema_proto",
":typedef",
":typedef_cc_proto",
"//common:ast",
"//common:constant",
"//common/ast:constant_proto",
"//internal:status_macros",
"@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/types:span",
"@com_google_absl//absl/types:variant",
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
"@com_google_protobuf//:struct_cc_proto",
],
)

cc_library(
name = "proto_schema",
hdrs = ["proto_schema.h"],
deps = [
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings:string_view",
],
)

proto_library(
name = "proto_schema_protos",
srcs = ["proto_schema.proto"],
deps = [":typedef_protos"],
)

cc_proto_library(
name = "proto_schema_cc_proto",
deps = [":proto_schema_protos"],
)

cc_library(
name = "proto_schema_proto",
srcs = ["proto_schema_proto.cc"],
hdrs = ["proto_schema_proto.h"],
deps = [
":proto_schema",
":proto_schema_cc_proto",
":schema_proto",
":typedef_cc_proto",
"@com_google_absl//absl/base:nullability",
"@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 = "proto_schema_proto_test",
srcs = ["proto_schema_proto_test.cc"],
deps = [
":proto_schema",
":proto_schema_cc_proto",
":proto_schema_proto",
":schema_proto",
":typedef_cc_proto",
"//internal:testing",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "typedef_proto_test",
srcs = ["typedef_proto_test.cc"],
data = [
"//common/typedef/testdata:typedef_testdata",
],
deps = [
":proto_schema_proto",
":schema_proto",
":typedef",
":typedef_cc_proto",
":typedef_proto",
"//internal:proto_matchers",
"//internal:runfiles",
"//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_protobuf//:protobuf",
],
)
130 changes: 130 additions & 0 deletions common/typedef/proto_schema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// 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_PROTO_SCHEMA_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_PROTO_SCHEMA_H_

#include <cstdint>
#include <optional>
#include <string>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"

namespace cel {

inline constexpr absl::string_view kProto2SchemaName = "proto2";
inline constexpr absl::string_view kProto3SchemaName = "proto3";

// Protocol Buffers schema properties attached to a StructTypeDef.
class ProtoStructProperties final {
public:
ProtoStructProperties() = default;
explicit ProtoStructProperties(std::optional<std::string> name)
: name_(std::move(name)) {}

// Overrides the canonical CEL struct name with a target-specific protobuf
// message name when they differ (e.g., mapping a canonical CEL type
// "com.example.Order" to "com.example.proto2.Order" in proto2 and
// "com.example.proto3.Order" in proto3).
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return name_;
}

private:
std::optional<std::string> name_;
};

// Protocol Buffers schema properties attached to a StructTypeDef::Field.
class ProtoFieldProperties final {
public:
ProtoFieldProperties() = default;
explicit ProtoFieldProperties(
std::optional<std::string> name, std::optional<int32_t> id = std::nullopt,
std::optional<std::string> json_name = std::nullopt)
: name_(std::move(name)), id_(id), json_name_(std::move(json_name)) {}

// Overrides the canonical CEL field name with the protobuf field name when
// they differ (e.g., mapping a CEL field "order_id" to a legacy proto2 field
// "order_id_v2", or mapping a camelCase CEL field "userId" to "user_id").
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return name_;
}

// The protobuf field number (tag) in the message definition (e.g., `1` in
// `string order_id = 1;`), required for wire serialization and descriptor
// generation.
std::optional<int32_t> id() const { return id_; }

// Custom JSON field name specified via the `[json_name = "..."]` field
// option when it differs from the default lowerCamelCase of the proto field
// name (e.g., `[json_name = "order_id_legacy"]` on field `order_id`).
const std::optional<std::string>& json_name() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return json_name_;
}

private:
std::optional<std::string> name_;
std::optional<int32_t> id_;
std::optional<std::string> json_name_;
};

// Protocol Buffers schema properties attached to an EnumTypeDef.
class ProtoEnumProperties final {
public:
ProtoEnumProperties() = default;
explicit ProtoEnumProperties(std::optional<std::string> name,
std::optional<bool> allow_alias = std::nullopt)
: name_(std::move(name)), allow_alias_(allow_alias) {}

// Overrides the canonical CEL enum name with a target-specific protobuf enum
// name when they differ (e.g., mapping "com.example.OrderStatus" to
// "com.example.proto2.OrderStatus").
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return name_;
}

// Whether the protobuf enum sets `option allow_alias = true;`, permitting
// multiple enum constants to share the same numeric value (e.g.,
// `STARTED = 1;` and `RUNNING = 1;`).
std::optional<bool> allow_alias() const { return allow_alias_; }

private:
std::optional<std::string> name_;
std::optional<bool> allow_alias_;
};

// Protocol Buffers schema properties attached to an EnumTypeDef::EnumConstant.
class ProtoEnumConstantProperties final {
public:
ProtoEnumConstantProperties() = default;
explicit ProtoEnumConstantProperties(std::optional<std::string> name)
: name_(std::move(name)) {}

// Overrides the canonical CEL enum constant name with the protobuf enum value
// name when they differ (e.g., mapping a CEL constant "ORDER_STATUS_ACTIVE"
// to "STATUS_ACTIVE_V2" in a legacy proto2 enum).
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return name_;
}

private:
std::optional<std::string> name_;
};

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_PROTO_SCHEMA_H_
64 changes: 64 additions & 0 deletions common/typedef/proto_schema.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.

edition = "2023";

package cel.types;

import "common/typedef/typedef.proto";

option cc_enable_arenas = true;

// Protocol Buffers schema properties attached to a Struct.
message ProtoStructProperties {
string name = 1;
}

// Protocol Buffers schema properties attached to a Struct.Field.
message ProtoFieldProperties {
string name = 1;
int32 id = 2;
string json_name = 3;
}

// Protocol Buffers schema properties attached to an Enum.
message ProtoEnumProperties {
string name = 1;
bool allow_alias = 2;
}

// Protocol Buffers schema properties attached to an Enum.EnumConstant.
message ProtoEnumConstantProperties {
string name = 1;
}

extend Struct {
ProtoStructProperties proto2_struct_schema = 1001;
ProtoStructProperties proto3_struct_schema = 1002;
}

extend Struct.Field {
ProtoFieldProperties proto2_field_schema = 1001;
ProtoFieldProperties proto3_field_schema = 1002;
}

extend Enum {
ProtoEnumProperties proto2_enum_schema = 1001;
ProtoEnumProperties proto3_enum_schema = 1002;
}

extend Enum.EnumConstant {
ProtoEnumConstantProperties proto2_enum_constant_schema = 1001;
ProtoEnumConstantProperties proto3_enum_constant_schema = 1002;
}
Loading
Loading