cel 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +239 -0
- data/README.md +101 -0
- data/lib/cel/ast/elements.rb +322 -0
- data/lib/cel/ast/types.rb +78 -0
- data/lib/cel/checker.rb +323 -0
- data/lib/cel/context.rb +36 -0
- data/lib/cel/environment.rb +48 -0
- data/lib/cel/errors.rb +31 -0
- data/lib/cel/macro.rb +75 -0
- data/lib/cel/parser.rb +996 -0
- data/lib/cel/program.rb +147 -0
- data/lib/cel/protobuf.rb +175 -0
- data/lib/cel/version.rb +7 -0
- data/lib/cel.rb +29 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cd670ce6076540d040019d05ad1ae2a67bd8a8d94f60e51ea860090f69d4f179
|
4
|
+
data.tar.gz: 76782582c4d81ddb8dbd5fda83c67a56635a30eebe88cf9320bcfe21f7926c9c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40cadbc9e54c3ae8ea219c6d1a41bae8242ced85f8474115a865a70eff65fb234935874bcaf1d6b467b615f92224c4a6dba31aee30af9628585e77bb809d6ecd
|
7
|
+
data.tar.gz: 1f8269fb2391074acecedae34645d0cb3bd0c06eda26004d99f8fd7ea15b26735c2796f5124c74b474133aabe639a739599e248d280cd630f8c2e7d6dd94a6a6
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,239 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
https://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
Copyright 2013-2017 Docker, Inc.
|
180
|
+
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
182
|
+
you may not use this file except in compliance with the License.
|
183
|
+
You may obtain a copy of the License at
|
184
|
+
|
185
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
186
|
+
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
190
|
+
See the License for the specific language governing permissions and
|
191
|
+
limitations under the License.
|
192
|
+
|
193
|
+
|
194
|
+
* lib/httpx/domain_name.rb
|
195
|
+
|
196
|
+
This file is derived from the implementation of punycode available at
|
197
|
+
here:
|
198
|
+
|
199
|
+
https://www.verisign.com/en_US/channel-resources/domain-registry-products/idn-sdks/index.xhtml
|
200
|
+
|
201
|
+
Copyright (C) 2000-2002 Verisign Inc., All rights reserved.
|
202
|
+
|
203
|
+
Redistribution and use in source and binary forms, with or
|
204
|
+
without modification, are permitted provided that the following
|
205
|
+
conditions are met:
|
206
|
+
|
207
|
+
1) Redistributions of source code must retain the above copyright
|
208
|
+
notice, this list of conditions and the following disclaimer.
|
209
|
+
|
210
|
+
2) Redistributions in binary form must reproduce the above copyright
|
211
|
+
notice, this list of conditions and the following disclaimer in
|
212
|
+
the documentation and/or other materials provided with the
|
213
|
+
distribution.
|
214
|
+
|
215
|
+
3) Neither the name of the VeriSign Inc. nor the names of its
|
216
|
+
contributors may be used to endorse or promote products derived
|
217
|
+
from this software without specific prior written permission.
|
218
|
+
|
219
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
220
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
221
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
222
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
223
|
+
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
224
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
225
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
226
|
+
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
227
|
+
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
228
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
229
|
+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
230
|
+
POSSIBILITY OF SUCH DAMAGE.
|
231
|
+
|
232
|
+
This software is licensed under the BSD open source license. For more
|
233
|
+
information visit www.opensource.org.
|
234
|
+
|
235
|
+
Authors:
|
236
|
+
John Colosi (VeriSign)
|
237
|
+
Srikanth Veeramachaneni (VeriSign)
|
238
|
+
Nagesh Chigurupati (Verisign)
|
239
|
+
Praveen Srinivasan(Verisign)
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# Cel::Ruby
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/cel.svg)](http://rubygems.org/gems/cel)
|
4
|
+
[![pipeline status](https://gitlab.com/honeyryderchuck/cel-ruby/badges/master/pipeline.svg)](https://gitlab.com/honeyryderchuck/cel-ruby/pipelines?page=1&scope=all&ref=master)
|
5
|
+
[![coverage report](https://gitlab.com/honeyryderchuck/cel-ruby/badges/master/coverage.svg?job=coverage)](https://honeyryderchuck.gitlab.io/cel-ruby/coverage/#_AllFiles)
|
6
|
+
|
7
|
+
Pure Ruby implementation of Google Common Expression Language, https://opensource.google/projects/cel.
|
8
|
+
|
9
|
+
> The Common Expression Language (CEL) implements common semantics for expression evaluation, enabling different applications to more easily interoperate.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'cel'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install cel
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
The usage pattern follows the pattern defined by [cel-go](https://github.com/google/cel-go), i.e. define an environment, which then can be used to parse, compile and evaluate a CEL program.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require "cel"
|
33
|
+
|
34
|
+
# set the environment
|
35
|
+
env = Cel::Environment.new(name: :string, group: :string)
|
36
|
+
|
37
|
+
# parse and check
|
38
|
+
begin
|
39
|
+
ast = env.compile('name.startsWith("/groups/" + group)') #=> Cel::Types[:bool], which is == :bool
|
40
|
+
rescue Cel::Error => e
|
41
|
+
STDERR.puts("type-check error: #{e.message}")
|
42
|
+
raise e
|
43
|
+
end
|
44
|
+
|
45
|
+
# evaluate
|
46
|
+
begin
|
47
|
+
return_value = env.evaluate(ast,
|
48
|
+
name: Cel::String.new("/groups/acme.co/documents/secret-stuff"),
|
49
|
+
group: Cel::String.new("acme.co"))
|
50
|
+
rescue Cel::Error => e
|
51
|
+
STDERR.puts("evaluation error: #{e.message}")
|
52
|
+
raise e
|
53
|
+
end
|
54
|
+
|
55
|
+
puts return_value #=> true
|
56
|
+
```
|
57
|
+
|
58
|
+
### protobuf
|
59
|
+
|
60
|
+
If `google/protobuf` is available in the environment, `cel-ruby` will also be able to integrate with protobuf declarations in CEL expressions.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
# gem "google-protobuf" in your Gemfile
|
64
|
+
require "cel"
|
65
|
+
|
66
|
+
env = Cel::Environment.new
|
67
|
+
|
68
|
+
env.evaluate("google.protobuf.Duration{seconds: 123}.seconds == 123") #=> true
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
## Supported Rubies
|
73
|
+
|
74
|
+
All Rubies greater or equal to 2.5, and always latest JRuby and Truffleruby.
|
75
|
+
|
76
|
+
## Development
|
77
|
+
|
78
|
+
Clone the repo in your local machine, where you have `ruby` installed. Then you can:
|
79
|
+
|
80
|
+
```bash
|
81
|
+
# install dev dependencies
|
82
|
+
> bundle install
|
83
|
+
# run tests
|
84
|
+
> bundle exec rake test
|
85
|
+
```
|
86
|
+
|
87
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
88
|
+
|
89
|
+
### CEL parser
|
90
|
+
|
91
|
+
The parser is based on the grammar defined in [cel-spec](https://github.com/google/cel-spec/blob/master/doc/langdef.md#syntax), and developed using [racc](https://github.com/ruby/racc), a LALR(1) parser generator, which is part of ruby's standard library.
|
92
|
+
|
93
|
+
Changes in the parser are therefore accomplished by modifying the `parser.ry` file and running:
|
94
|
+
|
95
|
+
```bash
|
96
|
+
> bundle exec racc -o lib/cel/parser.rb lib/cel/parser.ry
|
97
|
+
```
|
98
|
+
|
99
|
+
## Contributing
|
100
|
+
|
101
|
+
Bug reports and pull requests are welcome on GitHub at https://gitlab.com/honeyryderchuck/cel-ruby.
|
@@ -0,0 +1,322 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
|
5
|
+
module Cel
|
6
|
+
LOGICAL_OPERATORS = %w[< <= >= > == != in].freeze
|
7
|
+
MULTI_OPERATORS = %w[* / %].freeze
|
8
|
+
|
9
|
+
class Identifier < SimpleDelegator
|
10
|
+
attr_reader :id
|
11
|
+
|
12
|
+
attr_accessor :type
|
13
|
+
|
14
|
+
def initialize(identifier)
|
15
|
+
@id = identifier
|
16
|
+
@type = TYPES[:any]
|
17
|
+
super(@id)
|
18
|
+
end
|
19
|
+
|
20
|
+
def ==(other)
|
21
|
+
super || other.to_s == @id.to_s
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Message < SimpleDelegator
|
26
|
+
attr_reader :type, :struct
|
27
|
+
|
28
|
+
def initialize(type, struct)
|
29
|
+
@struct = Struct.new(*struct.keys.map(&:to_sym)).new(*struct.values)
|
30
|
+
@type = type.is_a?(Type) ? type : MapType.new(struct.map do |k, v|
|
31
|
+
[Literal.to_cel_type(k), Literal.to_cel_type(v)]
|
32
|
+
end.to_h)
|
33
|
+
super(@struct)
|
34
|
+
end
|
35
|
+
|
36
|
+
def field?(key)
|
37
|
+
!@type.get(key).nil?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Invoke
|
42
|
+
attr_reader :var, :func, :args
|
43
|
+
|
44
|
+
def initialize(func:, var: nil, args: nil)
|
45
|
+
@var = var
|
46
|
+
@func = func.to_sym
|
47
|
+
@args = args
|
48
|
+
end
|
49
|
+
|
50
|
+
def ==(other)
|
51
|
+
super || (
|
52
|
+
other.respond_to?(:to_ary) &&
|
53
|
+
[@var, @func, @args].compact == other
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_s
|
58
|
+
if var
|
59
|
+
if func == :[]
|
60
|
+
"#{var}[#{args}]"
|
61
|
+
else
|
62
|
+
"#{var}.#{func}#{"(#{args.map(&:to_s).join(", ")})" if args}"
|
63
|
+
end
|
64
|
+
else
|
65
|
+
"#{func}#{"(#{args.map(&:to_s).join(", ")})" if args}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class Literal < SimpleDelegator
|
71
|
+
attr_reader :type, :value
|
72
|
+
|
73
|
+
def initialize(type, value)
|
74
|
+
@type = type.is_a?(Type) ? type : TYPES[type]
|
75
|
+
@value = value
|
76
|
+
super(value)
|
77
|
+
check
|
78
|
+
end
|
79
|
+
|
80
|
+
def ==(other)
|
81
|
+
@value == other || super
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.to_cel_type(val)
|
85
|
+
case val
|
86
|
+
when Literal, Identifier
|
87
|
+
val
|
88
|
+
# TODO: should support byte streams?
|
89
|
+
when ::String
|
90
|
+
String.new(val)
|
91
|
+
when ::Symbol
|
92
|
+
Identifier.new(val)
|
93
|
+
when ::Integer
|
94
|
+
Number.new(:int, val)
|
95
|
+
when ::Float, ::BigDecimal
|
96
|
+
Number.new(:double, val)
|
97
|
+
when ::Hash
|
98
|
+
Map.new(val)
|
99
|
+
when ::Array
|
100
|
+
List.new(val)
|
101
|
+
when true, false
|
102
|
+
Bool.new(val)
|
103
|
+
when nil
|
104
|
+
Null.new
|
105
|
+
else
|
106
|
+
raise BindingError, "can't convert #{val} to CEL type"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
def check; end
|
113
|
+
end
|
114
|
+
|
115
|
+
class Number < Literal
|
116
|
+
[:+, :-, *MULTI_OPERATORS].each do |op|
|
117
|
+
class_eval(<<-OUT, __FILE__, __LINE__ + 1)
|
118
|
+
def #{op}(other)
|
119
|
+
Number.new(@type, super)
|
120
|
+
end
|
121
|
+
OUT
|
122
|
+
end
|
123
|
+
|
124
|
+
LOGICAL_OPERATORS.each do |op|
|
125
|
+
class_eval(<<-OUT, __FILE__, __LINE__ + 1)
|
126
|
+
def #{op}(other)
|
127
|
+
Bool.new(super)
|
128
|
+
end
|
129
|
+
OUT
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
class Bool < Literal
|
134
|
+
def initialize(value)
|
135
|
+
super(:bool, value)
|
136
|
+
end
|
137
|
+
|
138
|
+
LOGICAL_OPERATORS.each do |op|
|
139
|
+
class_eval(<<-OUT, __FILE__, __LINE__ + 1)
|
140
|
+
def #{op}(other)
|
141
|
+
Bool.new(super)
|
142
|
+
end
|
143
|
+
OUT
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
class Null < Literal
|
148
|
+
def initialize
|
149
|
+
super(:null_type, nil)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
class String < Literal
|
154
|
+
def initialize(value)
|
155
|
+
super(:string, value)
|
156
|
+
end
|
157
|
+
|
158
|
+
# CEL string functions
|
159
|
+
|
160
|
+
def contains(string)
|
161
|
+
Bool.new(@value.include?(string))
|
162
|
+
end
|
163
|
+
|
164
|
+
def endsWith(string)
|
165
|
+
Bool.new(@value.end_with?(string))
|
166
|
+
end
|
167
|
+
|
168
|
+
def startsWith(string)
|
169
|
+
Bool.new(@value.start_with?(string))
|
170
|
+
end
|
171
|
+
|
172
|
+
def matches(pattern)
|
173
|
+
Macro.matches(self, pattern)
|
174
|
+
end
|
175
|
+
|
176
|
+
LOGICAL_OPERATORS.each do |op|
|
177
|
+
class_eval(<<-OUT, __FILE__, __LINE__ + 1)
|
178
|
+
def #{op}(other)
|
179
|
+
Bool.new(super)
|
180
|
+
end
|
181
|
+
OUT
|
182
|
+
end
|
183
|
+
|
184
|
+
%i[+ -].each do |op|
|
185
|
+
class_eval(<<-OUT, __FILE__, __LINE__ + 1)
|
186
|
+
def #{op}(other)
|
187
|
+
String.new(super)
|
188
|
+
end
|
189
|
+
OUT
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
class Bytes < Literal
|
194
|
+
def initialize(value)
|
195
|
+
super(:bytes, value)
|
196
|
+
end
|
197
|
+
|
198
|
+
def to_ary
|
199
|
+
[self]
|
200
|
+
end
|
201
|
+
|
202
|
+
LOGICAL_OPERATORS.each do |op|
|
203
|
+
class_eval(<<-OUT, __FILE__, __LINE__ + 1)
|
204
|
+
def #{op}(other)
|
205
|
+
Bool.new(super)
|
206
|
+
end
|
207
|
+
OUT
|
208
|
+
end
|
209
|
+
|
210
|
+
%i[+ -].each do |op|
|
211
|
+
class_eval(<<-OUT, __FILE__, __LINE__ + 1)
|
212
|
+
def #{op}(other)
|
213
|
+
String.new(@type, super)
|
214
|
+
end
|
215
|
+
OUT
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
class List < Literal
|
220
|
+
def initialize(value)
|
221
|
+
value = value.map do |v|
|
222
|
+
Literal.to_cel_type(v)
|
223
|
+
end
|
224
|
+
super(ListType.new(value), value)
|
225
|
+
end
|
226
|
+
|
227
|
+
def to_ary
|
228
|
+
[self]
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
class Map < Literal
|
233
|
+
def initialize(value)
|
234
|
+
value = value.map do |k, v|
|
235
|
+
[Literal.to_cel_type(k), Literal.to_cel_type(v)]
|
236
|
+
end.to_h
|
237
|
+
super(MapType.new(value), value)
|
238
|
+
check
|
239
|
+
end
|
240
|
+
|
241
|
+
def ==(other)
|
242
|
+
super || (
|
243
|
+
other.respond_to?(:to_hash) &&
|
244
|
+
@value.zip(other).all? { |(x1, y1), (x2, y2)| x1 == x2 && y1 == y2 }
|
245
|
+
)
|
246
|
+
end
|
247
|
+
|
248
|
+
def to_ary
|
249
|
+
[self]
|
250
|
+
end
|
251
|
+
|
252
|
+
def respond_to_missing?(meth, *args)
|
253
|
+
super || (@value && @value.keys.any? { |k| k.to_s == meth.to_s })
|
254
|
+
end
|
255
|
+
|
256
|
+
def method_missing(meth, *args)
|
257
|
+
return super unless @value
|
258
|
+
|
259
|
+
key = @value.keys.find { |k| k.to_s == meth.to_s } or return super
|
260
|
+
|
261
|
+
@value[key]
|
262
|
+
end
|
263
|
+
|
264
|
+
private
|
265
|
+
|
266
|
+
ALLOWED_TYPES = %i[int uint bool string].map { |typ| TYPES[typ] }.freeze
|
267
|
+
|
268
|
+
# For a map, the entry keys are sub-expressions that must evaluate to values
|
269
|
+
# of an allowed type (int, uint, bool, or string)
|
270
|
+
def check
|
271
|
+
return if @value.each_key.all? { |key| key.is_a?(Identifier) || ALLOWED_TYPES.include?(key.type) }
|
272
|
+
|
273
|
+
raise CheckError, "#{self} is invalid (keys must be of an allowed type (int, uint, bool, or string)"
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
class Group
|
278
|
+
attr_reader :value
|
279
|
+
|
280
|
+
def initialize(value)
|
281
|
+
@value = value
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
class Operation
|
286
|
+
attr_reader :op, :operands
|
287
|
+
|
288
|
+
attr_accessor :type
|
289
|
+
|
290
|
+
def initialize(op, operands)
|
291
|
+
@op = op
|
292
|
+
@operands = operands
|
293
|
+
@type = TYPES[:any]
|
294
|
+
end
|
295
|
+
|
296
|
+
def ==(other)
|
297
|
+
if other.is_a?(Array)
|
298
|
+
other.size == @operands.size + 1 &&
|
299
|
+
other.first == @op &&
|
300
|
+
other.slice(1..-1).zip(@operands).all? { |x1, x2| x1 == x2 }
|
301
|
+
else
|
302
|
+
super
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
def to_s
|
307
|
+
return "#{@op}#{@operands.first}" if @operands.size == 1
|
308
|
+
|
309
|
+
@operands.join(" #{@op} ")
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
class Condition
|
314
|
+
attr_reader :if, :then, :else
|
315
|
+
|
316
|
+
def initialize(if_, then_, else_)
|
317
|
+
@if = if_
|
318
|
+
@then = then_
|
319
|
+
@else = else_
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|