Class: Protobuf::Descriptor::FieldDescriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/descriptor/field_descriptor.rb

Instance Method Summary collapse

Constructor Details

#initialize(field_instance = nil) ⇒ FieldDescriptor

Returns a new instance of FieldDescriptor.



4
5
6
# File 'lib/protobuf/descriptor/field_descriptor.rb', line 4

def initialize(field_instance=nil)
  @field_instance = field_instance
end

Instance Method Details

#build(proto, opt = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/protobuf/descriptor/field_descriptor.rb', line 12

def build(proto, opt={})
  cls = opt[:class]
  rule = Protobuf::Descriptor.id2label(proto.label)
  type = Protobuf::Descriptor.id2type(proto.type)
  type = proto.type_name.to_sym if [:message, :enum].include?(type)
  opts = {}
  opts[:default] = proto.default_value if proto.default_value
  cls.define_field(rule, type, proto.name, proto.number, opts)
end

#proto_typeObject



8
9
10
# File 'lib/protobuf/descriptor/field_descriptor.rb', line 8

def proto_type
  'Google::Protobuf::FieldDescriptorProto'
end

#unbuild(parent_proto, extension = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/protobuf/descriptor/field_descriptor.rb', line 22

def unbuild(parent_proto, extension=false)
  field_proto = Google::Protobuf::FieldDescriptorProto.new
  field_proto.name = @field_instance.name.to_s
  field_proto.number = @field_instance.tag
  field_proto.label = Protobuf::Descriptor.label2id(@field_instance.rule)
  field_proto.type = Protobuf::Descriptor.type2id(@field_instance.type)
  if [Google::Protobuf::FieldDescriptorProto::Type::TYPE_MESSAGE,
    Google::Protobuf::FieldDescriptorProto::Type::TYPE_ENUM].include? field_proto.type
    field_proto.type_name = @field_instance.type.to_s.split('::').last
  end
  field_proto.default_value = @field_instance.default.to_s if @field_instance.default

  case parent_proto
  when Google::Protobuf::FileDescriptorProto then
    parent_proto.extension << field_proto
  when Google::Protobuf::DescriptorProto then
    if extension
      parent_proto.extension << field_proto
    else
      parent_proto.field << field_proto
    end
  else
    raise TypeError, parent_proto.class.name
  end
end