Class: Cel::Message

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, struct) ⇒ Message

Returns a new instance of Message.



41
42
43
44
45
46
47
# File 'lib/cel/ast/elements.rb', line 41

def initialize(type, struct)
  @struct = Struct.new(*struct.keys.map(&:to_sym)).new(*struct.values)
  @type = type.is_a?(Type) ? type : MapType.new(struct.to_h do |k, v|
                                                  [Literal.to_cel_type(k), Literal.to_cel_type(v)]
                                                end)
  super(@struct)
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



32
33
34
# File 'lib/cel/ast/elements.rb', line 32

def struct
  @struct
end

#typeObject (readonly)

Returns the value of attribute type.



32
33
34
# File 'lib/cel/ast/elements.rb', line 32

def type
  @type
end

Class Method Details

.convert_from_type(type, value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cel/ast/elements.rb', line 53

def self.convert_from_type(type, value)
  case type
  when Invoke, Identifier
    spread_type = type.to_s
    Protobuf.convert_from_type(spread_type, value)
  when Type
    [type, value]
  else
    [
      MapType.new(struct.to_h do |k, v|
        [Literal.to_cel_type(k), Literal.to_cel_type(v)]
      end),
      Struct.new(*struct.keys.map(&:to_sym)).new(*struct.values),
    ]
  end
end

.new(type, struct) ⇒ Object



34
35
36
37
38
39
# File 'lib/cel/ast/elements.rb', line 34

def self.new(type, struct)
  value = convert_from_type(type, struct)
  return value if value.is_a?(Null) || value != struct

  super
end

Instance Method Details

#field?(key) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/cel/ast/elements.rb', line 49

def field?(key)
  !@type.get(key).nil?
end