Coverage Report

Created: 2026-10-09 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/ast/constant_proto.cc
Line
Count
Source
1
// Copyright 2024 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 "common/ast/constant_proto.h"
16
17
#include <cstddef>
18
#include <cstdint>
19
20
#include "cel/expr/syntax.pb.h"
21
#include "google/protobuf/struct.pb.h"
22
#include "absl/base/nullability.h"
23
#include "absl/functional/overload.h"
24
#include "absl/status/status.h"
25
#include "absl/strings/str_cat.h"
26
#include "absl/time/time.h"
27
#include "absl/types/variant.h"
28
#include "common/constant.h"
29
#include "internal/proto_time_encoding.h"
30
31
namespace cel::ast_internal {
32
33
using ConstantProto = cel::expr::Constant;
34
35
absl::Status ConstantToProto(const Constant& constant,
36
386k
                             ConstantProto* absl_nonnull proto) {
37
386k
  return absl::visit(absl::Overload(
38
386k
                         [proto](std::monostate) -> absl::Status {
39
0
                           proto->clear_constant_kind();
40
0
                           return absl::OkStatus();
41
0
                         },
42
386k
                         [proto](std::nullptr_t) -> absl::Status {
43
517
                           proto->set_null_value(google::protobuf::NULL_VALUE);
44
517
                           return absl::OkStatus();
45
517
                         },
46
386k
                         [proto](bool value) -> absl::Status {
47
19.1k
                           proto->set_bool_value(value);
48
19.1k
                           return absl::OkStatus();
49
19.1k
                         },
50
386k
                         [proto](int64_t value) -> absl::Status {
51
226k
                           proto->set_int64_value(value);
52
226k
                           return absl::OkStatus();
53
226k
                         },
54
386k
                         [proto](uint64_t value) -> absl::Status {
55
8.12k
                           proto->set_uint64_value(value);
56
8.12k
                           return absl::OkStatus();
57
8.12k
                         },
58
386k
                         [proto](double value) -> absl::Status {
59
15.4k
                           proto->set_double_value(value);
60
15.4k
                           return absl::OkStatus();
61
15.4k
                         },
62
386k
                         [proto](const BytesConstant& value) -> absl::Status {
63
2.28k
                           proto->set_bytes_value(value);
64
2.28k
                           return absl::OkStatus();
65
2.28k
                         },
66
386k
                         [proto](const StringConstant& value) -> absl::Status {
67
114k
                           proto->set_string_value(value);
68
114k
                           return absl::OkStatus();
69
114k
                         },
70
386k
                         [proto](absl::Duration value) -> absl::Status {
71
0
                           return internal::EncodeDuration(
72
0
                               value, proto->mutable_duration_value());
73
0
                         },
74
386k
                         [proto](absl::Time value) -> absl::Status {
75
0
                           return internal::EncodeTime(
76
0
                               value, proto->mutable_timestamp_value());
77
0
                         }),
78
386k
                     constant.kind());
79
386k
}
80
81
160k
absl::Status ConstantFromProto(const ConstantProto& proto, Constant& constant) {
82
160k
  switch (proto.constant_kind_case()) {
83
0
    case ConstantProto::CONSTANT_KIND_NOT_SET:
84
0
      constant = Constant{};
85
0
      break;
86
275
    case ConstantProto::kNullValue:
87
275
      constant.set_null_value();
88
275
      break;
89
17.9k
    case ConstantProto::kBoolValue:
90
17.9k
      constant.set_bool_value(proto.bool_value());
91
17.9k
      break;
92
112k
    case ConstantProto::kInt64Value:
93
112k
      constant.set_int_value(proto.int64_value());
94
112k
      break;
95
7.60k
    case ConstantProto::kUint64Value:
96
7.60k
      constant.set_uint_value(proto.uint64_value());
97
7.60k
      break;
98
9.17k
    case ConstantProto::kDoubleValue:
99
9.17k
      constant.set_double_value(proto.double_value());
100
9.17k
      break;
101
10.6k
    case ConstantProto::kStringValue:
102
10.6k
      constant.set_string_value(proto.string_value());
103
10.6k
      break;
104
1.59k
    case ConstantProto::kBytesValue:
105
1.59k
      constant.set_bytes_value(proto.bytes_value());
106
1.59k
      break;
107
0
    case ConstantProto::kDurationValue:
108
0
      constant.set_duration_value(
109
0
          internal::DecodeDuration(proto.duration_value()));
110
0
      break;
111
0
    case ConstantProto::kTimestampValue:
112
0
      constant.set_timestamp_value(
113
0
          internal::DecodeTime(proto.timestamp_value()));
114
0
      break;
115
0
    default:
116
0
      return absl::InvalidArgumentError(
117
0
          absl::StrCat("unexpected ConstantKindCase: ",
118
0
                       static_cast<int>(proto.constant_kind_case())));
119
160k
  }
120
160k
  return absl::OkStatus();
121
160k
}
122
123
}  // namespace cel::ast_internal