Class: Cel::Checker
- Inherits:
-
Object
- Object
- Cel::Checker
- Defined in:
- lib/cel/checker.rb
Constant Summary collapse
- LOGICAL_EXPECTED_TYPES =
i[bool int uint double string bytes duration].freeze
- ADD_EXPECTED_TYPES =
i[int uint double string bytes list duration].freeze
- SUB_EXPECTED_TYPES =
i[int uint double duration].freeze
- MULTIDIV_EXPECTED_TYPES =
i[int uint double].freeze
- REMAINDER_EXPECTED_TYPES =
i[int uint].freeze
- BOOLABLE_OPERATORS =
%w[&& || == != < <= >= >].freeze
- CAST_ALLOWED_TYPES =
{ int: i[int uint double string ], # TODO: enum uint: i[int uint double string], string: i[int uint double bytes bool bytes string duration], double: i[double int uint string], bytes: i[bytes string], bool: i[bool string], duration: i[string duration], timestamp: i[int string ], }.freeze
Instance Attribute Summary collapse
-
#declarations ⇒ Object
readonly
Returns the value of attribute declarations.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
Instance Method Summary collapse
- #check(ast) ⇒ Object (also: #call)
- #check_arity(func, args, arity, op = :===) ⇒ Object
- #check_arity_any(func, args) ⇒ Object
- #find_match_all_types(expected, types) ⇒ Object
-
#initialize(environment, declarations = environment.declarations) ⇒ Checker
constructor
A new instance of Checker.
- #merge(declarations) ⇒ Object
- #unsupported_operation(op) ⇒ Object
- #unsupported_type(op) ⇒ Object
Constructor Details
#initialize(environment, declarations = environment.declarations) ⇒ Checker
26 27 28 29 |
# File 'lib/cel/checker.rb', line 26 def initialize(environment, declarations = environment.declarations) @environment = environment @declarations = declarations end |
Instance Attribute Details
#declarations ⇒ Object (readonly)
Returns the value of attribute declarations.
24 25 26 |
# File 'lib/cel/checker.rb', line 24 def declarations @declarations end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
24 25 26 |
# File 'lib/cel/checker.rb', line 24 def environment @environment end |
Instance Method Details
#check(ast) ⇒ Object Also known as: call
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cel/checker.rb', line 31 def check(ast) return ast if ast.is_a?(Class) && ast < Protobuf.base_class case ast when Group check(ast.value) when Invoke check_invoke(ast) when Operation check_operation(ast) when Literal check_literal(ast) when Message (ast) when Identifier check_identifier(ast) when Condition check_condition(ast) end end |
#check_arity(func, args, arity, op = :===) ⇒ Object
68 69 70 71 72 |
# File 'lib/cel/checker.rb', line 68 def check_arity(func, args, arity, op = :===) return if arity.__send__(op, args.size) raise CheckError, "`#{func}` invoked with wrong number of arguments (should be #{arity})" end |
#check_arity_any(func, args) ⇒ Object
74 75 76 77 78 |
# File 'lib/cel/checker.rb', line 74 def check_arity_any(func, args) return if args.size.positive? raise CheckError, "`#{func}` invoked with no arguments" end |
#find_match_all_types(expected, types) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cel/checker.rb', line 54 def find_match_all_types(expected, types) # at least an expected type must match all values type = expected.find do |expected_type| case types when Array types.all? { |typ| typ == expected_type } else types == expected_type end end type && types.is_a?(Type) ? types : TYPES[type] end |
#merge(declarations) ⇒ Object
88 89 90 |
# File 'lib/cel/checker.rb', line 88 def merge(declarations) Checker.new(@environment, @declarations ? @declarations.merge(declarations) : declarations) end |
#unsupported_operation(op) ⇒ Object
84 85 86 |
# File 'lib/cel/checker.rb', line 84 def unsupported_operation(op) raise CheckError, "unsupported operation (#{op})" end |
#unsupported_type(op) ⇒ Object
80 81 82 |
# File 'lib/cel/checker.rb', line 80 def unsupported_type(op) raise CheckError, "no matching overload: #{op}" end |