/proc/self/cwd/common/values/parsed_json_value.cc
Line | Count | Source |
1 | | // Copyright 2024 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/values/parsed_json_value.h" |
16 | | |
17 | | #include <string> |
18 | | #include <utility> |
19 | | |
20 | | #include "google/protobuf/struct.pb.h" |
21 | | #include "absl/base/attributes.h" |
22 | | #include "absl/base/nullability.h" |
23 | | #include "absl/base/optimization.h" |
24 | | #include "absl/functional/overload.h" |
25 | | #include "absl/status/status.h" |
26 | | #include "absl/strings/cord.h" |
27 | | #include "absl/strings/str_cat.h" |
28 | | #include "absl/strings/string_view.h" |
29 | | #include "absl/types/variant.h" |
30 | | #include "common/value.h" |
31 | | #include "internal/well_known_types.h" |
32 | | #include "google/protobuf/arena.h" |
33 | | #include "google/protobuf/message.h" |
34 | | #include "google/protobuf/message_lite.h" |
35 | | |
36 | | namespace cel::common_internal { |
37 | | |
38 | | namespace { |
39 | | |
40 | | using ::cel::well_known_types::AsVariant; |
41 | | using ::cel::well_known_types::GetValueReflectionOrDie; |
42 | | using ::cel::well_known_types::ValueReflection; |
43 | | |
44 | | google::protobuf::Arena* absl_nonnull MessageArenaOr( |
45 | | const google::protobuf::Message* absl_nonnull message, |
46 | 11.9k | google::protobuf::Arena* absl_nonnull or_arena) { |
47 | 11.9k | google::protobuf::Arena* absl_nullable arena = message->GetArena(); |
48 | 11.9k | if (arena == nullptr) { |
49 | 0 | arena = or_arena; |
50 | 0 | } |
51 | 11.9k | return arena; |
52 | 11.9k | } |
53 | | |
54 | | } // namespace |
55 | | |
56 | | Value ParsedJsonValue(const google::protobuf::Message* absl_nonnull message, |
57 | 24.5k | google::protobuf::Arena* absl_nonnull arena) { |
58 | 24.5k | if (const auto* generated_message = |
59 | 24.5k | google::protobuf::DynamicCastMessage<google::protobuf::Value>(message); |
60 | 24.5k | ABSL_PREDICT_TRUE(generated_message != nullptr)) { |
61 | 24.5k | const auto kind_case = ValueReflection::GetKindCase(*generated_message); |
62 | 24.5k | switch (kind_case) { |
63 | 0 | case google::protobuf::Value::KIND_NOT_SET: |
64 | 0 | ABSL_FALLTHROUGH_INTENDED; |
65 | 236 | case google::protobuf::Value::kNullValue: |
66 | 236 | return NullValue(); |
67 | 630 | case google::protobuf::Value::kBoolValue: |
68 | 630 | return BoolValue(ValueReflection::GetBoolValue(*generated_message)); |
69 | 10.5k | case google::protobuf::Value::kNumberValue: |
70 | 10.5k | return DoubleValue(ValueReflection::GetNumberValue(*generated_message)); |
71 | 1.23k | case google::protobuf::Value::kStringValue: { |
72 | 1.23k | absl::string_view string = |
73 | 1.23k | ValueReflection::GetStringValue(*generated_message); |
74 | 1.23k | if (string.empty()) { |
75 | 344 | return StringValue(); |
76 | 344 | } |
77 | 891 | if (google::protobuf::Arena* message_arena = generated_message->GetArena(); |
78 | 891 | message_arena != nullptr) { |
79 | 891 | return StringValue::Wrap(string, message_arena); |
80 | 891 | } |
81 | 0 | return StringValue::From(string, arena); |
82 | 891 | } |
83 | 838 | case google::protobuf::Value::kListValue: |
84 | 838 | return ParsedJsonListValue( |
85 | 838 | &ValueReflection::GetListValue(*generated_message), |
86 | 838 | MessageArenaOr(generated_message, arena)); |
87 | 11.0k | case google::protobuf::Value::kStructValue: |
88 | 11.0k | return ParsedJsonMapValue( |
89 | 11.0k | &ValueReflection::GetStructValue(*generated_message), |
90 | 11.0k | MessageArenaOr(generated_message, arena)); |
91 | 0 | default: |
92 | 0 | return ErrorValue::From(absl::InvalidArgumentError(absl::StrCat( |
93 | 0 | "unexpected value kind case: ", kind_case)), |
94 | 0 | arena); |
95 | 24.5k | } |
96 | 24.5k | } |
97 | 0 | const auto reflection = GetValueReflectionOrDie(message->GetDescriptor()); |
98 | 0 | const auto kind_case = reflection.GetKindCase(*message); |
99 | 0 | switch (kind_case) { |
100 | 0 | case google::protobuf::Value::KIND_NOT_SET: |
101 | 0 | ABSL_FALLTHROUGH_INTENDED; |
102 | 0 | case google::protobuf::Value::kNullValue: |
103 | 0 | return NullValue(); |
104 | 0 | case google::protobuf::Value::kBoolValue: |
105 | 0 | return BoolValue(reflection.GetBoolValue(*message)); |
106 | 0 | case google::protobuf::Value::kNumberValue: |
107 | 0 | return DoubleValue(reflection.GetNumberValue(*message)); |
108 | 0 | case google::protobuf::Value::kStringValue: { |
109 | 0 | std::string scratch; |
110 | 0 | return absl::visit( |
111 | 0 | absl::Overload( |
112 | 0 | [&](absl::string_view string) -> StringValue { |
113 | 0 | if (string.empty()) { |
114 | 0 | return StringValue(); |
115 | 0 | } |
116 | 0 | if (string.data() == scratch.data() && |
117 | 0 | string.size() == scratch.size()) { |
118 | 0 | return StringValue::From(std::move(scratch), arena); |
119 | 0 | } else { |
120 | 0 | if (google::protobuf::Arena* message_arena = message->GetArena(); |
121 | 0 | message_arena != nullptr) { |
122 | 0 | return StringValue::Wrap(string, message_arena); |
123 | 0 | } |
124 | 0 | return StringValue::From(string, arena); |
125 | 0 | } |
126 | 0 | }, |
127 | 0 | [&](absl::Cord&& cord) -> StringValue { |
128 | 0 | if (cord.empty()) { |
129 | 0 | return StringValue(); |
130 | 0 | } |
131 | 0 | return StringValue::From(std::move(cord), arena); |
132 | 0 | }), |
133 | 0 | AsVariant(reflection.GetStringValue(*message, scratch))); |
134 | 0 | } |
135 | 0 | case google::protobuf::Value::kListValue: |
136 | 0 | return ParsedJsonListValue(&reflection.GetListValue(*message), |
137 | 0 | MessageArenaOr(message, arena)); |
138 | 0 | case google::protobuf::Value::kStructValue: |
139 | 0 | return ParsedJsonMapValue(&reflection.GetStructValue(*message), |
140 | 0 | MessageArenaOr(message, arena)); |
141 | 0 | default: |
142 | 0 | return ErrorValue::From(absl::InvalidArgumentError(absl::StrCat( |
143 | 0 | "unexpected value kind case: ", kind_case)), |
144 | 0 | arena); |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | | } // namespace cel::common_internal |