forked from cel-expr/cel-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_cel_type.h
More file actions
111 lines (94 loc) · 3.72 KB
/
Copy pathpy_cel_type.h
File metadata and controls
111 lines (94 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef THIRD_PARTY_CEL_PYTHON_PY_CEL_TYPE_H_
#define THIRD_PARTY_CEL_PYTHON_PY_CEL_TYPE_H_
#include <Python.h> // IWYU pragma: keep - Needed for PyObject
#include <cstddef>
#include <functional>
#include <string>
#include <vector>
#include "cel/expr/checked.pb.h"
#include "absl/status/statusor.h"
#include "common/kind.h"
#include "common/type.h"
#include "common/value.h"
#include "env/config.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include <pybind11/pybind11.h>
namespace cel_python {
// Represents a CEL type.
class PyCelType {
public:
static void DefinePythonBindings(pybind11::module& m);
// Creates a dynamic type.
PyCelType();
PyCelType(const PyCelType& other) = default;
PyCelType& operator=(const PyCelType& other) = default;
PyCelType(PyCelType&& other) = default;
PyCelType& operator=(PyCelType&& other) = default;
// Creates a message type.
explicit PyCelType(const std::string& name);
PyCelType(cel::Kind kind, const std::string& name);
PyCelType(cel::Kind kind, const std::string& name,
std::vector<PyCelType> parameters);
~PyCelType() = default;
static PyCelType ListType(const PyCelType& element_type);
// May throw if the key type is not supported.
static PyCelType MapType(const PyCelType& key_type,
const PyCelType& value_type);
static PyCelType TypeType(const PyCelType& parameter);
static PyCelType AbstractType(const std::string& name,
const std::vector<PyCelType>& params = {});
cel::Kind GetKind() const { return kind_; }
std::string GetName() const;
bool IsAssignableFrom(const PyCelType& other) const;
bool IsMessage() const { return kind_ == cel::Kind::kMessage; }
size_t GetParamCount() const { return parameters_.size(); }
const PyCelType& GetParam(int index) const { return parameters_[index]; }
std::string ToString() const;
static absl::StatusOr<PyCelType> ForPyObject(
PyObject* py_object, const std::function<std::string()>& context);
static PyCelType ForCelValue(const cel::Value& cel_value);
static PyCelType FromCelType(const cel::Type& cel_type);
static PyCelType FromTypeProto(const cel::expr::Type& type);
static absl::StatusOr<cel::Type> ToCelType(
const PyCelType& type, google::protobuf::Arena* arena,
const google::protobuf::DescriptorPool& descriptor_pool);
static cel::Config::TypeInfo ToTypeInfo(const PyCelType& type);
bool operator==(const PyCelType& other) const;
static const PyCelType& Unknown();
static const PyCelType& Null();
static const PyCelType& Bool();
static const PyCelType& Int();
static const PyCelType& Uint();
static const PyCelType& Double();
static const PyCelType& Bytes();
static const PyCelType& String();
static const PyCelType& Error();
static const PyCelType& Dyn();
static const PyCelType& List();
static const PyCelType& Map();
static const PyCelType& Timestamp();
static const PyCelType& Duration();
static const PyCelType& Type();
private:
cel::Kind kind_;
std::string name_;
std::vector<PyCelType> parameters_;
};
} // namespace cel_python
#endif // THIRD_PARTY_CEL_PYTHON_PY_CEL_TYPE_H_