Skip to content

numq/protobuf-descriptor-parser

Repository files navigation

Protobuf Descriptor Parser

A library that provides extension functions for parsing the basic descriptors of Protocol Buffers, which allows you to create descriptors without code compilation, which is especially useful when working with dynamic messages.

Descriptors

You can change any parsing method by calling toBuilder()

Wire Schema
class name
Protobuf Kotlin
class name
Method name
EnumConstantElement EnumValueDescriptorProto enumValueDescriptorProto
EnumElement EnumDescriptorProto enumDescriptorProto
FieldElement FieldDescriptorProto fieldDescriptorProto
MessageElement DescriptorProto descriptorProto
ProtoFileElement FileDescriptorProto fileDescriptorProto
RpcElement MethodDescriptorProto methodDescriptorProto
ServiceElement ServiceDescriptorProto serviceDescriptorProto

Dependencies

You can exclude dependencies if they are already in use

Documentation - v3.23.4

Documentation - v4.8.0

Installation

build.gradle

    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }

    dependencies {
        implementation('com.github.numq:protobuf-descriptor-parser:1.0.0') {
            exclude group: 'com.google.protobuf', module: 'protobuf-kotlin'
            exclude group: 'com.squareup.wire', module: 'wire-schema'
        }
    }

build.gradle.kts

    allprojects {
        repositories {
            maven("https://jitpack.io")
        }
    }
    
    dependencies {
        implementation("com.github.numq:protobuf-descriptor-parser:1.0.0") {
            exclude(group = "com.google.protobuf", module = "protobuf-kotlin")
            exclude(group = "com.squareup.wire", module = "wire-schema")
        }
    }

Usage

val protoFile = File("./path/to/*.proto")
val schema = ProtoParser.parse(Location.get(path), proto.readText())
val descriptor = Descriptors.FileDescriptor.buildFrom(schema.parseDescriptor(protoFile.name), arrayOf())
// Use descriptor to create a dynamic message or for other purposes