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