Class: ProtoPlugin::FieldDescriptor

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

Overview

A wrapper class around Google::Protobuf::FieldDescriptorProto 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, message, context) ⇒ FieldDescriptor

Returns a new instance of FieldDescriptor.

Parameters:

  • descriptor (Google::Protobuf::FieldDescriptorProto)
  • message (MessageDescriptor)

    The message this field was defined within.

  • context (Context)


27
28
29
30
31
32
# File 'lib/proto_plugin/field_descriptor.rb', line 27

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

Instance Attribute Details

#descriptorGoogle::Protobuf::FieldDescriptorProto (readonly)

Returns:

  • (Google::Protobuf::FieldDescriptorProto)


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

def descriptor
  @descriptor
end

#messageMessageDescriptor (readonly)

The message descriptor this field was defined within.

Returns:



22
23
24
# File 'lib/proto_plugin/field_descriptor.rb', line 22

def message
  @message
end

Instance Method Details

#default_valueString?

The explicit default value declared for the field (proto2 only), as a string. Proto3 fields do not carry defaults.

Returns:

  • (String)

    the declared default

  • (nil)

    if no default was declared



136
137
138
139
# File 'lib/proto_plugin/field_descriptor.rb', line 136

def default_value
  value = descriptor.default_value
  value unless value.nil? || value.empty?
end

#enum?Boolean

Returns true if the field is an enum type.

Returns:

  • (Boolean)


95
96
97
# File 'lib/proto_plugin/field_descriptor.rb', line 95

def enum?
  type == :enum
end

#fileFileDescriptor

The file descriptor this field belongs to.

Returns:



37
38
39
# File 'lib/proto_plugin/field_descriptor.rb', line 37

def file
  message.file
end

#group?Boolean

Returns true if the field is a group type.

Returns:

  • (Boolean)


102
103
104
# File 'lib/proto_plugin/field_descriptor.rb', line 102

def group?
  type == :group
end

#json_nameString

The JSON name of the field, as computed by protoc (the lowerCamelCase form unless overridden with the json_name option).

Returns:

  • (String)


127
128
129
# File 'lib/proto_plugin/field_descriptor.rb', line 127

def json_name
  descriptor.json_name
end

#keyFieldDescriptor?

The map key field, for a map field.

Returns:

  • (FieldDescriptor)

    the synthetic entry’s key field (number 1)

  • (nil)

    if the field is not a map



72
73
74
# File 'lib/proto_plugin/field_descriptor.rb', line 72

def key
  map_entry&.fields&.find { |f| f.number == 1 }
end

#map?Boolean

Returns true if the field is a map<K, V> field.

A map is represented on the wire as a repeated message of synthetic entries. This detects that representation so callers can treat maps distinctly from repeated message fields.

Returns:

  • (Boolean)


64
65
66
# File 'lib/proto_plugin/field_descriptor.rb', line 64

def map?
  !map_entry.nil?
end

#message?Boolean

Returns true if the field is a message type. Map fields are excluded; use #map? to detect those.

Returns:

  • (Boolean)


88
89
90
# File 'lib/proto_plugin/field_descriptor.rb', line 88

def message?
  type == :message && !map?
end

#oneofOneofDescriptor?

The oneof this field is a member of, if any.

Returns:

  • (OneofDescriptor)

    if the field is a member of a oneof

  • (nil)

    otherwise



196
197
198
199
200
# File 'lib/proto_plugin/field_descriptor.rb', line 196

def oneof
  return unless oneof?

  message.oneofs[descriptor.oneof_index]
end

#oneof?Boolean

Note:

Fields declared with the proto3 optional keyword are backed by a synthetic oneof. Those are not considered oneof members here.

Returns true if the field is a member of a oneof.

Returns:

  • (Boolean)


188
189
190
# File 'lib/proto_plugin/field_descriptor.rb', line 188

def oneof?
  descriptor.has_oneof_index? && !proto3_optional?
end

#optional?Boolean

Note:

In proto3 all singular fields carry the optional label internally. Use #proto3_optional? to detect fields with explicit presence tracking.

Returns true if the field has the optional label.

Returns:

  • (Boolean)


170
171
172
# File 'lib/proto_plugin/field_descriptor.rb', line 170

def optional?
  label == :LABEL_OPTIONAL
end

#proto3_optional?Boolean

Returns true if the field was declared with proto3 explicit presence, i.e. an optional keyword in a proto3 file.

Returns:

  • (Boolean)


178
179
180
# File 'lib/proto_plugin/field_descriptor.rb', line 178

def proto3_optional?
  descriptor.proto3_optional
end

#repeated?Boolean

Returns true if the field has the repeated label. Map fields are excluded; use #map? to detect those.

Returns:

  • (Boolean)


153
154
155
# File 'lib/proto_plugin/field_descriptor.rb', line 153

def repeated?
  repeated_label? && !map?
end

#required?Boolean

Returns true if the field has the required label (proto2 only).

Returns:

  • (Boolean)


160
161
162
# File 'lib/proto_plugin/field_descriptor.rb', line 160

def required?
  label == :LABEL_REQUIRED
end

#scalar?Boolean

Returns true if the field is a scalar type (i.e. not a message, enum, group, or map).

Returns:

  • (Boolean)


145
146
147
# File 'lib/proto_plugin/field_descriptor.rb', line 145

def scalar?
  !map? && !message? && !enum? && !group?
end

#typeSymbol

The field’s type as a plain symbol.

Normalizes the underlying type enum by removing its TYPE_ prefix and downcasing, e.g. :TYPE_INT32 becomes :int32 and :TYPE_MESSAGE becomes :message. For :message and :enum fields, use #type_descriptor (or #key/#value for maps) to resolve the referenced type.

Examples:

field.type   #=> :int32
field.type   #=> :message

Returns:

  • (Symbol)


119
120
121
# File 'lib/proto_plugin/field_descriptor.rb', line 119

def type
  descriptor.type.to_s.delete_prefix("TYPE_").downcase.to_sym
end

#type_descriptorMessageDescriptor, ...

Resolves the message or enum descriptor referenced by this field.

Only message, enum, and group fields reference another type. For scalar and map fields (or when the referenced type was not included in the request), nil is returned. For a map field, inspect #key and #value instead.

Returns:

  • (MessageDescriptor)

    if the field is a message or group type

  • (EnumDescriptor)

    if the field is an enum type

  • (nil)

    if the field is a scalar or map type, or the type was not found



51
52
53
54
55
# File 'lib/proto_plugin/field_descriptor.rb', line 51

def type_descriptor
  return unless message? || enum? || group?

  @context.type_by_proto_name(type_name)
end

#valueFieldDescriptor?

The map value field, for a map field.

Returns:

  • (FieldDescriptor)

    the synthetic entry’s value field (number 2)

  • (nil)

    if the field is not a map



80
81
82
# File 'lib/proto_plugin/field_descriptor.rb', line 80

def value
  map_entry&.fields&.find { |f| f.number == 2 }
end