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