cel 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
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