Coverage Report

Created: 2025-11-29 07:01

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