Skip to content

Instantly share code, notes, and snippets.

Created March 11, 2018 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/d7444428251fe1b6c15e589b5daccbdd to your computer and use it in GitHub Desktop.
Save anonymous/d7444428251fe1b6c15e589b5daccbdd to your computer and use it in GitHub Desktop.
import com.google.protobuf.DescriptorProtos.FileDescriptorSet
import java.io.FileInputStream
import scala.collection.JavaConverters._
val descriptorFile = new FileInputStream("path/to/descriptorfile/descriptorfilename.desc")
val descriptorSetObject = FileDescriptorSet.parseFrom(descriptorFile)
//DescriptorSet contains multiple file descriptors including the dependencies
//To get a particular file descriptor, just filter out using the file's name
val abcProtoFileDescriptor = FileDescriptor.buildFrom(descriptorSetObject.getFileList.asScala.filter(_.getName.contains("abc")).head, Array())
//Once you have the file descriptor, you can extract message descriptors from it using message names
//e.g. suppose the message name is TestProtoMessage
val messageDescriptor = abcProtoFileDescriptor.getMessageTypes.iterator().asScala.filter(_.getName=="TestProtoMessage").toList.head
//To Parse a byte-array to proto object using dynamic messages
val protoObject = DynamicMessage.parseFrom(messageDescriptor, inputByteArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment