-
-
Save anonymous/d7444428251fe1b6c15e589b5daccbdd to your computer and use it in GitHub Desktop.
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
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