Skip to content

Commit 0306fc7

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
[cel-types] Add Protobuf definitions and codecs for CEL type definitions and schemas.
Define `CelTypeDefSet`, `CelTypeDef`, `CelTypeDefObject`, and `CelTypeDefEnum` messages with extension ranges for schema-specific properties, along with proto extension schemas for Protocol Buffers (`proto2` and `proto3`). Provide `SchemaProto`, `SchemaProtoRegistry`, and C++ encoder/decoder APIs (`TypeDefsToProto`, `TypeDefsFromProto`, and element-level overloads) for converting between `cel::TypeDef` types and their Protobuf representations. PiperOrigin-RevId: 995451531
1 parent 4dc0a65 commit 0306fc7

18 files changed

Lines changed: 3071 additions & 0 deletions

‎common/typedef/BUILD‎

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
16+
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
17+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
18+
load("@rules_cc//cc:cc_test.bzl", "cc_test")
19+
20+
package(default_visibility = ["//visibility:public"])
21+
22+
licenses(["notice"])
23+
24+
cc_library(
25+
name = "typedef",
26+
srcs = ["typedef.cc"],
27+
hdrs = ["typedef.h"],
28+
deps = [
29+
"//common:ast",
30+
"//common:constant",
31+
"@com_google_absl//absl/base:core_headers",
32+
"@com_google_absl//absl/base:nullability",
33+
"@com_google_absl//absl/container:flat_hash_map",
34+
"@com_google_absl//absl/functional:overload",
35+
"@com_google_absl//absl/log:absl_check",
36+
"@com_google_absl//absl/status",
37+
"@com_google_absl//absl/strings",
38+
"@com_google_absl//absl/strings:string_view",
39+
],
40+
)
41+
42+
cc_test(
43+
name = "typedef_test",
44+
srcs = ["typedef_test.cc"],
45+
deps = [
46+
":typedef",
47+
"//common:ast",
48+
"//common:constant",
49+
"//internal:testing",
50+
"@com_google_absl//absl/status",
51+
"@com_google_absl//absl/status:status_matchers",
52+
],
53+
)
54+
55+
proto_library(
56+
name = "typedef_protos",
57+
srcs = ["typedef.proto"],
58+
deps = [
59+
"@com_google_cel_spec//proto/cel/expr:checked_proto",
60+
"@com_google_cel_spec//proto/cel/expr:syntax_proto",
61+
],
62+
)
63+
64+
cc_proto_library(
65+
name = "typedef_cc_proto",
66+
deps = [":typedef_protos"],
67+
)
68+
69+
cc_library(
70+
name = "schema_proto",
71+
srcs = ["schema_proto.cc"],
72+
hdrs = ["schema_proto.h"],
73+
deps = [
74+
":typedef_cc_proto",
75+
"@com_google_absl//absl/base:nullability",
76+
"@com_google_absl//absl/container:flat_hash_map",
77+
"@com_google_absl//absl/status",
78+
"@com_google_absl//absl/status:statusor",
79+
"@com_google_absl//absl/strings",
80+
"@com_google_absl//absl/strings:string_view",
81+
],
82+
)
83+
84+
cc_test(
85+
name = "schema_proto_test",
86+
srcs = ["schema_proto_test.cc"],
87+
deps = [
88+
":schema_proto",
89+
":typedef_cc_proto",
90+
"//internal:testing",
91+
"@com_google_absl//absl/status",
92+
"@com_google_absl//absl/status:status_matchers",
93+
"@com_google_absl//absl/strings:string_view",
94+
],
95+
)
96+
97+
cc_library(
98+
name = "typedef_proto",
99+
srcs = ["typedef_proto.cc"],
100+
hdrs = ["typedef_proto.h"],
101+
deps = [
102+
":schema_proto",
103+
":typedef",
104+
":typedef_cc_proto",
105+
"//common:ast",
106+
"//common:constant",
107+
"//common/ast:constant_proto",
108+
"//internal:status_macros",
109+
"@com_google_absl//absl/algorithm:container",
110+
"@com_google_absl//absl/base:nullability",
111+
"@com_google_absl//absl/container:flat_hash_map",
112+
"@com_google_absl//absl/status",
113+
"@com_google_absl//absl/status:statusor",
114+
"@com_google_absl//absl/strings",
115+
"@com_google_absl//absl/types:span",
116+
"@com_google_absl//absl/types:variant",
117+
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
118+
"@com_google_protobuf//:protobuf",
119+
"@com_google_protobuf//:struct_cc_proto",
120+
],
121+
)
122+
123+
cc_library(
124+
name = "proto_schema",
125+
hdrs = ["proto_schema.h"],
126+
deps = [
127+
"@com_google_absl//absl/base:core_headers",
128+
"@com_google_absl//absl/strings:string_view",
129+
],
130+
)
131+
132+
proto_library(
133+
name = "proto_schema_protos",
134+
srcs = ["proto_schema.proto"],
135+
deps = [":typedef_protos"],
136+
)
137+
138+
cc_proto_library(
139+
name = "proto_schema_cc_proto",
140+
deps = [":proto_schema_protos"],
141+
)
142+
143+
cc_library(
144+
name = "proto_schema_proto",
145+
srcs = ["proto_schema_proto.cc"],
146+
hdrs = ["proto_schema_proto.h"],
147+
deps = [
148+
":proto_schema",
149+
":proto_schema_cc_proto",
150+
":schema_proto",
151+
":typedef_cc_proto",
152+
"@com_google_absl//absl/base:nullability",
153+
"@com_google_absl//absl/status",
154+
"@com_google_absl//absl/status:statusor",
155+
"@com_google_absl//absl/strings",
156+
"@com_google_absl//absl/strings:string_view",
157+
],
158+
)
159+
160+
cc_test(
161+
name = "proto_schema_proto_test",
162+
srcs = ["proto_schema_proto_test.cc"],
163+
deps = [
164+
":proto_schema",
165+
":proto_schema_cc_proto",
166+
":proto_schema_proto",
167+
":schema_proto",
168+
":typedef_cc_proto",
169+
"//internal:testing",
170+
"@com_google_absl//absl/status",
171+
"@com_google_absl//absl/status:status_matchers",
172+
"@com_google_protobuf//:protobuf",
173+
],
174+
)
175+
176+
cc_test(
177+
name = "typedef_proto_test",
178+
srcs = ["typedef_proto_test.cc"],
179+
data = [
180+
"//common/typedef/testdata:typedef_testdata",
181+
],
182+
deps = [
183+
":proto_schema",
184+
":proto_schema_cc_proto",
185+
":proto_schema_proto",
186+
":schema_proto",
187+
":typedef",
188+
":typedef_cc_proto",
189+
":typedef_proto",
190+
"//common:ast",
191+
"//internal:runfiles",
192+
"//internal:testing",
193+
"@com_google_absl//absl/container:flat_hash_map",
194+
"@com_google_absl//absl/log:absl_log",
195+
"@com_google_absl//absl/status",
196+
"@com_google_absl//absl/status:status_matchers",
197+
"@com_google_absl//absl/strings",
198+
"@com_google_absl//absl/strings:string_view",
199+
"@com_google_protobuf//:protobuf",
200+
],
201+
)

‎common/typedef/proto_schema.h‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_PROTO_SCHEMA_H_
16+
#define THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_PROTO_SCHEMA_H_
17+
18+
#include <cstdint>
19+
#include <optional>
20+
#include <string>
21+
#include <utility>
22+
23+
#include "absl/base/attributes.h"
24+
#include "absl/strings/string_view.h"
25+
26+
namespace cel {
27+
28+
inline constexpr absl::string_view kProto2SchemaName = "proto2";
29+
inline constexpr absl::string_view kProto3SchemaName = "proto3";
30+
31+
// Protocol Buffers schema properties attached to a StructTypeDef.
32+
class ProtoStructProperties final {
33+
public:
34+
ProtoStructProperties() = default;
35+
explicit ProtoStructProperties(std::optional<std::string> name)
36+
: name_(std::move(name)) {}
37+
38+
// Overrides the canonical CEL struct name with a target-specific protobuf
39+
// message name when they differ (e.g., mapping a canonical CEL type
40+
// "com.example.Order" to "com.example.proto2.Order" in proto2 and
41+
// "com.example.proto3.Order" in proto3).
42+
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
43+
return name_;
44+
}
45+
46+
private:
47+
std::optional<std::string> name_;
48+
};
49+
50+
// Protocol Buffers schema properties attached to a StructTypeDef::Field.
51+
class ProtoFieldProperties final {
52+
public:
53+
ProtoFieldProperties() = default;
54+
explicit ProtoFieldProperties(
55+
std::optional<std::string> name, std::optional<int32_t> id = std::nullopt,
56+
std::optional<std::string> json_name = std::nullopt)
57+
: name_(std::move(name)), id_(id), json_name_(std::move(json_name)) {}
58+
59+
// Overrides the canonical CEL field name with the protobuf field name when
60+
// they differ (e.g., mapping a CEL field "order_id" to a legacy proto2 field
61+
// "order_id_v2", or mapping a camelCase CEL field "userId" to "user_id").
62+
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
63+
return name_;
64+
}
65+
66+
// The protobuf field number (tag) in the message definition (e.g., `1` in
67+
// `string order_id = 1;`), required for wire serialization and descriptor
68+
// generation.
69+
std::optional<int32_t> id() const { return id_; }
70+
71+
// Custom JSON field name specified via the `[json_name = "..."]` field
72+
// option when it differs from the default lowerCamelCase of the proto field
73+
// name (e.g., `[json_name = "order_id_legacy"]` on field `order_id`).
74+
const std::optional<std::string>& json_name() const
75+
ABSL_ATTRIBUTE_LIFETIME_BOUND {
76+
return json_name_;
77+
}
78+
79+
private:
80+
std::optional<std::string> name_;
81+
std::optional<int32_t> id_;
82+
std::optional<std::string> json_name_;
83+
};
84+
85+
// Protocol Buffers schema properties attached to an EnumTypeDef.
86+
class ProtoEnumProperties final {
87+
public:
88+
ProtoEnumProperties() = default;
89+
explicit ProtoEnumProperties(std::optional<std::string> name,
90+
std::optional<bool> allow_alias = std::nullopt)
91+
: name_(std::move(name)), allow_alias_(allow_alias) {}
92+
93+
// Overrides the canonical CEL enum name with a target-specific protobuf enum
94+
// name when they differ (e.g., mapping "com.example.OrderStatus" to
95+
// "com.example.proto2.OrderStatus").
96+
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
97+
return name_;
98+
}
99+
100+
// Whether the protobuf enum sets `option allow_alias = true;`, permitting
101+
// multiple enum constants to share the same numeric value (e.g.,
102+
// `STARTED = 1;` and `RUNNING = 1;`).
103+
std::optional<bool> allow_alias() const { return allow_alias_; }
104+
105+
private:
106+
std::optional<std::string> name_;
107+
std::optional<bool> allow_alias_;
108+
};
109+
110+
// Protocol Buffers schema properties attached to an EnumTypeDef::EnumConstant.
111+
class ProtoEnumConstantProperties final {
112+
public:
113+
ProtoEnumConstantProperties() = default;
114+
explicit ProtoEnumConstantProperties(std::optional<std::string> name)
115+
: name_(std::move(name)) {}
116+
117+
// Overrides the canonical CEL enum constant name with the protobuf enum value
118+
// name when they differ (e.g., mapping a CEL constant "ORDER_STATUS_ACTIVE"
119+
// to "STATUS_ACTIVE_V2" in a legacy proto2 enum).
120+
const std::optional<std::string>& name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
121+
return name_;
122+
}
123+
124+
private:
125+
std::optional<std::string> name_;
126+
};
127+
128+
} // namespace cel
129+
130+
#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPEDEF_PROTO_SCHEMA_H_

‎common/typedef/proto_schema.proto‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
edition = "2023";
16+
17+
package cel.types;
18+
19+
import "common/typedef/typedef.proto";
20+
21+
option cc_enable_arenas = true;
22+
23+
// Protocol Buffers schema properties attached to a Struct.
24+
message ProtoStructProperties {
25+
string name = 1;
26+
}
27+
28+
// Protocol Buffers schema properties attached to a Struct.Field.
29+
message ProtoFieldProperties {
30+
string name = 1;
31+
int32 id = 2;
32+
string json_name = 3;
33+
}
34+
35+
// Protocol Buffers schema properties attached to an Enum.
36+
message ProtoEnumProperties {
37+
string name = 1;
38+
bool allow_alias = 2;
39+
}
40+
41+
// Protocol Buffers schema properties attached to an Enum.EnumConstant.
42+
message ProtoEnumConstantProperties {
43+
string name = 1;
44+
}
45+
46+
extend Struct {
47+
ProtoStructProperties proto2_struct_schema = 1001;
48+
ProtoStructProperties proto3_struct_schema = 1002;
49+
}
50+
51+
extend Struct.Field {
52+
ProtoFieldProperties proto2_field_schema = 1001;
53+
ProtoFieldProperties proto3_field_schema = 1002;
54+
}
55+
56+
extend Enum {
57+
ProtoEnumProperties proto2_enum_schema = 1001;
58+
ProtoEnumProperties proto3_enum_schema = 1002;
59+
}
60+
61+
extend Enum.EnumConstant {
62+
ProtoEnumConstantProperties proto2_enum_constant_schema = 1001;
63+
ProtoEnumConstantProperties proto3_enum_constant_schema = 1002;
64+
}

0 commit comments

Comments
 (0)