Class: Google::Protobuf::Internal::MessageBuilder
- Inherits:
-
Object
- Object
- Google::Protobuf::Internal::MessageBuilder
- Defined in:
- lib/google/protobuf/descriptor_dsl.rb
Instance Method Summary collapse
-
#initialize(name, file_builder, file_proto) ⇒ MessageBuilder
constructor
A new instance of MessageBuilder.
- #internal_add_field(label, name, type, number, type_class, options, oneof_index: nil, proto3_optional: false) ⇒ Object
-
#internal_add_synthetic_oneofs ⇒ Object
—- Internal methods, not part of the DSL —-.
- #internal_msg_proto ⇒ Object
-
#map(name, key_type, value_type, number, value_type_class = nil) ⇒ Object
Defines a new map field on this message type with the given key and value types, tag number, and type class (for message and enum value types).
- #oneof(name, &block) ⇒ Object
- #optional(name, type, number, type_class = nil, options = nil) ⇒ Object
- #proto3_optional(name, type, number, type_class = nil, options = nil) ⇒ Object
- #repeated(name, type, number, type_class = nil, options = nil) ⇒ Object
- #required(name, type, number, type_class = nil, options = nil) ⇒ Object
Constructor Details
#initialize(name, file_builder, file_proto) ⇒ MessageBuilder
Returns a new instance of MessageBuilder.
290 291 292 293 294 295 296 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 290 def initialize(name, file_builder, file_proto) @file_builder = file_builder @msg_proto = Google::Protobuf::DescriptorProto.new( :name => name ) file_proto. << @msg_proto end |
Instance Method Details
#internal_add_field(label, name, type, number, type_class, options, oneof_index: nil, proto3_optional: false) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 366 def internal_add_field(label, name, type, number, type_class, , oneof_index: nil, proto3_optional: false) # Allow passing either: # - (name, type, number, options) or # - (name, type, number, type_class, options) if .nil? and type_class.instance_of?(Hash) = type_class; type_class = nil; end field_proto = Google::Protobuf::FieldDescriptorProto.new( :label => label, :name => name, :type => ("TYPE_" + type.to_s.upcase).to_sym, :number => number ) if type_class # Make it an absolute type name by prepending a dot. field_proto.type_name = "." + type_class end if oneof_index field_proto.oneof_index = oneof_index end if proto3_optional field_proto.proto3_optional = true end if if .key?(:default) default = [:default] if !default.instance_of?(String) # Call #to_s since all defaults are strings in the descriptor. default = default.to_s end # XXX: we should be C-escaping bytes defaults. field_proto.default_value = default.dup.force_encoding("UTF-8") end if .key?(:json_name) field_proto.json_name = [:json_name] end end @msg_proto.field << field_proto end |
#internal_add_synthetic_oneofs ⇒ Object
—- Internal methods, not part of the DSL —-
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 343 def internal_add_synthetic_oneofs # We have to build a set of all names, to ensure that synthetic oneofs # are not creating conflicts names = {} @msg_proto.field.each { |field| names[field.name] = true } @msg_proto.oneof_decl.each { |oneof| names[oneof.name] = true } @msg_proto.field.each { |field| if field.proto3_optional # Prepend '_' until we are no longer conflicting. oneof_name = field.name while names[oneof_name] oneof_name = "_" + oneof_name end names[oneof_name] = true field.oneof_index = @msg_proto.oneof_decl.size @msg_proto.oneof_decl << Google::Protobuf::OneofDescriptorProto.new( name: oneof_name ) end } end |
#internal_msg_proto ⇒ Object
414 415 416 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 414 def internal_msg_proto @msg_proto end |
#map(name, key_type, value_type, number, value_type_class = nil) ⇒ Object
Defines a new map field on this message type with the given key and value types, tag number, and type class (for message and enum value types). The key type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the type_class must be a string, if present (as accepted by FieldDescriptor#submsg_name=).
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 325 def map(name, key_type, value_type, number, value_type_class = nil) if key_type == :float or key_type == :double or key_type == :enum or key_type == :message raise ArgError, "Not an acceptable key type: " + key_type end entry_name = "#{@msg_proto.name}_MapEntry_#{name}" @file_builder. entry_name do optional :key, key_type, 1 optional :value, value_type, 2, value_type_class end = @file_builder.internal_file_proto..last. ||= MessageOptions.new .map_entry = true repeated name, :message, number, entry_name end |
#oneof(name, &block) ⇒ Object
315 316 317 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 315 def oneof(name, &block) OneofBuilder.new(name, self).instance_eval(&block) end |
#optional(name, type, number, type_class = nil, options = nil) ⇒ Object
298 299 300 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 298 def optional(name, type, number, type_class=nil, =nil) internal_add_field(:LABEL_OPTIONAL, name, type, number, type_class, ) end |
#proto3_optional(name, type, number, type_class = nil, options = nil) ⇒ Object
302 303 304 305 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 302 def proto3_optional(name, type, number, type_class=nil, =nil) internal_add_field(:LABEL_OPTIONAL, name, type, number, type_class, , proto3_optional: true) end |
#repeated(name, type, number, type_class = nil, options = nil) ⇒ Object
311 312 313 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 311 def repeated(name, type, number, type_class = nil, =nil) internal_add_field(:LABEL_REPEATED, name, type, number, type_class, ) end |
#required(name, type, number, type_class = nil, options = nil) ⇒ Object
307 308 309 |
# File 'lib/google/protobuf/descriptor_dsl.rb', line 307 def required(name, type, number, type_class=nil, =nil) internal_add_field(:LABEL_REQUIRED, name, type, number, type_class, ) end |