diff --git a/common/internal/BUILD b/common/internal/BUILD index 94fbbe3d5..e00a8ea7a 100644 --- a/common/internal/BUILD +++ b/common/internal/BUILD @@ -102,3 +102,35 @@ cc_test( "@com_google_protobuf//:protobuf", ], ) + +cc_library( + name = "value_conversion", + srcs = ["value_conversion.cc"], + hdrs = ["value_conversion.h"], + deps = [ + "//common:any", + "//common:value", + "//common:value_kind", + "//extensions/protobuf:value", + "//internal:proto_time_encoding", + "//internal:status_macros", + "//internal:time", + "@com_google_absl//absl/base:core_headers", + "@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:cord", + "@com_google_absl//absl/time", + "@com_google_cel_spec//proto/cel/expr:checked_cc_proto", + "@com_google_cel_spec//proto/cel/expr:syntax_cc_proto", + "@com_google_cel_spec//proto/cel/expr:value_cc_proto", + "@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto", + "@com_google_googleapis//google/api/expr/v1alpha1:value_cc_proto", + "@com_google_protobuf//:any_cc_proto", + "@com_google_protobuf//:protobuf", + "@com_google_protobuf//:struct_cc_proto", + "@com_google_protobuf//:timestamp_cc_proto", + "@com_google_protobuf//src/google/protobuf/io", + ], +) diff --git a/conformance/value_conversion.cc b/common/internal/value_conversion.cc similarity index 78% rename from conformance/value_conversion.cc rename to common/internal/value_conversion.cc index ef567de84..66b8fb2b5 100644 --- a/conformance/value_conversion.cc +++ b/common/internal/value_conversion.cc @@ -11,7 +11,7 @@ // 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 "conformance/value_conversion.h" +#include "common/internal/value_conversion.h" #include #include @@ -38,38 +38,38 @@ #include "google/protobuf/io/zero_copy_stream_impl_lite.h" #include "google/protobuf/message.h" -namespace cel::conformance_internal { +namespace cel::common_internal { namespace { -using ConformanceKind = cel::expr::Value::KindCase; -using ConformanceMapValue = cel::expr::MapValue; -using ConformanceListValue = cel::expr::ListValue; +using ExprValueKind = cel::expr::Value::KindCase; +using ExprMapValue = cel::expr::MapValue; +using ExprListValue = cel::expr::ListValue; -std::string ToString(ConformanceKind kind_case) { +std::string ToString(ExprValueKind kind_case) { switch (kind_case) { - case ConformanceKind::kBoolValue: + case ExprValueKind::kBoolValue: return "bool_value"; - case ConformanceKind::kInt64Value: + case ExprValueKind::kInt64Value: return "int64_value"; - case ConformanceKind::kUint64Value: + case ExprValueKind::kUint64Value: return "uint64_value"; - case ConformanceKind::kDoubleValue: + case ExprValueKind::kDoubleValue: return "double_value"; - case ConformanceKind::kStringValue: + case ExprValueKind::kStringValue: return "string_value"; - case ConformanceKind::kBytesValue: + case ExprValueKind::kBytesValue: return "bytes_value"; - case ConformanceKind::kTypeValue: + case ExprValueKind::kTypeValue: return "type_value"; - case ConformanceKind::kEnumValue: + case ExprValueKind::kEnumValue: return "enum_value"; - case ConformanceKind::kMapValue: + case ExprValueKind::kMapValue: return "map_value"; - case ConformanceKind::kListValue: + case ExprValueKind::kListValue: return "list_value"; - case ConformanceKind::kNullValue: + case ExprValueKind::kNullValue: return "null_value"; - case ConformanceKind::kObjectValue: + case ExprValueKind::kObjectValue: return "object_value"; default: return "unknown kind case"; @@ -104,18 +104,18 @@ absl::StatusOr FromObject( arena); } -absl::StatusOr MapValueFromConformance( - const ConformanceMapValue& map_value, +absl::StatusOr MapValueFromExpr( + const ExprMapValue& map_value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, google::protobuf::Arena* ABSL_NONNULL arena) { auto builder = cel::NewMapValueBuilder(arena); for (const auto& entry : map_value.entries()) { CEL_ASSIGN_OR_RETURN(auto key, - FromConformanceValue(entry.key(), descriptor_pool, + FromExprValue(entry.key(), descriptor_pool, message_factory, arena)); CEL_ASSIGN_OR_RETURN(auto value, - FromConformanceValue(entry.value(), descriptor_pool, + FromExprValue(entry.value(), descriptor_pool, message_factory, arena)); CEL_RETURN_IF_ERROR(builder->Put(std::move(key), std::move(value))); } @@ -123,8 +123,8 @@ absl::StatusOr MapValueFromConformance( return std::move(*builder).Build(); } -absl::StatusOr ListValueFromConformance( - const ConformanceListValue& list_value, +absl::StatusOr ListValueFromExpr( + const ExprListValue& list_value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, google::protobuf::Arena* ABSL_NONNULL arena) { @@ -132,19 +132,19 @@ absl::StatusOr ListValueFromConformance( for (const auto& elem : list_value.values()) { CEL_ASSIGN_OR_RETURN( auto value, - FromConformanceValue(elem, descriptor_pool, message_factory, arena)); + FromExprValue(elem, descriptor_pool, message_factory, arena)); CEL_RETURN_IF_ERROR(builder->Add(std::move(value))); } return std::move(*builder).Build(); } -absl::StatusOr MapValueToConformance( +absl::StatusOr MapValueToExpr( const MapValue& map_value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, google::protobuf::Arena* ABSL_NONNULL arena) { - ConformanceMapValue result; + ExprMapValue result; CEL_ASSIGN_OR_RETURN(auto iter, map_value.NewIterator()); @@ -157,9 +157,9 @@ absl::StatusOr MapValueToConformance( CEL_ASSIGN_OR_RETURN( auto key, - ToConformanceValue(key_value, descriptor_pool, message_factory, arena)); + ToExprValue(key_value, descriptor_pool, message_factory, arena)); CEL_ASSIGN_OR_RETURN(auto value, - ToConformanceValue(value_value, descriptor_pool, + ToExprValue(value_value, descriptor_pool, message_factory, arena)); auto* entry = result.add_entries(); @@ -171,12 +171,12 @@ absl::StatusOr MapValueToConformance( return result; } -absl::StatusOr ListValueToConformance( +absl::StatusOr ListValueToExpr( const ListValue& list_value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, google::protobuf::Arena* ABSL_NONNULL arena) { - ConformanceListValue result; + ExprListValue result; CEL_ASSIGN_OR_RETURN(auto iter, list_value.NewIterator()); @@ -185,7 +185,7 @@ absl::StatusOr ListValueToConformance( iter->Next(descriptor_pool, message_factory, arena)); CEL_ASSIGN_OR_RETURN( *result.add_values(), - ToConformanceValue(elem, descriptor_pool, message_factory, arena)); + ToExprValue(elem, descriptor_pool, message_factory, arena)); } return result; @@ -208,44 +208,44 @@ absl::StatusOr ToProtobufAny( } // namespace -absl::StatusOr FromConformanceValue( +absl::StatusOr FromExprValue( const cel::expr::Value& value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, google::protobuf::Arena* ABSL_NONNULL arena) { google::protobuf::LinkMessageReflection(); switch (value.kind_case()) { - case ConformanceKind::kBoolValue: + case ExprValueKind::kBoolValue: return cel::BoolValue(value.bool_value()); - case ConformanceKind::kInt64Value: + case ExprValueKind::kInt64Value: return cel::IntValue(value.int64_value()); - case ConformanceKind::kUint64Value: + case ExprValueKind::kUint64Value: return cel::UintValue(value.uint64_value()); - case ConformanceKind::kDoubleValue: + case ExprValueKind::kDoubleValue: return cel::DoubleValue(value.double_value()); - case ConformanceKind::kStringValue: + case ExprValueKind::kStringValue: return cel::StringValue(value.string_value()); - case ConformanceKind::kBytesValue: + case ExprValueKind::kBytesValue: return cel::BytesValue(value.bytes_value()); - case ConformanceKind::kNullValue: + case ExprValueKind::kNullValue: return cel::NullValue(); - case ConformanceKind::kObjectValue: + case ExprValueKind::kObjectValue: return FromObject(value.object_value(), descriptor_pool, message_factory, arena); - case ConformanceKind::kMapValue: - return MapValueFromConformance(value.map_value(), descriptor_pool, + case ExprValueKind::kMapValue: + return MapValueFromExpr(value.map_value(), descriptor_pool, message_factory, arena); - case ConformanceKind::kListValue: - return ListValueFromConformance(value.list_value(), descriptor_pool, + case ExprValueKind::kListValue: + return ListValueFromExpr(value.list_value(), descriptor_pool, message_factory, arena); default: return absl::UnimplementedError(absl::StrCat( - "FromConformanceValue not supported ", ToString(value.kind_case()))); + "FromExprValue not supported ", ToString(value.kind_case()))); } } -absl::StatusOr ToConformanceValue( +absl::StatusOr ToExprValue( const Value& value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, @@ -293,14 +293,14 @@ absl::StatusOr ToConformanceValue( case ValueKind::kMap: { CEL_ASSIGN_OR_RETURN( *result.mutable_map_value(), - MapValueToConformance(value.GetMap(), descriptor_pool, + MapValueToExpr(value.GetMap(), descriptor_pool, message_factory, arena)); break; } case ValueKind::kList: { CEL_ASSIGN_OR_RETURN( *result.mutable_list_value(), - ListValueToConformance(value.GetList(), descriptor_pool, + ListValueToExpr(value.GetList(), descriptor_pool, message_factory, arena)); break; } @@ -312,10 +312,10 @@ absl::StatusOr ToConformanceValue( } default: return absl::UnimplementedError( - absl::StrCat("ToConformanceValue not supported ", + absl::StrCat("ToExprValue not supported ", ValueKindToString(value->kind()))); } return result; } -} // namespace cel::conformance_internal +} // namespace cel::common_internal diff --git a/conformance/value_conversion.h b/common/internal/value_conversion.h similarity index 91% rename from conformance/value_conversion.h rename to common/internal/value_conversion.h index 6f15ad99b..20a853799 100644 --- a/conformance/value_conversion.h +++ b/common/internal/value_conversion.h @@ -13,8 +13,8 @@ // limitations under the License. // // Converters to/from serialized Value to/from runtime values. -#ifndef THIRD_PARTY_CEL_CPP_CONFORMANCE_VALUE_CONVERSION_H_ -#define THIRD_PARTY_CEL_CPP_CONFORMANCE_VALUE_CONVERSION_H_ +#ifndef THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_VALUE_CONVERSION_H_ +#define THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_VALUE_CONVERSION_H_ #include "cel/expr/checked.pb.h" #include "cel/expr/syntax.pb.h" @@ -32,7 +32,7 @@ #include "google/protobuf/message.h" #include "google/protobuf/message_lite.h" -namespace cel::conformance_internal { +namespace cel::common_internal { ABSL_MUST_USE_RESULT inline bool UnsafeConvertWireCompatProto( @@ -97,17 +97,17 @@ inline bool ConvertWireCompatProto( return UnsafeConvertWireCompatProto(src, dest); } -absl::StatusOr FromConformanceValue( +absl::StatusOr FromExprValue( const cel::expr::Value& value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, google::protobuf::Arena* ABSL_NONNULL arena); -absl::StatusOr ToConformanceValue( +absl::StatusOr ToExprValue( const Value& value, const google::protobuf::DescriptorPool* ABSL_NONNULL descriptor_pool, google::protobuf::MessageFactory* ABSL_NONNULL message_factory, google::protobuf::Arena* ABSL_NONNULL arena); -} // namespace cel::conformance_internal -#endif // THIRD_PARTY_CEL_CPP_CONFORMANCE_VALUE_CONVERSION_H_ +} // namespace cel::common_internal +#endif // THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_VALUE_CONVERSION_H_ diff --git a/conformance/BUILD b/conformance/BUILD index 95353e1c2..c9892c5a9 100644 --- a/conformance/BUILD +++ b/conformance/BUILD @@ -18,45 +18,12 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) -cc_library( - name = "value_conversion", - srcs = ["value_conversion.cc"], - hdrs = ["value_conversion.h"], - deps = [ - "//common:any", - "//common:value", - "//common:value_kind", - "//extensions/protobuf:value", - "//internal:proto_time_encoding", - "//internal:status_macros", - "//internal:time", - "@com_google_absl//absl/base:core_headers", - "@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:cord", - "@com_google_absl//absl/time", - "@com_google_cel_spec//proto/cel/expr:checked_cc_proto", - "@com_google_cel_spec//proto/cel/expr:syntax_cc_proto", - "@com_google_cel_spec//proto/cel/expr:value_cc_proto", - "@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto", - "@com_google_googleapis//google/api/expr/v1alpha1:value_cc_proto", - "@com_google_protobuf//:any_cc_proto", - "@com_google_protobuf//:protobuf", - "@com_google_protobuf//:struct_cc_proto", - "@com_google_protobuf//:timestamp_cc_proto", - "@com_google_protobuf//src/google/protobuf/io", - ], -) - cc_library( name = "service", testonly = True, srcs = ["service.cc"], hdrs = ["service.h"], deps = [ - ":value_conversion", "//checker:optional", "//checker:standard_library", "//checker:type_checker_builder", @@ -69,6 +36,7 @@ cc_library( "//common:source", "//common:type", "//common:value", + "//common/internal:value_conversion", "//eval/public:activation", "//eval/public:builtin_func_registrar", "//eval/public:cel_expr_builder_factory", diff --git a/conformance/service.cc b/conformance/service.cc index 2bb2854f2..fd83a65db 100644 --- a/conformance/service.cc +++ b/conformance/service.cc @@ -50,10 +50,10 @@ #include "common/decl.h" #include "common/decl_proto_v1alpha1.h" #include "common/expr.h" +#include "common/internal/value_conversion.h" #include "common/source.h" #include "common/type.h" #include "common/value.h" -#include "conformance/value_conversion.h" #include "eval/public/activation.h" #include "eval/public/builtin_func_registrar.h" #include "eval/public/cel_expr_builder_factory.h" @@ -95,9 +95,9 @@ using ::cel::CreateStandardRuntimeBuilder; using ::cel::Runtime; using ::cel::RuntimeOptions; -using ::cel::conformance_internal::ConvertWireCompatProto; -using ::cel::conformance_internal::FromConformanceValue; -using ::cel::conformance_internal::ToConformanceValue; +using ::cel::common_internal::ConvertWireCompatProto; +using ::cel::common_internal::FromExprValue; +using ::cel::common_internal::ToExprValue; using ::cel::extensions::RegisterProtobufEnum; using ::google::protobuf::Arena; @@ -561,8 +561,8 @@ class ModernConformanceServiceImpl : public ConformanceServiceInterface { ABSL_CHECK(ConvertWireCompatProto(pair.second.value(), // Crash OK &import_value)); auto import_status = - FromConformanceValue(import_value, runtime->GetDescriptorPool(), - runtime->GetMessageFactory(), &arena); + FromExprValue(import_value, runtime->GetDescriptorPool(), + runtime->GetMessageFactory(), &arena); if (!import_status.ok()) { return absl::InternalError(import_status.status().ToString( absl::StatusToStringMode::kWithEverything)); @@ -591,9 +591,8 @@ class ModernConformanceServiceImpl : public ConformanceServiceInterface { ->mutable_message() = std::string( error.ToString(absl::StatusToStringMode::kWithEverything)); } else { - auto export_status = - ToConformanceValue(result, runtime->GetDescriptorPool(), - runtime->GetMessageFactory(), &arena); + auto export_status = ToExprValue(result, runtime->GetDescriptorPool(), + runtime->GetMessageFactory(), &arena); if (!export_status.ok()) { return absl::InternalError(export_status.status().ToString( absl::StatusToStringMode::kWithEverything));