Skip to content

Commit e9abf6a

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
[cel-types] Add Protobuf definitions and codecs for CEL type definitions and aspects.
Define `CelTypeDefSet`, `CelTypeDef`, `CelTypeDefObject`, and `CelTypeDefEnum` messages with extension ranges for aspects, along with proto extension aspects for Protocol Buffers (`proto2` and `proto3`). Provide `AspectProto`, `AspectProtoRegistry`, 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 d10bc43 commit e9abf6a

18 files changed

Lines changed: 3055 additions & 0 deletions

‎common/typedef/BUILD‎

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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 = "aspect_proto",
71+
srcs = ["aspect_proto.cc"],
72+
hdrs = ["aspect_proto.h"],
73+
deps = [
74+
":typedef_cc_proto",
75+
"@com_google_absl//absl/base:core_headers",
76+
"@com_google_absl//absl/base:nullability",
77+
"@com_google_absl//absl/container:flat_hash_map",
78+
"@com_google_absl//absl/status",
79+
"@com_google_absl//absl/status:statusor",
80+
"@com_google_absl//absl/strings",
81+
"@com_google_absl//absl/strings:string_view",
82+
],
83+
)
84+
85+
cc_test(
86+
name = "aspect_proto_test",
87+
srcs = ["aspect_proto_test.cc"],
88+
deps = [
89+
":aspect_proto",
90+
":typedef_cc_proto",
91+
"//internal:testing",
92+
"@com_google_absl//absl/status",
93+
"@com_google_absl//absl/status:status_matchers",
94+
"@com_google_absl//absl/strings:string_view",
95+
],
96+
)
97+
98+
cc_library(
99+
name = "typedef_proto",
100+
srcs = ["typedef_proto.cc"],
101+
hdrs = ["typedef_proto.h"],
102+
deps = [
103+
":aspect_proto",
104+
":typedef",
105+
":typedef_cc_proto",
106+
"//common:ast",
107+
"//common:constant",
108+
"//common/ast:constant_proto",
109+
"//internal:status_macros",
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/types:span",
115+
"@com_google_absl//absl/types:variant",
116+
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
117+
"@com_google_protobuf//:struct_cc_proto",
118+
],
119+
)
120+
121+
cc_library(
122+
name = "proto_schema",
123+
hdrs = ["proto_schema.h"],
124+
deps = [
125+
"@com_google_absl//absl/base:core_headers",
126+
"@com_google_absl//absl/strings:string_view",
127+
],
128+
)
129+
130+
proto_library(
131+
name = "proto_schema_protos",
132+
srcs = ["proto_schema.proto"],
133+
deps = [":typedef_protos"],
134+
)
135+
136+
cc_proto_library(
137+
name = "proto_schema_cc_proto",
138+
deps = [":proto_schema_protos"],
139+
)
140+
141+
cc_library(
142+
name = "proto_schema_proto",
143+
srcs = ["proto_schema_proto.cc"],
144+
hdrs = ["proto_schema_proto.h"],
145+
deps = [
146+
":aspect_proto",
147+
":proto_schema",
148+
":proto_schema_cc_proto",
149+
":typedef_cc_proto",
150+
"@com_google_absl//absl/base:nullability",
151+
"@com_google_absl//absl/status",
152+
"@com_google_absl//absl/status:statusor",
153+
"@com_google_absl//absl/strings",
154+
"@com_google_absl//absl/strings:string_view",
155+
],
156+
)
157+
158+
cc_test(
159+
name = "proto_schema_proto_test",
160+
srcs = ["proto_schema_proto_test.cc"],
161+
deps = [
162+
":aspect_proto",
163+
":proto_schema",
164+
":proto_schema_cc_proto",
165+
":proto_schema_proto",
166+
":typedef_cc_proto",
167+
"//internal:testing",
168+
"@com_google_absl//absl/status",
169+
"@com_google_absl//absl/status:status_matchers",
170+
"@com_google_protobuf//:protobuf",
171+
],
172+
)
173+
174+
cc_test(
175+
name = "typedef_proto_test",
176+
srcs = ["typedef_proto_test.cc"],
177+
data = [
178+
"//common/typedef/testdata:typedef_testdata",
179+
],
180+
deps = [
181+
":aspect_proto",
182+
":proto_schema_proto",
183+
":typedef",
184+
":typedef_cc_proto",
185+
":typedef_proto",
186+
"//internal:proto_matchers",
187+
"//internal:runfiles",
188+
"//internal:testing",
189+
"@com_google_absl//absl/status",
190+
"@com_google_absl//absl/status:status_matchers",
191+
"@com_google_absl//absl/status:statusor",
192+
"@com_google_absl//absl/strings",
193+
"@com_google_absl//absl/strings:string_view",
194+
"@com_google_protobuf//:protobuf",
195+
],
196+
)

‎common/typedef/aspect_proto.cc‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
#include "common/typedef/aspect_proto.h"
16+
17+
#include <any>
18+
#include <memory>
19+
#include <utility>
20+
#include <vector>
21+
22+
#include "absl/base/nullability.h"
23+
#include "absl/status/status.h"
24+
#include "absl/status/statusor.h"
25+
#include "absl/strings/str_cat.h"
26+
#include "absl/strings/string_view.h"
27+
#include "common/typedef/typedef.pb.h"
28+
29+
namespace cel {
30+
31+
absl::StatusOr<std::any> AspectProto::DecodeStructProperties(
32+
const types::Struct& proto) const {
33+
return std::any();
34+
}
35+
36+
absl::StatusOr<std::any> AspectProto::DecodeFieldProperties(
37+
const types::Struct::Field& proto) const {
38+
return std::any();
39+
}
40+
41+
absl::StatusOr<std::any> AspectProto::DecodeEnumProperties(
42+
const types::Enum& proto) const {
43+
return std::any();
44+
}
45+
46+
absl::StatusOr<std::any> AspectProto::DecodeEnumConstantProperties(
47+
const types::Enum::EnumConstant& proto) const {
48+
return std::any();
49+
}
50+
51+
absl::Status AspectProto::EncodeStructProperties(
52+
const std::any& properties, types::Struct* absl_nonnull proto) const {
53+
return absl::UnimplementedError(absl::StrCat(
54+
"Custom struct properties are not supported for aspect '", name(), "'"));
55+
}
56+
57+
absl::Status AspectProto::EncodeFieldProperties(
58+
const std::any& properties,
59+
types::Struct::Field* absl_nonnull proto) const {
60+
return absl::UnimplementedError(absl::StrCat(
61+
"Custom field properties are not supported for aspect '", name(), "'"));
62+
}
63+
64+
absl::Status AspectProto::EncodeEnumProperties(
65+
const std::any& properties, types::Enum* absl_nonnull proto) const {
66+
return absl::UnimplementedError(absl::StrCat(
67+
"Custom enum properties are not supported for aspect '", name(), "'"));
68+
}
69+
70+
absl::Status AspectProto::EncodeEnumConstantProperties(
71+
const std::any& properties,
72+
types::Enum::EnumConstant* absl_nonnull proto) const {
73+
return absl::UnimplementedError(absl::StrCat(
74+
"Custom enum constant properties are not supported for aspect '", name(),
75+
"'"));
76+
}
77+
78+
AspectProtoRegistry::AspectProtoRegistry(
79+
std::vector<std::unique_ptr<const AspectProto>> aspects) {
80+
aspects_.reserve(aspects.size());
81+
for (std::unique_ptr<const AspectProto>& aspect : aspects) {
82+
Add(std::move(aspect));
83+
}
84+
}
85+
86+
const AspectProto* absl_nullable AspectProtoRegistry::Find(
87+
absl::string_view aspect_name) const {
88+
auto it = aspects_.find(aspect_name);
89+
if (it == aspects_.end()) {
90+
return nullptr;
91+
}
92+
return it->second.get();
93+
}
94+
95+
} // namespace cel

0 commit comments

Comments
 (0)