/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 "absl/base/attributes.h" |
21 | | #include "absl/base/nullability.h" |
22 | | #include "absl/functional/overload.h" |
23 | | #include "absl/status/status.h" |
24 | | #include "absl/strings/cord.h" |
25 | | #include "absl/strings/str_cat.h" |
26 | | #include "absl/strings/string_view.h" |
27 | | #include "absl/types/variant.h" |
28 | | #include "common/allocator.h" |
29 | | #include "common/memory.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 | | |
35 | | namespace cel::common_internal { |
36 | | |
37 | | namespace { |
38 | | |
39 | | using ::cel::well_known_types::AsVariant; |
40 | | using ::cel::well_known_types::GetValueReflectionOrDie; |
41 | | |
42 | | google::protobuf::Arena* absl_nonnull MessageArenaOr( |
43 | | const google::protobuf::Message* absl_nonnull message, |
44 | 0 | google::protobuf::Arena* absl_nonnull or_arena) { |
45 | 0 | google::protobuf::Arena* absl_nullable arena = message->GetArena(); |
46 | 0 | if (arena == nullptr) { |
47 | 0 | arena = or_arena; |
48 | 0 | } |
49 | 0 | return arena; |
50 | 0 | } |
51 | | |
52 | | } // namespace |
53 | | |
54 | | Value ParsedJsonValue(const google::protobuf::Message* absl_nonnull message, |
55 | 0 | google::protobuf::Arena* absl_nonnull arena) { |
56 | 0 | const auto reflection = GetValueReflectionOrDie(message->GetDescriptor()); |
57 | 0 | const auto kind_case = reflection.GetKindCase(*message); |
58 | 0 | switch (kind_case) { |
59 | 0 | case google::protobuf::Value::KIND_NOT_SET: |
60 | 0 | ABSL_FALLTHROUGH_INTENDED; |
61 | 0 | case google::protobuf::Value::kNullValue: |
62 | 0 | return NullValue(); |
63 | 0 | case google::protobuf::Value::kBoolValue: |
64 | 0 | return BoolValue(reflection.GetBoolValue(*message)); |
65 | 0 | case google::protobuf::Value::kNumberValue: |
66 | 0 | return DoubleValue(reflection.GetNumberValue(*message)); |
67 | 0 | case google::protobuf::Value::kStringValue: { |
68 | 0 | std::string scratch; |
69 | 0 | return absl::visit( |
70 | 0 | absl::Overload( |
71 | 0 | [&](absl::string_view string) -> StringValue { |
72 | 0 | if (string.empty()) { |
73 | 0 | return StringValue(); |
74 | 0 | } |
75 | 0 | if (string.data() == scratch.data() && |
76 | 0 | string.size() == scratch.size()) { |
77 | 0 | return StringValue(arena, std::move(scratch)); |
78 | 0 | } else { |
79 | 0 | return StringValue( |
80 | 0 | Borrower::Arena(MessageArenaOr(message, arena)), string); |
81 | 0 | } |
82 | 0 | }, |
83 | 0 | [&](absl::Cord&& cord) -> StringValue { |
84 | 0 | if (cord.empty()) { |
85 | 0 | return StringValue(); |
86 | 0 | } |
87 | 0 | return StringValue(std::move(cord)); |
88 | 0 | }), |
89 | 0 | AsVariant(reflection.GetStringValue(*message, scratch))); |
90 | 0 | } |
91 | 0 | case google::protobuf::Value::kListValue: |
92 | 0 | return ParsedJsonListValue(&reflection.GetListValue(*message), |
93 | 0 | MessageArenaOr(message, arena)); |
94 | 0 | case google::protobuf::Value::kStructValue: |
95 | 0 | return ParsedJsonMapValue(&reflection.GetStructValue(*message), |
96 | 0 | MessageArenaOr(message, arena)); |
97 | 0 | default: |
98 | 0 | return ErrorValue(absl::InvalidArgumentError( |
99 | 0 | absl::StrCat("unexpected value kind case: ", kind_case))); |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | | } // namespace cel::common_internal |