Class: Protod::Proto::Message

Inherits:
Part
  • Object
show all
Includes:
FieldCollectable, FieldNumeringable, Findable, InterpreterBindable
Defined in:
lib/protod/proto/message.rb

Instance Method Summary collapse

Methods included from InterpreterBindable

#bind

Methods included from FieldNumeringable

#numbering_fields_with

Methods included from FieldCollectable

#collect_fields

Methods inherited from Part

#ancestor_as, #ident=, #push, #root

Instance Method Details

#freezeObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/protod/proto/message.rb', line 56

def freeze
  messages.each { _1.depth = depth + 1 }
  fields.each { _1.depth = depth + 1 }

  messages.each.with_index(1) { |m, i| m.index = i }
  numbering_fields_with(1)

  @message_map = self.class.findable_keys_for(:message).index_with { |k| messages.index_by(&k.to_sym) }
  @field_map   = self.class.findable_keys_for(:field).index_with { |k| fields.index_by(&k.to_sym) }

  super
end

#full_identObject



46
47
48
# File 'lib/protod/proto/message.rb', line 46

def full_ident
  [parent&.full_ident, ident].compact.join('.').presence
end

#has?(part, in_the:) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/protod/proto/message.rb', line 22

def has?(part, in_the:)
  case in_the
  when :fields
    idents = fields.flat_map do |f|
      case f
      when Protod::Proto::Field
        [f.ident]
      when Protod::Proto::Oneof
        [f.ident, *f.fields.map(&:ident)]
      else
        raise ArgumentError, "Unacceptable field : #{f.ident} of #{f.class.name}"
      end
    end

    part.ident.in?(idents)
  else
    super
  end
end

#pb_constObject

Raises:

  • (NotImplementedError)


50
51
52
53
54
# File 'lib/protod/proto/message.rb', line 50

def pb_const
  raise NotImplementedError, "Can't call pb_const for #{ident} : not set parent yet" unless parent

  Google::Protobuf::DescriptorPool.generated_pool.lookup(full_ident).msgclass
end

#ruby_identObject



42
43
44
# File 'lib/protod/proto/message.rb', line 42

def ruby_ident
  ident.const_name
end

#to_protoObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/protod/proto/message.rb', line 69

def to_proto
  message_part = messages.map { _1.to_proto }.join("\n\n").presence

  field_part = fields.filter_map do |f|
    case f
    when Protod::Proto::Field
      next if f.void?

      f.to_proto
    when Protod::Proto::Oneof
      f.to_proto
    else
      raise NotImplementedError, "Sorry, this is bug forgetting to implement for #{f.class.name}"
    end
  end.join("\n").presence

  body = [message_part, field_part].compact.join("\n\n").presence
  body = "\n#{body}\n" if body

  [
    format_proto("message %s {%s", ident, body),
    format_proto("}")
  ].join
end