Class: Google::Protobuf::FieldDescriptor

Inherits:
Object
  • Object
show all
Extended by:
FFI::DataConverter, Internal::PointerHelper, Internal::TypeSafety
Includes:
Internal::Convert
Defined in:
lib/google/protobuf/ffi/field_descriptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal::PointerHelper

descriptor_from_file_def

Methods included from Internal::TypeSafety

to_native

Methods included from Internal::Convert

#convert_ruby_to_upb, #convert_upb_to_ruby, #map_create_hash, #message_value_deep_copy, #repeated_field_create_array, #scalar_create_hash, #to_h_internal

Instance Attribute Details

#field_defObject (readonly)

Returns the value of attribute field_def.



11
12
13
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 11

def field_def
  @field_def
end

Class Method Details

.from_native(field_def, _ = nil) ⇒ Object

Parameters:

  • field_def (::FFI::Pointer)

    FieldDef pointer to be wrapped

  • _ (Object) (defaults to: nil)

    Unused



35
36
37
38
39
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 35

def from_native(field_def, _ = nil)
  return nil if field_def.nil? or field_def.null?
  file_def = Google::Protobuf::FFI.file_def_by_raw_field_def(field_def)
  descriptor_from_file_def(file_def, field_def)
end

.new(*arguments, &block) ⇒ Object



42
43
44
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 42

def self.new(*arguments, &block)
  raise "Descriptor objects may not be created from Ruby."
end

.to_native(value, _) ⇒ Object

Parameters:

  • value (FieldDescriptor)

    FieldDescriptor to convert to an FFI native type

  • _ (Object)

    Unused



25
26
27
28
29
30
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 25

def to_native(value, _)
  field_def_ptr = value.instance_variable_get(:@field_def)
  warn "Underlying field_def was nil!" if field_def_ptr.nil?
  raise "Underlying field_def was null!" if !field_def_ptr.nil? and field_def_ptr.null?
  field_def_ptr
end

Instance Method Details

#clear(msg) ⇒ Object

Parameters:

  • msg (Google::Protobuf::Message)


160
161
162
163
164
165
166
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 160

def clear(msg)
  if msg.class.descriptor != Google::Protobuf::FFI.get_containing_message_def(self)
    raise TypeError.new "clear method called on wrong message type"
  end
  Google::Protobuf::FFI.clear_message_field msg.instance_variable_get(:@msg), self
  nil
end

#defaultObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 74

def default
  return nil if Google::Protobuf::FFI.is_sub_message(self)
  if Google::Protobuf::FFI.is_repeated(self)
    message_value = Google::Protobuf::FFI::MessageValue.new
  else
    message_value = Google::Protobuf::FFI.get_default(self)
  end
  enum_def = Google::Protobuf::FFI.get_subtype_as_enum(self)
  if enum_def.null?
    convert_upb_to_ruby message_value, c_type
  else
    convert_upb_to_ruby message_value, c_type, enum_def
  end
end

#get(msg) ⇒ Object

Tests if this field has been set on the argument message.

Parameters:

  • msg (Google::Protobuf::Message)

Returns:

  • (Object)

    Value of the field on this message.

Raises:

  • (TypeError)

    If the field is not defined on this message.



110
111
112
113
114
115
116
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 110

def get(msg)
  if msg.class.descriptor == Google::Protobuf::FFI.get_containing_message_def(self)
    msg.send :get_field, self
  else
    raise TypeError.new "get method called on wrong message type"
  end
end

#has?(msg) ⇒ Boolean

Tests if this field has been set on the argument message.

Parameters:

  • msg (Google::Protobuf::Message)

Returns:

  • (Boolean)

    True iff message has this field set

Raises:

  • (TypeError)

    If this field does not exist on the message

  • (ArgumentError)

    If this field does not track presence



140
141
142
143
144
145
146
147
148
149
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 140

def has?(msg)
  if msg.class.descriptor != Google::Protobuf::FFI.get_containing_message_def(self)
    raise TypeError.new "has method called on wrong message type"
  end
  unless has_presence?
    raise ArgumentError.new "does not track presence"
  end

  Google::Protobuf::FFI.get_message_has msg.instance_variable_get(:@msg), self
end

#has_presence?Boolean

Tests if this field tracks presence.

Returns:

  • (Boolean)

    True iff this field tracks presence



155
156
157
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 155

def has_presence?
  @has_presence ||= Google::Protobuf::FFI.get_has_presence(self)
end

#inspectObject



50
51
52
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 50

def inspect
  "#{self.class.name}: #{name}"
end

#json_nameObject



58
59
60
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 58

def json_name
  @json_name ||= Google::Protobuf::FFI.get_json_name(self)
end

#labelObject



70
71
72
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 70

def label
  @label ||= Google::Protobuf::FFI::Label[Google::Protobuf::FFI.get_label(self)]
end

#map?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 188

def map?
  @map ||= Google::Protobuf::FFI.is_map self
end

#nameObject



54
55
56
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 54

def name
  @name ||= Google::Protobuf::FFI.get_full_name(self)
end

#numberObject



62
63
64
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 62

def number
  @number ||= Google::Protobuf::FFI.get_number(self)
end

#optionsObject



209
210
211
212
213
214
215
216
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 209

def options
  @options ||= begin
    size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
    temporary_arena = Google::Protobuf::FFI.create_arena
    buffer = Google::Protobuf::FFI.field_options(self, size_ptr, temporary_arena)
    Google::Protobuf::FieldOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze).send(:internal_deep_freeze)
  end
end

#repeated?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 192

def repeated?
  @repeated ||= Google::Protobuf::FFI.is_repeated self
end

#set(msg, value) ⇒ Object

call-seq:

FieldDescriptor.set(message, value)

Sets the value corresponding to this field to the given value on the given message. Raises an exception if message is of the wrong type. Performs the ordinary type-checks for field setting.

Parameters:

  • msg (Google::Protobuf::Message)
  • value (Object)


178
179
180
181
182
183
184
185
186
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 178

def set(msg, value)
  if msg.class.descriptor != Google::Protobuf::FFI.get_containing_message_def(self)
    raise TypeError.new "set method called on wrong message type"
  end
  unless set_value_on_message value, msg.instance_variable_get(:@msg), msg.instance_variable_get(:@arena)
    raise RuntimeError.new "allocation failed"
  end
  nil
end

#sub_message?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 196

def sub_message?
  @sub_message ||= Google::Protobuf::FFI.is_sub_message self
end

#submsg_nameObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 89

def submsg_name
  if defined? @submsg_name
    @submsg_name
  else
    @submsg_name = case c_type
      when :enum
        Google::Protobuf::FFI.get_enum_fullname Google::Protobuf::FFI.get_subtype_as_enum self
      when :message
        Google::Protobuf::FFI.get_message_fullname Google::Protobuf::FFI.get_subtype_as_message self
      else
        nil
      end
  end
end

#subtypeObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 118

def subtype
  if defined? @subtype
    @subtype
  else
    @subtype = case c_type
      when :enum
        Google::Protobuf::FFI.get_subtype_as_enum(self)
      when :message
        Google::Protobuf::FFI.get_subtype_as_message(self)
      else
        nil
      end
  end
end

#to_sObject



46
47
48
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 46

def to_s
  inspect
end

#typeObject



66
67
68
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 66

def type
  @type ||= Google::Protobuf::FFI.get_type(self)
end

#wrapper?Boolean

Returns:

  • (Boolean)


200
201
202
203
204
205
206
207
# File 'lib/google/protobuf/ffi/field_descriptor.rb', line 200

def wrapper?
  if defined? @wrapper
    @wrapper
  else
    message_descriptor = Google::Protobuf::FFI.get_subtype_as_message(self)
    @wrapper = message_descriptor.nil? ? false : message_descriptor.send(:wrapper?)
  end
end