/proc/self/cwd/common/values/duration_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 <string> |
16 | | |
17 | | #include "google/protobuf/duration.pb.h" |
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 "absl/time/time.h" |
23 | | #include "common/value.h" |
24 | | #include "internal/status_macros.h" |
25 | | #include "internal/time.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::DurationReflection; |
37 | | using ::cel::well_known_types::ValueReflection; |
38 | | |
39 | 0 | std::string DurationDebugString(absl::Duration value) { |
40 | 0 | return internal::DebugStringDuration(value); |
41 | 0 | } |
42 | | |
43 | | } // namespace |
44 | | |
45 | 0 | std::string DurationValue::DebugString() const { |
46 | 0 | return DurationDebugString(NativeValue()); |
47 | 0 | } |
48 | | |
49 | | absl::Status DurationValue::SerializeTo( |
50 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
51 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
52 | 0 | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const { |
53 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
54 | 0 | ABSL_DCHECK(message_factory != nullptr); |
55 | 0 | ABSL_DCHECK(output != nullptr); |
56 | |
|
57 | 0 | google::protobuf::Duration message; |
58 | 0 | CEL_RETURN_IF_ERROR( |
59 | 0 | DurationReflection::SetFromAbslDuration(&message, NativeValue())); |
60 | 0 | if (!message.SerializePartialToZeroCopyStream(output)) { |
61 | 0 | return absl::UnknownError( |
62 | 0 | absl::StrCat("failed to serialize message: ", message.GetTypeName())); |
63 | 0 | } |
64 | | |
65 | 0 | return absl::OkStatus(); |
66 | 0 | } |
67 | | |
68 | | absl::Status DurationValue::ConvertToJson( |
69 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
70 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
71 | 0 | google::protobuf::Message* absl_nonnull json) const { |
72 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
73 | 0 | ABSL_DCHECK(message_factory != nullptr); |
74 | 0 | ABSL_DCHECK(json != nullptr); |
75 | 0 | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
76 | 0 | google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE); |
77 | |
|
78 | 0 | ValueReflection value_reflection; |
79 | 0 | CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor())); |
80 | 0 | value_reflection.SetStringValueFromDuration(json, NativeValue()); |
81 | |
|
82 | 0 | return absl::OkStatus(); |
83 | 0 | } |
84 | | |
85 | | absl::Status DurationValue::Equal( |
86 | | const Value& other, |
87 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
88 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
89 | 0 | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const { |
90 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
91 | 0 | ABSL_DCHECK(message_factory != nullptr); |
92 | 0 | ABSL_DCHECK(arena != nullptr); |
93 | 0 | ABSL_DCHECK(result != nullptr); |
94 | |
|
95 | 0 | if (auto other_value = other.AsDuration(); other_value.has_value()) { |
96 | 0 | *result = BoolValue{NativeValue() == other_value->NativeValue()}; |
97 | 0 | return absl::OkStatus(); |
98 | 0 | } |
99 | 0 | *result = FalseValue(); |
100 | 0 | return absl::OkStatus(); |
101 | 0 | } |
102 | | |
103 | | } // namespace cel |