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.



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

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

Instance Attribute Details

#file_descriptorObject

Returns the value of attribute file_descriptor.



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

def file_descriptor
  @file_descriptor
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

Instance Method Details

#add_option(name, value) ⇒ Object



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

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

#current_descriptorObject



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

def current_descriptor
  @context.last
end

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



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

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

#enum_descriptor=(descriptor) ⇒ Object



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

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

#enum_value_descriptor=(descriptor) ⇒ Object



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

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

#extension_range_descriptor=(descriptor) ⇒ Object



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

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

#field_descriptor=(descriptor) ⇒ Object



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

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

#in_context(descriptor, &block) ⇒ Object



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

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

#method_descriptor=(descriptor) ⇒ Object



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

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

#service_descriptor=(descriptor) ⇒ Object



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

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

#visit(node) ⇒ Object



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

def visit(node)
  node.accept_descriptor_visitor(self)
  self
end