Class: Protobuf::Visitor::CreateDescriptorVisitor

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

Instance Attribute Summary collapse

Attributes inherited from Base

#silent

Instance Method Summary collapse

Methods inherited from Base

#create_file_with_backup, #log_writing

Constructor Details

#initialize(filename = nil) ⇒ CreateDescriptorVisitor

Returns a new instance of CreateDescriptorVisitor.



193
194
195
196
# File 'lib/protobuf/compiler/visitors.rb', line 193

def initialize(filename=nil)
  @context = []
  @filename = filename
end

Instance Attribute Details

#file_descriptorObject

Returns the value of attribute file_descriptor.



190
191
192
# File 'lib/protobuf/compiler/visitors.rb', line 190

def file_descriptor
  @file_descriptor
end

#filenameObject

Returns the value of attribute filename.



191
192
193
# File 'lib/protobuf/compiler/visitors.rb', line 191

def filename
  @filename
end

Instance Method Details

#add_option(name, value) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/protobuf/compiler/visitors.rb', line 218

def add_option(name, value)
  options = 
    case current_descriptor
    when Google::Protobuf::FileDescriptorProto
      Google::Protobuf::FileOptions.new
    when Google::Protobuf::DescriptorProto
      Google::Protobuf::MessageOptions.new
    when Google::Protobuf::FieldDescriptorProto
      Google::Protobuf::FieldOptions.new
    when Google::Protobuf::EnumDescriptorProto
      Google::Protobuf::EnumOptions.new
    when Google::Protobuf::EnumValueDescriptorProto
      Google::Protobuf::EnumValueOptions.new
    when Google::Protobuf::ServiceDescriptorProto
      Google::Protobuf::ServiceOptions.new
    when Google::Protobuf::MethodDescriptorProto
      Google::Protobuf::MethodOptions.new
    else
      raise ArgumentError.new('Invalid context')
    end
  #TODO how should options be handled?
  #current_descriptor.options << option
end

#current_descriptorObject



209
210
211
# File 'lib/protobuf/compiler/visitors.rb', line 209

def current_descriptor
  @context.last
end

#descriptor=(descriptor) ⇒ Object Also known as: message_descriptor=



242
243
244
245
246
247
248
249
250
251
# File 'lib/protobuf/compiler/visitors.rb', line 242

def descriptor=(descriptor)
  case current_descriptor
  when Google::Protobuf::FileDescriptorProto
    current_descriptor.message_type << descriptor
  when Google::Protobuf::DescriptorProto
    current_descriptor.nested_type << descriptor
  else
    raise ArgumentError.new('Invalid context')
  end
end

#enum_descriptor=(descriptor) ⇒ Object



254
255
256
# File 'lib/protobuf/compiler/visitors.rb', line 254

def enum_descriptor=(descriptor)
  current_descriptor.enum_type << descriptor
end

#enum_value_descriptor=(descriptor) ⇒ Object



258
259
260
# File 'lib/protobuf/compiler/visitors.rb', line 258

def enum_value_descriptor=(descriptor)
  current_descriptor.value << descriptor
end

#extension_range_descriptor=(descriptor) ⇒ Object



283
284
285
# File 'lib/protobuf/compiler/visitors.rb', line 283

def extension_range_descriptor=(descriptor)
  current_descriptor.extension_range << descriptor
end

#field_descriptor=(descriptor) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/protobuf/compiler/visitors.rb', line 270

def field_descriptor=(descriptor)
  case current_descriptor
  when Google::Protobuf::FileDescriptorProto
    current_descriptor.extension << descriptor
  when Google::Protobuf::DescriptorProto
    current_descriptor.field << descriptor
    #TODO: how should i distiguish between field and extension
    #current_descriptor.extension << descriptor
  else
    raise ArgumentError.new('Invalid context')
  end
end

#in_context(descriptor, &block) ⇒ Object



203
204
205
206
207
# File 'lib/protobuf/compiler/visitors.rb', line 203

def in_context(descriptor, &block)
  @context.push descriptor
  block.call
  @context.pop
end

#method_descriptor=(descriptor) ⇒ Object



266
267
268
# File 'lib/protobuf/compiler/visitors.rb', line 266

def method_descriptor=(descriptor)
  current_descriptor.method << descriptor
end

#service_descriptor=(descriptor) ⇒ Object



262
263
264
# File 'lib/protobuf/compiler/visitors.rb', line 262

def service_descriptor=(descriptor)
  current_descriptor.service << descriptor
end

#visit(node) ⇒ Object



198
199
200
201
# File 'lib/protobuf/compiler/visitors.rb', line 198

def visit(node)
  node.accept_descriptor_visitor self 
  self
end