cel 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +85 -0
- data/README.md +33 -6
- data/lib/cel/ast/elements/protobuf.rb +127 -0
- data/lib/cel/ast/elements.rb +258 -4
- data/lib/cel/ast/types.rb +49 -2
- data/lib/cel/checker.rb +116 -38
- data/lib/cel/context.rb +8 -3
- data/lib/cel/encoder.rb +102 -0
- data/lib/cel/environment.rb +19 -6
- data/lib/cel/parser.rb +28 -12
- data/lib/cel/program.rb +20 -6
- data/lib/cel/version.rb +1 -1
- data/lib/cel.rb +1 -6
- metadata +19 -4
- data/lib/cel/protobuf.rb +0 -175
data/lib/cel.rb
CHANGED
@@ -7,16 +7,11 @@ require "cel/ast/types"
|
|
7
7
|
require "cel/parser"
|
8
8
|
require "cel/macro"
|
9
9
|
require "cel/context"
|
10
|
+
require "cel/encoder" if RUBY_VERSION >= "3.1.0"
|
10
11
|
require "cel/checker"
|
11
12
|
require "cel/program"
|
12
13
|
require "cel/environment"
|
13
14
|
|
14
|
-
begin
|
15
|
-
require "google/protobuf/well_known_types"
|
16
|
-
require "cel/protobuf"
|
17
|
-
rescue LoadError # rubocop:disable Lint/SuppressedException
|
18
|
-
end
|
19
|
-
|
20
15
|
module Cel
|
21
16
|
def self.to_numeric(anything)
|
22
17
|
num = BigDecimal(anything.to_s)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Cardoso
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tzinfo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
description: Pure Ruby implementation of Google Common Expression Language, https://opensource.google/projects/cel.
|
28
42
|
email:
|
29
43
|
- cardoso_tiago@hotmail.com
|
@@ -39,15 +53,16 @@ files:
|
|
39
53
|
- README.md
|
40
54
|
- lib/cel.rb
|
41
55
|
- lib/cel/ast/elements.rb
|
56
|
+
- lib/cel/ast/elements/protobuf.rb
|
42
57
|
- lib/cel/ast/types.rb
|
43
58
|
- lib/cel/checker.rb
|
44
59
|
- lib/cel/context.rb
|
60
|
+
- lib/cel/encoder.rb
|
45
61
|
- lib/cel/environment.rb
|
46
62
|
- lib/cel/errors.rb
|
47
63
|
- lib/cel/macro.rb
|
48
64
|
- lib/cel/parser.rb
|
49
65
|
- lib/cel/program.rb
|
50
|
-
- lib/cel/protobuf.rb
|
51
66
|
- lib/cel/version.rb
|
52
67
|
homepage:
|
53
68
|
licenses:
|
@@ -73,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
88
|
- !ruby/object:Gem::Version
|
74
89
|
version: '0'
|
75
90
|
requirements: []
|
76
|
-
rubygems_version: 3.
|
91
|
+
rubygems_version: 3.4.10
|
77
92
|
signing_key:
|
78
93
|
specification_version: 4
|
79
94
|
summary: Pure Ruby implementation of Google Common Expression Language, https://opensource.google/projects/cel.
|
data/lib/cel/protobuf.rb
DELETED
@@ -1,175 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "time"
|
4
|
-
|
5
|
-
module Cel
|
6
|
-
module Protobuf
|
7
|
-
module CheckExtensions
|
8
|
-
def self.included(klass)
|
9
|
-
super
|
10
|
-
klass.alias_method :check_standard_func_without_protobuf, :check_standard_func
|
11
|
-
klass.alias_method :check_standard_func, :check_standard_func_with_protobuf
|
12
|
-
|
13
|
-
klass.alias_method :check_invoke_without_protobuf, :check_invoke
|
14
|
-
klass.alias_method :check_invoke, :check_invoke_with_protobuf
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def check_invoke_with_protobuf(funcall)
|
20
|
-
var = funcall.var
|
21
|
-
|
22
|
-
return check_standard_func(funcall) unless var
|
23
|
-
|
24
|
-
var_type = case var
|
25
|
-
when Identifier
|
26
|
-
check_identifier(var)
|
27
|
-
when Invoke
|
28
|
-
check_invoke(var)
|
29
|
-
else
|
30
|
-
var.type
|
31
|
-
end
|
32
|
-
|
33
|
-
return check_invoke_without_protobuf(funcall, var_type) if var_type.is_a?(Cel::Type)
|
34
|
-
return check_invoke_without_protobuf(funcall, var_type) unless var_type < Google::Protobuf::MessageExts
|
35
|
-
|
36
|
-
func = funcall.func
|
37
|
-
|
38
|
-
attribute = var_type.descriptor.lookup(func.to_s)
|
39
|
-
|
40
|
-
raise NoSuchFieldError.new(var, func) unless attribute
|
41
|
-
|
42
|
-
# TODO: return super to any, or raise error?
|
43
|
-
|
44
|
-
type = attribute.type
|
45
|
-
|
46
|
-
# https://github.com/google/cel-spec/blob/master/doc/langdef.md#protocol-buffer-data-conversion
|
47
|
-
case type
|
48
|
-
when :int32, :int64, :sint32, :sint64, :sfixed32, :sfixed64
|
49
|
-
TYPES[:int]
|
50
|
-
when :uint32, :uint64, :fixed32, :fixed64
|
51
|
-
TYPES[:uint]
|
52
|
-
when :float, :double
|
53
|
-
TYPES[:double]
|
54
|
-
when :bool, :string, :bytes
|
55
|
-
TYPES[type]
|
56
|
-
when :repeated
|
57
|
-
TYPES[:list]
|
58
|
-
when :map
|
59
|
-
TYPES[:map]
|
60
|
-
when :oneof
|
61
|
-
TYPES[:any]
|
62
|
-
else
|
63
|
-
type
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def check_standard_func_with_protobuf(funcall)
|
68
|
-
func = funcall.func
|
69
|
-
args = funcall.args
|
70
|
-
|
71
|
-
case func
|
72
|
-
when :duration
|
73
|
-
check_arity(func, args, 1)
|
74
|
-
unsupported_type(funcall) unless args.first.is_a?(String)
|
75
|
-
|
76
|
-
return Google::Protobuf::Duration
|
77
|
-
when :timestamp
|
78
|
-
check_arity(func, args, 1)
|
79
|
-
unsupported_type(funcall) unless args.first.is_a?(String)
|
80
|
-
|
81
|
-
return Google::Protobuf::Timestamp
|
82
|
-
end
|
83
|
-
|
84
|
-
check_standard_func_without_protobuf(funcall)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
module EvaluateExtensions
|
89
|
-
def self.included(klass)
|
90
|
-
super
|
91
|
-
klass.alias_method :evaluate_standard_func_without_protobuf, :evaluate_standard_func
|
92
|
-
klass.alias_method :evaluate_standard_func, :evaluate_standard_func_with_protobuf
|
93
|
-
|
94
|
-
klass.alias_method :evaluate_invoke_without_protobuf, :evaluate_invoke
|
95
|
-
klass.alias_method :evaluate_invoke, :evaluate_invoke_with_protobuf
|
96
|
-
end
|
97
|
-
|
98
|
-
private
|
99
|
-
|
100
|
-
def evaluate_invoke_with_protobuf(invoke)
|
101
|
-
var = invoke.var
|
102
|
-
|
103
|
-
return evaluate_standard_func(invoke) unless var
|
104
|
-
|
105
|
-
var = case var
|
106
|
-
when Identifier
|
107
|
-
evaluate_identifier(var)
|
108
|
-
when Invoke
|
109
|
-
evaluate_invoke(var)
|
110
|
-
else
|
111
|
-
var
|
112
|
-
end
|
113
|
-
|
114
|
-
return evaluate_invoke_without_protobuf(invoke, var) unless var.is_a?(Google::Protobuf::MessageExts)
|
115
|
-
|
116
|
-
func = invoke.func
|
117
|
-
|
118
|
-
var.public_send(func)
|
119
|
-
end
|
120
|
-
|
121
|
-
def evaluate_standard_func_with_protobuf(funcall)
|
122
|
-
func = funcall.func
|
123
|
-
args = funcall.args
|
124
|
-
|
125
|
-
case func
|
126
|
-
when :duration
|
127
|
-
seconds = 0
|
128
|
-
nanos = 0
|
129
|
-
args.first.value.scan(/([0-9]*(?:\.[0-9]*)?)([a-z]+)/) do |duration, units|
|
130
|
-
case units
|
131
|
-
when "h"
|
132
|
-
seconds += Cel.to_numeric(duration) * 60 * 60
|
133
|
-
when "m"
|
134
|
-
seconds += Cel.to_numeric(duration) * 60
|
135
|
-
when "s"
|
136
|
-
seconds += Cel.to_numeric(duration)
|
137
|
-
when "ms"
|
138
|
-
nanos += Cel.to_numeric(duration) * 1000 * 1000
|
139
|
-
when "us"
|
140
|
-
nanos += Cel.to_numeric(duration) * 1000
|
141
|
-
when "ns"
|
142
|
-
nanos += Cel.to_numeric(duration)
|
143
|
-
else
|
144
|
-
raise EvaluateError, "#{units} is unsupported"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
return Google::Protobuf::Duration.new(seconds: seconds, nanos: nanos)
|
148
|
-
when :timestamp
|
149
|
-
time = Time.parse(args.first)
|
150
|
-
return Google::Protobuf::Timestamp.from_time(time)
|
151
|
-
end
|
152
|
-
|
153
|
-
evaluate_standard_func_without_protobuf(funcall)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
module ContextExtensions
|
158
|
-
def self.included(klass)
|
159
|
-
super
|
160
|
-
klass.alias_method :to_cel_type_without_protobuf, :to_cel_type
|
161
|
-
klass.alias_method :to_cel_type, :to_cel_type_with_protobuf
|
162
|
-
end
|
163
|
-
|
164
|
-
def to_cel_type_with_protobuf(v)
|
165
|
-
return v if v.is_a?(Google::Protobuf::MessageExts)
|
166
|
-
|
167
|
-
to_cel_type_without_protobuf(v)
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
Checker.include(Protobuf::CheckExtensions)
|
173
|
-
Program.include(Protobuf::EvaluateExtensions)
|
174
|
-
Context.include(Protobuf::ContextExtensions)
|
175
|
-
end
|