MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / ExportAsProtoValue

Function ExportAsProtoValue

eval/public/value_export_util.cc:41–149  ·  view source on GitHub ↗

Export content of CelValue as google.protobuf.Value.

Source from the content-addressed store, hash-verified

39
40// Export content of CelValue as google.protobuf.Value.
41absl::Status ExportAsProtoValue(const CelValue& in_value, Value* out_value,
42 google::protobuf::Arena* arena) {
43 if (in_value.IsNull()) {
44 out_value->set_null_value(google::protobuf::NULL_VALUE);
45 return absl::OkStatus();
46 }
47 switch (in_value.type()) {
48 case CelValue::Type::kBool: {
49 out_value->set_bool_value(in_value.BoolOrDie());
50 break;
51 }
52 case CelValue::Type::kInt64: {
53 out_value->set_number_value(static_cast<double>(in_value.Int64OrDie()));
54 break;
55 }
56 case CelValue::Type::kUint64: {
57 out_value->set_number_value(static_cast<double>(in_value.Uint64OrDie()));
58 break;
59 }
60 case CelValue::Type::kDouble: {
61 out_value->set_number_value(in_value.DoubleOrDie());
62 break;
63 }
64 case CelValue::Type::kString: {
65 auto value = in_value.StringOrDie().value();
66 out_value->set_string_value(value.data(), value.size());
67 break;
68 }
69 case CelValue::Type::kBytes: {
70 *out_value->mutable_string_value() =
71 absl::Base64Escape(in_value.BytesOrDie().value());
72 break;
73 }
74 case CelValue::Type::kDuration: {
75 Duration duration;
76 auto status =
77 cel::internal::EncodeDuration(in_value.DurationOrDie(), &duration);
78 if (!status.ok()) {
79 return status;
80 }
81 out_value->set_string_value(TimeUtil::ToString(duration));
82 break;
83 }
84 case CelValue::Type::kTimestamp: {
85 Timestamp timestamp;
86 auto status =
87 cel::internal::EncodeTime(in_value.TimestampOrDie(), &timestamp);
88 if (!status.ok()) {
89 return status;
90 }
91 out_value->set_string_value(TimeUtil::ToString(timestamp));
92 break;
93 }
94 case CelValue::Type::kMessage: {
95 google::protobuf::util::JsonPrintOptions json_options;
96 json_options.preserve_proto_field_names = true;
97 std::string json;
98 auto status = google::protobuf::util::MessageToJsonString(*in_value.MessageOrDie(),

Callers

nothing calls this directly

Calls 15

EncodeDurationFunction · 0.85
EncodeTimeFunction · 0.85
KeyAsStringFunction · 0.85
CelValueClass · 0.85
typeMethod · 0.80
BoolOrDieMethod · 0.80
Int64OrDieMethod · 0.80
Uint64OrDieMethod · 0.80
DoubleOrDieMethod · 0.80
StringOrDieMethod · 0.80
BytesOrDieMethod · 0.80
DurationOrDieMethod · 0.80

Tested by

no test coverage detected