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 timestamp 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 timestamp].freeze, # TODO: enum uint: %i[int uint double string].freeze, string: %i[int uint double bytes bool bytes string timestamp duration].freeze, double: %i[double int uint string].freeze, bytes: %i[bytes string].freeze, bool: %i[bool string].freeze, duration: %i[string duration].freeze, timestamp: %i[int string timestamp].freeze, }.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
Returns a new instance of Checker.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cel/checker.rb', line 26 def initialize(environment, declarations = environment.declarations) @environment = environment @declarations = declarations @cast_allowed_types = CAST_ALLOWED_TYPES.dup @environment.extensions.each_value do |ext| next unless ext.const_defined?(:CAST_ALLOWED_TYPES) ext.const_get(:CAST_ALLOWED_TYPES).each do |type, allowed_types| @cast_allowed_types[type] ||= [] @cast_allowed_types[type] += allowed_types end end 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cel/checker.rb', line 40 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
77 78 79 80 81 82 83 |
# File 'lib/cel/checker.rb', line 77 def check_arity(func, args, arity, op = :===) return arity.zero? if args.nil? 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
85 86 87 88 89 |
# File 'lib/cel/checker.rb', line 85 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
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/cel/checker.rb', line 63 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
99 100 101 |
# File 'lib/cel/checker.rb', line 99 def merge(declarations) Checker.new(@environment, @declarations ? @declarations.merge(declarations) : declarations) end |
#unsupported_operation(op) ⇒ Object
95 96 97 |
# File 'lib/cel/checker.rb', line 95 def unsupported_operation(op) raise CheckError, "unsupported operation (#{op})" end |
#unsupported_type(op) ⇒ Object
91 92 93 |
# File 'lib/cel/checker.rb', line 91 def unsupported_type(op) raise CheckError, "no matching overload: #{op}" end |