Coverage Report

Created: 2026-07-16 06:29

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
458k
                             ConstantProto* absl_nonnull proto) {
37
458k
  return absl::visit(absl::Overload(
38
458k
                         [proto](std::monostate) -> absl::Status {
39
0
                           proto->clear_constant_kind();
40
0
                           return absl::OkStatus();
41
0
                         },
42
458k
                         [proto](std::nullptr_t) -> absl::Status {
43
1.89k
                           proto->set_null_value(google::protobuf::NULL_VALUE);
44
1.89k
                           return absl::OkStatus();
45
1.89k
                         },
46
458k
                         [proto](bool value) -> absl::Status {
47
10.0k
                           proto->set_bool_value(value);
48
10.0k
                           return absl::OkStatus();
49
10.0k
                         },
50
458k
                         [proto](int64_t value) -> absl::Status {
51
261k
                           proto->set_int64_value(value);
52
261k
                           return absl::OkStatus();
53
261k
                         },
54
458k
                         [proto](uint64_t value) -> absl::Status {
55
11.2k
                           proto->set_uint64_value(value);
56
11.2k
                           return absl::OkStatus();
57
11.2k
                         },
58
458k
                         [proto](double value) -> absl::Status {
59
20.8k
                           proto->set_double_value(value);
60
20.8k
                           return absl::OkStatus();
61
20.8k
                         },
62
458k
                         [proto](const BytesConstant& value) -> absl::Status {
63
3.00k
                           proto->set_bytes_value(value);
64
3.00k
                           return absl::OkStatus();
65
3.00k
                         },
66
458k
                         [proto](const StringConstant& value) -> absl::Status {
67
150k
                           proto->set_string_value(value);
68
150k
                           return absl::OkStatus();
69
150k
                         },
70
458k
                         [proto](absl::Duration value) -> absl::Status {
71
0
                           return internal::EncodeDuration(
72
0
                               value, proto->mutable_duration_value());
73
0
                         },
74
458k
                         [proto](absl::Time value) -> absl::Status {
75
0
                           return internal::EncodeTime(
76
0
                               value, proto->mutable_timestamp_value());
77
0
                         }),
78
458k
                     constant.kind());
79
458k
}
80
81
161k
absl::Status ConstantFromProto(const ConstantProto& proto, Constant& constant) {
82
161k
  switch (proto.constant_kind_case()) {
83
0
    case ConstantProto::CONSTANT_KIND_NOT_SET:
84
0
      constant = Constant{};
85
0
      break;
86
339
    case ConstantProto::kNullValue:
87
339
      constant.set_null_value();
88
339
      break;
89
8.99k
    case ConstantProto::kBoolValue:
90
8.99k
      constant.set_bool_value(proto.bool_value());
91
8.99k
      break;
92
113k
    case ConstantProto::kInt64Value:
93
113k
      constant.set_int_value(proto.int64_value());
94
113k
      break;
95
10.5k
    case ConstantProto::kUint64Value:
96
10.5k
      constant.set_uint_value(proto.uint64_value());
97
10.5k
      break;
98
14.3k
    case ConstantProto::kDoubleValue:
99
14.3k
      constant.set_double_value(proto.double_value());
100
14.3k
      break;
101
10.3k
    case ConstantProto::kStringValue:
102
10.3k
      constant.set_string_value(proto.string_value());
103
10.3k
      break;
104
2.53k
    case ConstantProto::kBytesValue:
105
2.53k
      constant.set_bytes_value(proto.bytes_value());
106
2.53k
      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
161k
  }
120
161k
  return absl::OkStatus();
121
161k
}
122
123
}  // namespace cel::ast_internal