Class: Cel::Literal

Inherits:
SimpleDelegator
  • Object
show all
Includes:
CelMethods
Defined in:
lib/cel/ast/elements/literal.rb

Direct Known Subclasses

Bool, Bytes, Duration, List, Map, Null, Number, String, Timestamp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CelMethods

included

Constructor Details

#initialize(type, value) ⇒ Literal

Returns a new instance of Literal.



48
49
50
51
52
53
# File 'lib/cel/ast/elements/literal.rb', line 48

def initialize(type, value)
  @type = type.is_a?(Type) ? type : TYPES[type]
  @value = value
  super(value)
  check
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/cel/ast/elements/literal.rb', line 7

def type
  @type
end

#valueObject (readonly) Also known as: to_ruby_type

Returns the value of attribute value.



7
8
9
# File 'lib/cel/ast/elements/literal.rb', line 7

def value
  @value
end

Class Method Details

.to_cel_type(val) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cel/ast/elements/literal.rb', line 10

def to_cel_type(val)
  if val.is_a?(Protobuf.base_class) ||
     val.is_a?(Struct) # no-protobuf mode
    val = Protobuf.try_convert_from_wrapper(val)

    return val if val.is_a?(Protobuf.base_class) # still
  end

  case val
  when Literal, Identifier, Invoke, Operation, Condition, Group, # already cel
      Protobuf.enum_class # already a usable class a la protobuf
    val
  when Protobuf.map_class
    Map.new(val.to_h)
  when ::String
    String.new(val)
  when ::Symbol
    Identifier.new(val)
  when ::Integer
    Number.new(:int, val)
  when ::Float, ::BigDecimal
    Number.new(:double, val)
  when ::Hash
    Map.new(val)
  when ::Array
    List.new(val)
  when true, false
    Bool.cast(val)
  when nil
    Null::INSTANCE
  when Time
    Timestamp.new(val)
  else
    raise BindingError, "can't convert #{val} to CEL type"
  end
end