Created
December 31, 2013 03:50
-
-
Save codeck/8192348 to your computer and use it in GitHub Desktop.
peek specific field from a protobuf message by field numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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