/proc/self/cwd/common/values/uint_value.cc
Line | Count | Source |
1 | | // Copyright 2023 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 <cstdint> |
16 | | #include <string> |
17 | | |
18 | | #include "absl/base/nullability.h" |
19 | | #include "absl/log/absl_check.h" |
20 | | #include "absl/status/status.h" |
21 | | #include "absl/strings/str_cat.h" |
22 | | #include "common/value.h" |
23 | | #include "internal/number.h" |
24 | | #include "internal/status_macros.h" |
25 | | #include "internal/well_known_types.h" |
26 | | #include "google/protobuf/arena.h" |
27 | | #include "google/protobuf/descriptor.h" |
28 | | #include "google/protobuf/io/zero_copy_stream.h" |
29 | | #include "google/protobuf/message.h" |
30 | | |
31 | | namespace cel { |
32 | | |
33 | | namespace { |
34 | | |
35 | | using ::cel::well_known_types::ValueReflection; |
36 | | |
37 | 459 | std::string UintDebugString(int64_t value) { return absl::StrCat(value, "u"); } |
38 | | |
39 | | } // namespace |
40 | | |
41 | 459 | std::string UintValue::DebugString() const { |
42 | 459 | return UintDebugString(NativeValue()); |
43 | 459 | } |
44 | | |
45 | | absl::Status UintValue::SerializeTo( |
46 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
47 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
48 | 0 | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const { |
49 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
50 | 0 | ABSL_DCHECK(message_factory != nullptr); |
51 | 0 | ABSL_DCHECK(output != nullptr); |
52 | |
|
53 | 0 | google::protobuf::UInt64Value message; |
54 | 0 | message.set_value(NativeValue()); |
55 | 0 | if (!message.SerializePartialToZeroCopyStream(output)) { |
56 | 0 | return absl::UnknownError( |
57 | 0 | absl::StrCat("failed to serialize message: ", message.GetTypeName())); |
58 | 0 | } |
59 | | |
60 | 0 | return absl::OkStatus(); |
61 | 0 | } |
62 | | |
63 | | absl::Status UintValue::ConvertToJson( |
64 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
65 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
66 | 0 | google::protobuf::Message* absl_nonnull json) const { |
67 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
68 | 0 | ABSL_DCHECK(message_factory != nullptr); |
69 | 0 | ABSL_DCHECK(json != nullptr); |
70 | 0 | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
71 | 0 | google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE); |
72 | |
|
73 | 0 | ValueReflection value_reflection; |
74 | 0 | CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor())); |
75 | 0 | value_reflection.SetNumberValue(json, NativeValue()); |
76 | |
|
77 | 0 | return absl::OkStatus(); |
78 | 0 | } |
79 | | |
80 | | absl::Status UintValue::Equal( |
81 | | const Value& other, |
82 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
83 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
84 | 1.74k | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const { |
85 | 1.74k | ABSL_DCHECK(descriptor_pool != nullptr); |
86 | 1.74k | ABSL_DCHECK(message_factory != nullptr); |
87 | 1.74k | ABSL_DCHECK(arena != nullptr); |
88 | 1.74k | ABSL_DCHECK(result != nullptr); |
89 | | |
90 | 1.74k | if (auto other_value = other.AsUint(); other_value.has_value()) { |
91 | 178 | *result = BoolValue{NativeValue() == other_value->NativeValue()}; |
92 | 178 | return absl::OkStatus(); |
93 | 178 | } |
94 | 1.56k | if (auto other_value = other.AsDouble(); other_value.has_value()) { |
95 | 672 | *result = |
96 | 672 | BoolValue{internal::Number::FromUint64(NativeValue()) == |
97 | 672 | internal::Number::FromDouble(other_value->NativeValue())}; |
98 | 672 | return absl::OkStatus(); |
99 | 672 | } |
100 | 897 | if (auto other_value = other.AsInt(); other_value.has_value()) { |
101 | 408 | *result = |
102 | 408 | BoolValue{internal::Number::FromUint64(NativeValue()) == |
103 | 408 | internal::Number::FromInt64(other_value->NativeValue())}; |
104 | 408 | return absl::OkStatus(); |
105 | 408 | } |
106 | 489 | *result = FalseValue(); |
107 | 489 | return absl::OkStatus(); |
108 | 897 | } |
109 | | |
110 | | } // namespace cel |