rogchap / protoparser

A Protocol Buffer proto file parser for Go

Repository from Github https://github.comrogchap/protoparserRepository from Github https://github.comrogchap/protoparser

Proto Parser - a Protocol Buffer proto file parser for Go

NOTE: This is a new project that is WIP, and is not ready for production use

Motivation

There are a few parsers that are already available:

However, both these parsers have there own defined AST and APIs for managing proto files. The aim of protoparser is to provide a parser that uses the standard descriptorpb.FileDescriptorProto as the AST and provide a very basic API.

These makes it easier to use the standard protocol buffer tooling for Go (especially APIv2) For example using the protodesc package for converting FileDescriptorProto messages to protoreflect.FileDescriptor values. See google.golang.org/protobuf for details.

Design Spec

The parser should follow the Protocol Buffers Version 3 Language Specification

Usage

package main

import (
    "fmt"

    "rogchap.com/protoparser"
)

func main() {
    src := `
    syntax = "proto3";
    package foo.bar;

    message Foo {
        string name = 1;
    }
 
    message Bar {
        string id = 1;
        Foo foo = 2;
    }
    `

    // Parse the source
    fd, err := protoparser.ParseFile("", src)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Print the all the messages and their fields
    for _, m := fd.MessageType {
        fmt.Println(m.Name)
        for _, f := m.Field {
            fmt.Println(" - "+f.Name)
        }
        fmt.Println()
    }
}

About

A Protocol Buffer proto file parser for Go


Languages

Language:Go 100.0%