Class: ProtoPlugin::FieldDescriptor
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- ProtoPlugin::FieldDescriptor
- 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
- #descriptor ⇒ Google::Protobuf::FieldDescriptorProto readonly
-
#message ⇒ MessageDescriptor
readonly
The message descriptor this field was defined within.
Instance Method Summary collapse
-
#default_value ⇒ String?
The explicit default value declared for the field (proto2 only), as a string.
-
#enum? ⇒ Boolean
Returns true if the field is an enum type.
-
#file ⇒ FileDescriptor
The file descriptor this field belongs to.
-
#group? ⇒ Boolean
Returns true if the field is a group type.
-
#initialize(descriptor, message, context) ⇒ FieldDescriptor
constructor
A new instance of FieldDescriptor.
-
#json_name ⇒ String
The JSON name of the field, as computed by
protoc(the lowerCamelCase form unless overridden with thejson_nameoption). -
#key ⇒ FieldDescriptor?
The map key field, for a map field.
-
#map? ⇒ Boolean
Returns true if the field is a
map<K, V>field. -
#message? ⇒ Boolean
Returns true if the field is a message type.
-
#oneof ⇒ OneofDescriptor?
The oneof this field is a member of, if any.
-
#oneof? ⇒ Boolean
Returns true if the field is a member of a oneof.
-
#optional? ⇒ Boolean
Returns true if the field has the
optionallabel. -
#proto3_optional? ⇒ Boolean
Returns true if the field was declared with proto3 explicit presence, i.e.
-
#repeated? ⇒ Boolean
Returns true if the field has the
repeatedlabel. -
#required? ⇒ Boolean
Returns true if the field has the
requiredlabel (proto2 only). -
#scalar? ⇒ Boolean
Returns true if the field is a scalar type (i.e. not a message, enum, group, or map).
-
#type ⇒ Symbol
The field’s type as a plain symbol.
-
#type_descriptor ⇒ MessageDescriptor, ...
Resolves the message or enum descriptor referenced by this field.
-
#value ⇒ FieldDescriptor?
The map value field, for a map field.
Methods included from Commentable
#comments, #leading_comments, #source_location, #trailing_comments
Constructor Details
#initialize(descriptor, message, context) ⇒ FieldDescriptor
Returns a new instance of FieldDescriptor.
27 28 29 30 31 32 |
# File 'lib/proto_plugin/field_descriptor.rb', line 27 def initialize(descriptor, , context) super(descriptor) @descriptor = descriptor @message = @context = context end |
Instance Attribute Details
#descriptor ⇒ Google::Protobuf::FieldDescriptorProto (readonly)
17 18 19 |
# File 'lib/proto_plugin/field_descriptor.rb', line 17 def descriptor @descriptor end |
#message ⇒ MessageDescriptor (readonly)
The message descriptor this field was defined within.
22 23 24 |
# File 'lib/proto_plugin/field_descriptor.rb', line 22 def @message end |
Instance Method Details
#default_value ⇒ String?
The explicit default value declared for the field (proto2 only), as a string. Proto3 fields do not carry defaults.
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.
95 96 97 |
# File 'lib/proto_plugin/field_descriptor.rb', line 95 def enum? type == :enum end |
#file ⇒ FileDescriptor
The file descriptor this field belongs to.
37 38 39 |
# File 'lib/proto_plugin/field_descriptor.rb', line 37 def file .file end |
#group? ⇒ Boolean
Returns true if the field is a group type.
102 103 104 |
# File 'lib/proto_plugin/field_descriptor.rb', line 102 def group? type == :group end |
#json_name ⇒ String
The JSON name of the field, as computed by protoc (the lowerCamelCase form unless overridden with the json_name option).
127 128 129 |
# File 'lib/proto_plugin/field_descriptor.rb', line 127 def json_name descriptor.json_name end |
#key ⇒ FieldDescriptor?
The map key field, for a map field.
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.
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.
88 89 90 |
# File 'lib/proto_plugin/field_descriptor.rb', line 88 def type == :message && !map? end |
#oneof ⇒ OneofDescriptor?
The oneof this field is a member of, if any.
196 197 198 199 200 |
# File 'lib/proto_plugin/field_descriptor.rb', line 196 def oneof return unless oneof? .oneofs[descriptor.oneof_index] end |
#oneof? ⇒ Boolean
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.
188 189 190 |
# File 'lib/proto_plugin/field_descriptor.rb', line 188 def oneof? descriptor.has_oneof_index? && !proto3_optional? end |
#optional? ⇒ Boolean
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.
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.
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.
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).
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).
145 146 147 |
# File 'lib/proto_plugin/field_descriptor.rb', line 145 def scalar? !map? && ! && !enum? && !group? end |
#type ⇒ Symbol
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.
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_descriptor ⇒ MessageDescriptor, ...
51 52 53 54 55 |
# File 'lib/proto_plugin/field_descriptor.rb', line 51 def type_descriptor return unless || enum? || group? @context.type_by_proto_name(type_name) end |
#value ⇒ FieldDescriptor?
The map value field, for a map field.
80 81 82 |
# File 'lib/proto_plugin/field_descriptor.rb', line 80 def value map_entry&.fields&.find { |f| f.number == 2 } end |