Skip to content

Instantly share code, notes, and snippets.

@codeck
Created December 31, 2013 03:50
Show Gist options
  • Save codeck/8192348 to your computer and use it in GitHub Desktop.
Save codeck/8192348 to your computer and use it in GitHub Desktop.
peek specific field from a protobuf message by field numbers
private[this] def peakNested(fid:Int, pbs:CodedInputStream):Option[CodedInputStream] = {
val (_, res) = Iterator.continually(pbs.readTag()).span{tag =>
(tag != 0) && (WireFormat.getTagFieldNumber(tag) != fid) && pbs.skipField(tag)
}
val lastid = WireFormat.getTagFieldNumber(res.next)
if (lastid == fid) Some(pbs)
else None
}
def recursivePeek[T](ids:Seq[Int], extract: (CodedInputStream=>T), pbs:CodedInputStream):Option[T] = {
ids match {
case h::Nil =>
peakNested(h, pbs).map(s => extract(s))
case h::ds =>
peakNested(h, pbs).flatMap(s => {
s.readRawVarint32()
recursivePeek(ds, extract, pbs)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment