Coverage Report

Created: 2026-09-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
9.30k
    google::protobuf::Message* absl_nonnull json) const {
72
9.30k
  ABSL_DCHECK(descriptor_pool != nullptr);
73
9.30k
  ABSL_DCHECK(message_factory != nullptr);
74
9.30k
  ABSL_DCHECK(json != nullptr);
75
9.30k
  ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
76
9.30k
                 google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
77
78
9.30k
  CEL_RETURN_IF_ERROR(cel::internal::ValidateDuration(NativeValue()));
79
80
9.30k
  ValueReflection value_reflection;
81
9.30k
  CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
82
9.30k
  value_reflection.SetStringValueFromDuration(json, NativeValue());
83
84
9.30k
  return absl::OkStatus();
85
9.30k
}
86
87
absl::Status DurationValue::Equal(
88
    const Value& other,
89
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
90
    google::protobuf::MessageFactory* absl_nonnull message_factory,
91
13.3k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
92
13.3k
  ABSL_DCHECK(descriptor_pool != nullptr);
93
13.3k
  ABSL_DCHECK(message_factory != nullptr);
94
13.3k
  ABSL_DCHECK(arena != nullptr);
95
13.3k
  ABSL_DCHECK(result != nullptr);
96
97
13.3k
  if (auto other_value = other.AsDuration(); other_value.has_value()) {
98
13.1k
    *result = BoolValue{NativeValue() == other_value->NativeValue()};
99
13.1k
    return absl::OkStatus();
100
13.1k
  }
101
206
  *result = FalseValue();
102
206
  return absl::OkStatus();
103
13.3k
}
104
105
}  // namespace cel