Module: Beefcake::Message::Decode

Defined in:
lib/beefcake.rb

Instance Method Summary collapse

Instance Method Details

#decode(buf, o = self.new) ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/beefcake.rb', line 172

def decode(buf, o=self.new)
  if ! buf.is_a?(Buffer)
    buf = Buffer.new(buf)
  end

  # TODO: test for incomplete buffer
  while buf.length > 0
    fn, wire = buf.read_info

    fld = fields[fn]

    # We don't have a field for with index fn.
    # Ignore this data and move on.
    if fld.nil?
      buf.skip(wire)
      next
    end

    exp = Buffer.wire_for(fld.type)
    if wire != exp
      raise WrongTypeError.new(fld.name, exp, wire)
    end

    if fld.rule == :repeated && fld.opts[:packed]
      len = buf.read_uint64
      tmp = Buffer.new(buf.read(len))
      o[fld.name] ||= []
      while tmp.length > 0
        o[fld.name] << tmp.read(fld.type)
      end
    elsif fld.rule == :repeated
      val = buf.read(fld.type)
      o[fld.name] ||= []
      o[fld.name] << val
    else
      val = buf.read(fld.type)
      o[fld.name] = val
    end
  end

  # Set defaults
  fields.values.each do |f|
    next if o[f.name] == false
    o[f.name] ||= f.opts[:default]
  end

  o.validate!

  o
end

#read_delimited(buf, o = self.new) ⇒ Object


223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/beefcake.rb', line 223

def read_delimited(buf, o=self.new)
  if ! buf.is_a?(Buffer)
    buf = Buffer.new(buf)
  end

  return if buf.length == 0

  n = buf.read_int64
  tmp = Buffer.new(buf.read(n))

  decode(tmp, o)
end