Class: ProtoPlugin::MessageDescriptor

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Commentable
Defined in:
lib/proto_plugin/message_descriptor.rb

Overview

A wrapper class around Google::Protobuf::DescriptorProto which provides helpers and more idiomatic Ruby access patterns.

Any method not defined directly is delegated to the descriptor the wrapper was initialized with.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commentable

#comments, #leading_comments, #source_location, #trailing_comments

Constructor Details

#initialize(descriptor, parent, context) ⇒ MessageDescriptor

Returns a new instance of MessageDescriptor.

Parameters:

  • descriptor (Google::Protobuf::DescriptorProto)
  • parent (FileDescriptorFileDescriptorProto, MessageDescriptor)

    The file or message descriptor this message was defined within.

  • context (Context)


29
30
31
32
33
34
# File 'lib/proto_plugin/message_descriptor.rb', line 29

def initialize(descriptor, parent, context)
  super(descriptor)
  @descriptor = descriptor
  @parent = parent
  @context = context
end

Instance Attribute Details

#descriptorGoogle::Protobuf::DescriptorProto (readonly)

Returns:

  • (Google::Protobuf::DescriptorProto)


17
18
19
# File 'lib/proto_plugin/message_descriptor.rb', line 17

def descriptor
  @descriptor
end

#parentFileDescriptor, MessageDescriptor (readonly)

The file or message descriptor this message was defined within.

Returns:



23
24
25
# File 'lib/proto_plugin/message_descriptor.rb', line 23

def parent
  @parent
end

Instance Method Details

#enumsArray<EnumDescriptor>

The enums defined as children of this message.



73
74
75
76
77
# File 'lib/proto_plugin/message_descriptor.rb', line 73

def enums
  @enums ||= @descriptor.enum_type.map do |e|
    EnumDescriptor.new(e, self)
  end
end

#fieldsArray<FieldDescriptor>

The fields defined on this message.



49
50
51
52
53
# File 'lib/proto_plugin/message_descriptor.rb', line 49

def fields
  @fields ||= @descriptor.field.map do |f|
    FieldDescriptor.new(f, self, @context)
  end
end

#fileFileDescriptor

The file descriptor this message belongs to.

Returns:



39
40
41
# File 'lib/proto_plugin/message_descriptor.rb', line 39

def file
  parent.file
end

#full_nameString

The full name of the message, including parent namespace.

Examples:

"My::Ruby::Package::MessageName"

Returns:

  • (String)


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/proto_plugin/message_descriptor.rb', line 101

def full_name
  @full_name ||= begin
    prefix = case parent
    when MessageDescriptor
      parent.full_name
    when FileDescriptor
      parent.namespace
    end
    "#{prefix}::#{name}"
  end
end

#messagesArray<MessageDescriptor>

The messages defined as children of this message.

Synthetic map-entry types (generated by protoc to back map<K, V> fields) are excluded, as they are an implementation detail rather than user-declared messages. Use FieldDescriptor#map? to work with maps.



89
90
91
92
93
# File 'lib/proto_plugin/message_descriptor.rb', line 89

def messages
  @nested_messages ||= @descriptor.nested_type.reject { |m| m.options&.map_entry }.map do |m|
    MessageDescriptor.new(m, self, @context)
  end
end

#oneofsArray<OneofDescriptor>

The oneofs defined on this message.



61
62
63
64
65
# File 'lib/proto_plugin/message_descriptor.rb', line 61

def oneofs
  @oneofs ||= @descriptor.oneof_decl.each_with_index.map do |o, i|
    OneofDescriptor.new(o, self, i)
  end
end