Class: Protopuffs::MessageField

Inherits:
Object
  • Object
show all
Defined in:
lib/protopuffs/message/field.rb

Direct Known Subclasses

Bool, LengthDelimited, Numeric

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/protopuffs/message/field.rb', line 6

def default
  @default
end

#identifierObject (readonly)

Returns the value of attribute identifier.



6
7
8
# File 'lib/protopuffs/message/field.rb', line 6

def identifier
  @identifier
end

#tagObject (readonly)

Returns the value of attribute tag.



6
7
8
# File 'lib/protopuffs/message/field.rb', line 6

def tag
  @tag
end

Class Method Details

.factory(type, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/protopuffs/message/field.rb', line 8

def self.factory(type, *args)
  case type
  when "int32"    then Int32.new(*args)
  when "int64"    then Int64.new(*args)
  when "uint32"   then UInt32.new(*args)
  when "uint64"   then UInt64.new(*args)
  when "bool"     then Bool.new(*args)
  when "double"   then Double.new(*args)
  when "fixed64"  then Fixed64.new(*args)
  when "string"   then String.new(*args)
  when "bytes"    then Bytes.new(*args)
  when "float"    then Float.new(*args)
  when "fixed32"  then Fixed32.new(*args)
  when "sfixed32" then SFixed32.new(*args)
  else Embedded.new(type, *args)
  end
end

.shift_tag(buffer) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/protopuffs/message/field.rb', line 58

def self.shift_tag(buffer)
  bits = 0
  bytes = VarInt.shift(buffer)
  bytes.each_with_index do |byte, index|
    byte &= 0b01111111
    bits |= byte << (7 * index)
  end
  bits >> 3
end

Instance Method Details

#keyObject



37
38
39
# File 'lib/protopuffs/message/field.rb', line 37

def key
  (@tag << 3) | self.class.wire_type
end

#optional?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/protopuffs/message/field.rb', line 45

def optional?
  @modifier == "optional"
end

#repeated?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/protopuffs/message/field.rb', line 41

def repeated?
  @modifier == "repeated"
end

#to_wire_format_with_value(value) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/protopuffs/message/field.rb', line 49

def to_wire_format_with_value(value)
  field_encoder = lambda { |val| VarInt.encode(key) + self.class.encode(val) }
  if repeated?
    value.map(&field_encoder).join
  else
    field_encoder.call(value)
  end
end