| 52 | } // namespace |
| 53 | |
| 54 | Value ParsedJsonValue(const google::protobuf::Message* absl_nonnull message, |
| 55 | google::protobuf::Arena* absl_nonnull arena) { |
| 56 | const auto reflection = GetValueReflectionOrDie(message->GetDescriptor()); |
| 57 | const auto kind_case = reflection.GetKindCase(*message); |
| 58 | switch (kind_case) { |
| 59 | case google::protobuf::Value::KIND_NOT_SET: |
| 60 | ABSL_FALLTHROUGH_INTENDED; |
| 61 | case google::protobuf::Value::kNullValue: |
| 62 | return NullValue(); |
| 63 | case google::protobuf::Value::kBoolValue: |
| 64 | return BoolValue(reflection.GetBoolValue(*message)); |
| 65 | case google::protobuf::Value::kNumberValue: |
| 66 | return DoubleValue(reflection.GetNumberValue(*message)); |
| 67 | case google::protobuf::Value::kStringValue: { |
| 68 | std::string scratch; |
| 69 | return absl::visit( |
| 70 | absl::Overload( |
| 71 | [&](absl::string_view string) -> StringValue { |
| 72 | if (string.empty()) { |
| 73 | return StringValue(); |
| 74 | } |
| 75 | if (string.data() == scratch.data() && |
| 76 | string.size() == scratch.size()) { |
| 77 | return StringValue(arena, std::move(scratch)); |
| 78 | } else { |
| 79 | return StringValue( |
| 80 | Borrower::Arena(MessageArenaOr(message, arena)), string); |
| 81 | } |
| 82 | }, |
| 83 | [&](absl::Cord&& cord) -> StringValue { |
| 84 | if (cord.empty()) { |
| 85 | return StringValue(); |
| 86 | } |
| 87 | return StringValue(std::move(cord)); |
| 88 | }), |
| 89 | AsVariant(reflection.GetStringValue(*message, scratch))); |
| 90 | } |
| 91 | case google::protobuf::Value::kListValue: |
| 92 | return ParsedJsonListValue(&reflection.GetListValue(*message), |
| 93 | MessageArenaOr(message, arena)); |
| 94 | case google::protobuf::Value::kStructValue: |
| 95 | return ParsedJsonMapValue(&reflection.GetStructValue(*message), |
| 96 | MessageArenaOr(message, arena)); |
| 97 | default: |
| 98 | return ErrorValue(absl::InvalidArgumentError( |
| 99 | absl::StrCat("unexpected value kind case: ", kind_case))); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } // namespace cel::common_internal |