Coverage Report

Created: 2026-10-09 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/values/bytes_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/wrappers.pb.h"
18
#include "absl/base/nullability.h"
19
#include "absl/functional/overload.h"
20
#include "absl/log/absl_check.h"
21
#include "absl/status/status.h"
22
#include "absl/strings/cord.h"
23
#include "absl/strings/str_cat.h"
24
#include "absl/strings/string_view.h"
25
#include "common/internal/byte_string.h"
26
#include "common/value.h"
27
#include "internal/status_macros.h"
28
#include "internal/strings.h"
29
#include "internal/well_known_types.h"
30
#include "google/protobuf/arena.h"
31
#include "google/protobuf/descriptor.h"
32
#include "google/protobuf/io/zero_copy_stream.h"
33
#include "google/protobuf/message.h"
34
35
namespace cel {
36
37
namespace {
38
39
using ::cel::well_known_types::ValueReflection;
40
41
template <typename Bytes>
42
0
std::string BytesDebugString(const Bytes& value) {
43
0
  return value.NativeValue(absl::Overload(
44
0
      [](absl::string_view string) -> std::string {
45
0
        return internal::FormatBytesLiteral(string);
46
0
      },
47
0
      [](const absl::Cord& cord) -> std::string {
48
0
        if (auto flat = cord.TryFlat(); flat.has_value()) {
49
0
          return internal::FormatBytesLiteral(*flat);
50
0
        }
51
0
        return internal::FormatBytesLiteral(static_cast<std::string>(cord));
52
0
      }));
53
0
}
54
55
}  // namespace
56
57
BytesValue BytesValue::Concat(const BytesValue& lhs, const BytesValue& rhs,
58
1.41k
                              google::protobuf::Arena* absl_nonnull arena) {
59
1.41k
  return BytesValue(
60
1.41k
      common_internal::ByteString::Concat(lhs.value_, rhs.value_, arena));
61
1.41k
}
62
63
0
std::string BytesValue::DebugString() const { return BytesDebugString(*this); }
64
65
absl::Status BytesValue::SerializeTo(
66
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
67
    google::protobuf::MessageFactory* absl_nonnull message_factory,
68
0
    google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const {
69
0
  ABSL_DCHECK(descriptor_pool != nullptr);
70
0
  ABSL_DCHECK(message_factory != nullptr);
71
0
  ABSL_DCHECK(output != nullptr);
72
73
0
  google::protobuf::BytesValue message;
74
0
  message.set_value(NativeString());
75
0
  if (!message.SerializePartialToZeroCopyStream(output)) {
76
0
    return absl::UnknownError(
77
0
        absl::StrCat("failed to serialize message: ", message.GetTypeName()));
78
0
  }
79
80
0
  return absl::OkStatus();
81
0
}
82
83
absl::Status BytesValue::ConvertToJson(
84
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
85
    google::protobuf::MessageFactory* absl_nonnull message_factory,
86
2.50k
    google::protobuf::Message* absl_nonnull json) const {
87
2.50k
  ABSL_DCHECK(descriptor_pool != nullptr);
88
2.50k
  ABSL_DCHECK(message_factory != nullptr);
89
2.50k
  ABSL_DCHECK(json != nullptr);
90
2.50k
  ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
91
2.50k
                 google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
92
93
2.50k
  ValueReflection value_reflection;
94
2.50k
  CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
95
2.50k
  NativeValue([&](const auto& value) {
96
2.50k
    value_reflection.SetStringValueFromBytes(json, value);
97
2.50k
  });
bytes_value.cc:auto cel::BytesValue::ConvertToJson(google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Message*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const
Line
Count
Source
95
2.50k
  NativeValue([&](const auto& value) {
96
2.50k
    value_reflection.SetStringValueFromBytes(json, value);
97
2.50k
  });
Unexecuted instantiation: bytes_value.cc:auto cel::BytesValue::ConvertToJson(google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Message*) const::$_0::operator()<absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&) const
98
99
2.50k
  return absl::OkStatus();
100
2.50k
}
101
102
absl::Status BytesValue::Equal(
103
    const Value& other,
104
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
105
    google::protobuf::MessageFactory* absl_nonnull message_factory,
106
1.13k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
107
1.13k
  ABSL_DCHECK(descriptor_pool != nullptr);
108
1.13k
  ABSL_DCHECK(message_factory != nullptr);
109
1.13k
  ABSL_DCHECK(arena != nullptr);
110
1.13k
  ABSL_DCHECK(result != nullptr);
111
112
1.13k
  if (auto other_value = other.AsBytes(); other_value.has_value()) {
113
1.05k
    *result = NativeValue([other_value](const auto& value) -> BoolValue {
114
1.05k
      return other_value->NativeValue(
115
1.05k
          [&value](const auto& other_value) -> BoolValue {
116
1.05k
            return BoolValue{value == other_value};
117
1.05k
          });
bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const::{lambda(auto:1 const&)#1}::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const
Line
Count
Source
115
1.05k
          [&value](const auto& other_value) -> BoolValue {
116
1.05k
            return BoolValue{value == other_value};
117
1.05k
          });
Unexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const::{lambda(auto:1 const&)#1}::operator()<absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&) const
Unexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&) const::{lambda(auto:1 const&)#1}::operator()<std::__1::basic_string_view<char, {lambda(auto:1 const&)#1}::operator()::char_traits<char> > >(std::__1::basic_string_view<char, {lambda(auto:1 const&)#1}::operator()::char_traits<char> > const&) const
Unexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&) const::{lambda(auto:1 const&)#1}::operator()<absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&) const
118
1.05k
    });
bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const
Line
Count
Source
113
1.05k
    *result = NativeValue([other_value](const auto& value) -> BoolValue {
114
1.05k
      return other_value->NativeValue(
115
1.05k
          [&value](const auto& other_value) -> BoolValue {
116
1.05k
            return BoolValue{value == other_value};
117
1.05k
          });
118
1.05k
    });
Unexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&) const
119
1.05k
    return absl::OkStatus();
120
1.05k
  }
121
75
  *result = FalseValue();
122
75
  return absl::OkStatus();
123
1.13k
}
124
125
}  // namespace cel