Class: Protobuf::Field::BaseField

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

Direct Known Subclasses

BytesField, FloatField, MessageField, VarintField

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_class, rule, type, name, tag, options) ⇒ BaseField

Returns a new instance of BaseField.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/protobuf/message/field.rb', line 46

def initialize(message_class, rule, type, name, tag, options)
  @message_class, @rule, @type, @name, @tag = \
    message_class, rule, type, name, tag

  @default   = options.delete(:default)
  @extension = options.delete(:extension)
  @packed    = repeated? && options.delete(:packed)
  unless options.empty?
    warn "WARNING: Invalid options: #{options.inspect} (in #{@message_class.name.split('::').last}.#{@name})"
  end
  if packed? && ! [WireType::VARINT, WireType::FIXED32, WireType::FIXED64].include?(wire_type)
    raise "Can't use packed encoding for `#{@type}' type"
  end

  @default_value = \
    case @rule
    when :repeated then
      FieldArray.new(self).freeze
    when :required then
      nil
    when :optional then
      typed_default_value
    end

  define_accessor
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def default
  @default
end

#default_valueObject (readonly)

Returns the value of attribute default_value.



40
41
42
# File 'lib/protobuf/message/field.rb', line 40

def default_value
  @default_value
end

#message_classObject (readonly)

Returns the value of attribute message_class.



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def message_class
  @message_class
end

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def name
  @name
end

#ruleObject (readonly)

Returns the value of attribute rule.



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def rule
  @rule
end

#tagObject (readonly)

Returns the value of attribute tag.



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def tag
  @tag
end

#typeObject (readonly)

Returns the value of attribute type.



39
40
41
# File 'lib/protobuf/message/field.rb', line 39

def type
  @type
end

Class Method Details

.defaultObject



35
36
37
# File 'lib/protobuf/message/field.rb', line 35

def self.default
  nil
end

.descriptorObject



31
32
33
# File 'lib/protobuf/message/field.rb', line 31

def self.descriptor
  @descriptor ||= Descriptor::FieldDescriptor.new
end

Instance Method Details

#acceptable?(value) ⇒ Boolean

Is a +value+ acceptable for this field?

Returns:

  • (Boolean)


163
164
165
# File 'lib/protobuf/message/field.rb', line 163

def acceptable?(value)
  true
end

#decode(bytes) ⇒ Object

Decode +bytes+ and return a field value.

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/protobuf/message/field.rb', line 114

def decode(bytes)
  raise NotImplementedError, "#{self.class.name}\#decode"
end

#descriptorObject



42
43
44
# File 'lib/protobuf/message/field.rb', line 42

def descriptor
  @descriptor ||= Descriptor::FieldDescriptor.new(self)
end

#encode(value) ⇒ Object

Encode +value+ and return a byte string.

Raises:

  • (NotImplementedError)


119
120
121
# File 'lib/protobuf/message/field.rb', line 119

def encode(value)
  raise NotImplementedError, "#{self.class.name}\#encode"
end

#initialized?(message_instance) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/protobuf/message/field.rb', line 77

def initialized?(message_instance)
  value = message_instance.__send__(@name)
  case @rule
  when :required then
    ! value.nil? && (! kind_of?(MessageField) || value.initialized?)
  when :repeated then
    value.all? {|msg| ! kind_of?(MessageField) || msg.initialized? }
  when :optional then
    value.nil? || ! kind_of?(MessageField) || value.initialized?
  end
end

#maxObject

Upper limit for this field.



153
154
155
# File 'lib/protobuf/message/field.rb', line 153

def max
  self.class.max
end

#merge(message_instance, value) ⇒ Object

Merge +value+ with +message_instance+.



124
125
126
127
128
129
130
# File 'lib/protobuf/message/field.rb', line 124

def merge(message_instance, value)
  if repeated?
    merge_array(message_instance, value)
  else
    merge_value(message_instance, value)
  end
end

#minObject

Lower limit for this field.



158
159
160
# File 'lib/protobuf/message/field.rb', line 158

def min
  self.class.min
end

#optional?Boolean

Is this a optional field?

Returns:

  • (Boolean)


143
144
145
# File 'lib/protobuf/message/field.rb', line 143

def optional?
  @rule == :optional
end

#packed?Boolean

Is this a packed repeated field?

Returns:

  • (Boolean)


148
149
150
# File 'lib/protobuf/message/field.rb', line 148

def packed?
  !!@packed
end

#ready?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/protobuf/message/field.rb', line 73

def ready?
  true
end

#repeated?Boolean

Is this a repeated field?

Returns:

  • (Boolean)


133
134
135
# File 'lib/protobuf/message/field.rb', line 133

def repeated?
  @rule == :repeated
end

#required?Boolean

Is this a required field?

Returns:

  • (Boolean)


138
139
140
# File 'lib/protobuf/message/field.rb', line 138

def required?
  @rule == :required
end

#set(message_instance, bytes) ⇒ Object

Decode +bytes+ and pass to +message_instance+.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/protobuf/message/field.rb', line 90

def set(message_instance, bytes)
  if packed?
    array = message_instance.__send__(@name)
    method = \
      case wire_type
      when WireType::FIXED32 then :read_fixed32
      when WireType::FIXED64 then :read_fixed64
      when WireType::VARINT  then :read_varint
      end
    stream = StringIO.new(bytes)
    until stream.eof?
      array << decode(Decoder.__send__(method, stream))
    end
  else
    value = decode(bytes)
    if repeated?
      message_instance.__send__(@name) << value
    else
      message_instance.__send__("#{@name}=", value)
    end
  end
end

#to_sObject



167
168
169
# File 'lib/protobuf/message/field.rb', line 167

def to_s
  "#{@rule} #{@type} #{@name} = #{@tag} #{@default ? "[default=#{@default.inspect}]" : ''}"
end