Class: Protobuf::Node::FieldNode

Inherits:
Base
  • Object
show all
Defined in:
lib/protobuf/compiler/nodes.rb

Instance Method Summary collapse

Methods inherited from Base

#accept_rpc_visitor, #define_in_the_file

Constructor Details

#initialize(label, type, name, value, opts = {}) ⇒ FieldNode

Returns a new instance of FieldNode.



245
246
247
# File 'lib/protobuf/compiler/nodes.rb', line 245

def initialize(label, type, name, value, opts={})
  @label, @type, @name, @value, @opts = label, type, name, value, opts
end

Instance Method Details

#accept_descriptor_visitor(visitor) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/protobuf/compiler/nodes.rb', line 261

def accept_descriptor_visitor(visitor)
  descriptor = Google::Protobuf::FieldDescriptorProto.new :name => @name.to_s, :number => @value
  descriptor.label = Google::Protobuf::FieldDescriptorProto::Label.const_get "LABEL_#{@label.to_s.upcase}"
  descriptor.type = Google::Protobuf::FieldDescriptorProto::Type.const_get "TYPE_#{@type.to_s.upcase}" if predefined_type?
  descriptor.type_name = @type.is_a?(Array) ? @type.join : @type.to_s
  @opts.each do |key, val|
    case key.to_sym
    when :default
      descriptor.default_value = val.to_s
    end
  end
  visitor.field_descriptor = descriptor
end

#accept_message_visitor(visitor) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/protobuf/compiler/nodes.rb', line 249

def accept_message_visitor(visitor)
  opts = @opts.empty? ? '' : ", #{@opts.map{|k, v| ":#{k} => #{v.inspect}" }.join(', ')}"
  if visitor.context.first == Protobuf::Node::ExtendNode
    opts += ', :extension => true'
  end
  type = if @type.is_a?(Array)
         then (@type.size > 1) ? "'#{@type.join '::'}'" : @type[0]
         else @type
         end
  visitor.write "#{@label} :#{type}, :#{@name}, #{@value}#{opts}"
end

#predefined_type?Boolean

Returns:

  • (Boolean)


275
276
277
# File 'lib/protobuf/compiler/nodes.rb', line 275

def predefined_type?
  %w{double float int64 uint64 int32 fixed64 fixed32 bool string group message bytes uint32 enum sfixed32 sfixed64 sint32 sint64}.include? @type.to_s
end