Class: Cel::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/cel/ast/types.rb

Direct Known Subclasses

ListType, MapType

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Type

Returns a new instance of Type.



5
6
7
# File 'lib/cel/ast/types.rb', line 5

def initialize(type)
  @type = type
end

Instance Method Details

#==(other) ⇒ Object



9
10
11
# File 'lib/cel/ast/types.rb', line 9

def ==(other)
  other == @type || super
end

#cast(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cel/ast/types.rb', line 23

def cast(value)
  value = value.value if value.is_a?(Literal)

  case @type
  when :int
    Number.new(:int, Integer(value))
  when :uint
    Number.new(:uint, Integer(value).abs)
  when :double
    Number.new(:double, Float(value))
  when :string
    String.new(String(value))
  when :bytes
    Bytes.new(value.bytes)
  when :bool
    Bool.new(value)
  when :timestamp
    Timestamp.new(value)
  when :duration
    Duration.new(value)
  when :any
    value
  else
    raise Error, "unsupported cast operation to #{@type}"
  end
end

#to_strObject Also known as: to_s



13
14
15
# File 'lib/cel/ast/types.rb', line 13

def to_str
  @type.to_s
end

#typeObject



19
20
21
# File 'lib/cel/ast/types.rb', line 19

def type
  TYPES[:type]
end