Thursday, 4 October 2018

Protobuf getting metadata at runtime

Sometimes it's important to get the list of fields from Protobuf message.

The good example is Kafka with Protobuf serialiser - when the code generator to Java isn't used - but desc file should be generated anyway. It worse to invest into pact testing of the produced messages - check if the number of sent fields is within the range of defined in proto schema.
Unfortunately Protobuf for JVM doesn't support reading metadata from proto files, but if it's compiled to desc file - it's possible to read it via JVM API - that isn't intuitive:

import com.google.protobuf.DescriptorProtos.FileDescriptorSet
import com.google.protobuf.DescriptorProtos.FileDescriptorSet.parseFrom
import java.nio.file.{ Files, Paths }
import scala.collection.JavaConverters._
/**
* Reads protobuf desc file
* @param resourcePath2DescFile the path in resources to protobuf file
*/
class ProtobufReader(resourcePath2DescFile: String) {
private val fileSet: FileDescriptorSet =
parseFrom(Files.readAllBytes(Paths.get(this.getClass.getClassLoader.getResource(resourcePath2DescFile).toURI)))
assert(fileSet.getFileCount == 1, "Protobuf desc file should contain only one desc")
/**
* Returns the list of field names from message definition
* @param messageName message name definded in proto file
*/
def getFields(messageName: String): Seq[String] =
fileSet.getFile(0).getMessageTypeList.asScala
.find(_.getName == messageName)
.map(_.getFieldList.asScala).toList.flatten
.map(_.getName)
}

No comments:

Post a Comment