class Google::Protobuf::Internal::MessageBuilder

Public Class Methods

new(name, file_builder, file_proto) click to toggle source
# 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.message_type << @msg_proto
end

Public Instance Methods

internal_add_field(label, name, type, number, type_class, options, oneof_index: nil, proto3_optional: false) click to toggle source
# File lib/google/protobuf/descriptor_dsl.rb, line 366
def internal_add_field(label, name, type, number, type_class, options,
                       oneof_index: nil, proto3_optional: false)
  # Allow passing either:
  # - (name, type, number, options) or
  # - (name, type, number, type_class, options)
  if options.nil? and type_class.instance_of?(Hash)
    options = 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 options
    if options.key?(:default)
      default = options[: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 options.key?(:json_name)
      field_proto.json_name = options[:json_name]
    end
  end

  @msg_proto.field << field_proto
end
internal_add_synthetic_oneofs() click to toggle source

—- Internal methods, not part of the DSL —-

# 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() click to toggle source
# 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) click to toggle source

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=).

# 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.add_message entry_name do
    optional :key, key_type, 1
    optional :value, value_type, 2, value_type_class
  end
  options = @file_builder.internal_file_proto.message_type.last.options ||= MessageOptions.new
  options.map_entry = true
  repeated name, :message, number, entry_name
end
oneof(name, &block) click to toggle source
# 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) click to toggle source
# File lib/google/protobuf/descriptor_dsl.rb, line 298
def optional(name, type, number, type_class=nil, options=nil)
  internal_add_field(:LABEL_OPTIONAL, name, type, number, type_class, options)
end
proto3_optional(name, type, number, type_class=nil, options=nil) click to toggle source
# File lib/google/protobuf/descriptor_dsl.rb, line 302
def proto3_optional(name, type, number, type_class=nil, options=nil)
  internal_add_field(:LABEL_OPTIONAL, name, type, number, type_class, options,
                     proto3_optional: true)
end
repeated(name, type, number, type_class = nil, options=nil) click to toggle source
# File lib/google/protobuf/descriptor_dsl.rb, line 311
def repeated(name, type, number, type_class = nil, options=nil)
  internal_add_field(:LABEL_REPEATED, name, type, number, type_class, options)
end
required(name, type, number, type_class=nil, options=nil) click to toggle source
# File lib/google/protobuf/descriptor_dsl.rb, line 307
def required(name, type, number, type_class=nil, options=nil)
  internal_add_field(:LABEL_REQUIRED, name, type, number, type_class, options)
end