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.



52
53
54
55
56
57
58
# File 'lib/cel/ast/elements.rb', line 52

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.



43
44
45
# File 'lib/cel/ast/elements.rb', line 43

def struct
  @struct
end

#typeObject (readonly)

Returns the value of attribute type.



43
44
45
# File 'lib/cel/ast/elements.rb', line 43

def type
  @type
end

Class Method Details

.convert_from_type(type, value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cel/ast/elements.rb', line 64

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



45
46
47
48
49
50
# File 'lib/cel/ast/elements.rb', line 45

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)


60
61
62
# File 'lib/cel/ast/elements.rb', line 60

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