Class: Cel::Checker

Inherits:
Object
  • Object
show all
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], # TODO: enum
  uint: i[int uint double string],
  string: i[int uint double bytes bool bytes string timestamp duration],
  double: i[double int uint string],
  bytes: i[bytes string],
  bool: i[bool string],
  duration: i[string duration],
  timestamp: i[int string timestamp],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#declarationsObject (readonly)

Returns the value of attribute declarations.



24
25
26
# File 'lib/cel/checker.rb', line 24

def declarations
  @declarations
end

#environmentObject (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
    check_message(ast)
  when Identifier
    check_identifier(ast)
  when Condition
    check_condition(ast)
  end
end

#check_arity(func, args, arity, op = :===) ⇒ Object

Raises:



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

Raises:



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

Raises:



84
85
86
# File 'lib/cel/checker.rb', line 84

def unsupported_operation(op)
  raise CheckError, "unsupported operation (#{op})"
end

#unsupported_type(op) ⇒ Object

Raises:



80
81
82
# File 'lib/cel/checker.rb', line 80

def unsupported_type(op)
  raise CheckError, "no matching overload: #{op}"
end