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.



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

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



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

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 then
      descriptor.default_value = val.to_s
    end
  end
  visitor.field_descriptor = descriptor
end

#accept_message_visitor(visitor) ⇒ Object



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

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