/proc/self/cwd/runtime/internal/convert_constant.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 "runtime/internal/convert_constant.h" |
16 | | |
17 | | #include <cstddef> |
18 | | #include <cstdint> |
19 | | |
20 | | #include "absl/base/nullability.h" |
21 | | #include "absl/status/status.h" |
22 | | #include "absl/status/statusor.h" |
23 | | #include "absl/time/time.h" |
24 | | #include "absl/types/variant.h" |
25 | | #include "common/constant.h" |
26 | | #include "common/value.h" |
27 | | #include "eval/internal/errors.h" |
28 | | #include "google/protobuf/arena.h" |
29 | | |
30 | | namespace cel::runtime_internal { |
31 | | namespace { |
32 | | using ::cel::Constant; |
33 | | |
34 | | struct ConvertVisitor { |
35 | | google::protobuf::Arena* const arena; |
36 | | |
37 | 0 | absl::StatusOr<cel::Value> operator()(std::monostate) { |
38 | 0 | return absl::InvalidArgumentError("unspecified constant"); |
39 | 0 | } |
40 | 275 | absl::StatusOr<cel::Value> operator()(std::nullptr_t) { return NullValue(); } |
41 | 15.4k | absl::StatusOr<cel::Value> operator()(bool value) { return BoolValue(value); } |
42 | 112k | absl::StatusOr<cel::Value> operator()(int64_t value) { |
43 | 112k | return IntValue(value); |
44 | 112k | } |
45 | 7.60k | absl::StatusOr<cel::Value> operator()(uint64_t value) { |
46 | 7.60k | return UintValue(value); |
47 | 7.60k | } |
48 | 9.16k | absl::StatusOr<cel::Value> operator()(double value) { |
49 | 9.16k | return DoubleValue(value); |
50 | 9.16k | } |
51 | 10.6k | absl::StatusOr<cel::Value> operator()(const cel::StringConstant& value) { |
52 | 10.6k | return StringValue::From(value, arena); |
53 | 10.6k | } |
54 | 1.59k | absl::StatusOr<cel::Value> operator()(const cel::BytesConstant& value) { |
55 | 1.59k | return BytesValue::From(value, arena); |
56 | 1.59k | } |
57 | 0 | absl::StatusOr<cel::Value> operator()(const absl::Duration duration) { |
58 | 0 | if (duration >= kDurationHigh || duration <= kDurationLow) { |
59 | 0 | return ErrorValue::WrapUnsafe(DurationOverflowError()); |
60 | 0 | } |
61 | 0 | return UnsafeDurationValue(duration); |
62 | 0 | } |
63 | 0 | absl::StatusOr<cel::Value> operator()(const absl::Time timestamp) { |
64 | 0 | return UnsafeTimestampValue(timestamp); |
65 | 0 | } |
66 | | }; |
67 | | |
68 | | } // namespace |
69 | | |
70 | | // Converts an Ast constant into a runtime value, managed according to the |
71 | | // given value factory. |
72 | | // |
73 | | // A status maybe returned if value creation fails. |
74 | | absl::StatusOr<Value> ConvertConstant(const Constant& constant, |
75 | 157k | google::protobuf::Arena* absl_nonnull arena) { |
76 | 157k | return absl::visit(ConvertVisitor{arena}, constant.constant_kind()); |
77 | 157k | } |
78 | | |
79 | | } // namespace cel::runtime_internal |