Skip to content

Latest commit

 

History

History
66 lines (55 loc) · 2.1 KB

readme.md

File metadata and controls

66 lines (55 loc) · 2.1 KB

protodefgen - Protocol Buffer Definition Generator

This package provides Go structures for definitions used in Protocol Buffer file and a helper to write those constructs to .proto file with valid syntax.

Usage

  1. Get the package

    go get github.com/xapi-tools/protodefgen@latest
  2. Create Go structures for Protocol Buffers and generate file

    package main
    
    import pfg "github.com/xapi-tools/protodefgen"
    
    func main() {
        pw := pfg.NewProtoDefWriter(
            &pfg.Proto{
                Package: "test",
                Imports: []string{"google/protobuf/empty.proto"},
                Messages: []pfg.Message{
                    {
                        Name:        "BasicType",
                        Description: "This message contains basic types",
                        Fields: []pfg.MessageField{
                            {
                                Description: "This is required string field",
                                Name:        "name",
                                Type:        "string",
                                Id:          0,
                            },
                        },
                    },
                },
                Services: []pfg.Service{
                    {
                        Name:        "BasicService",
                        Description: "This is a service exercising BasicType",
                        Methods: []pfg.ServiceMethod{
                            {
                                Name:        "GetBasic",
                                Description: "Get BasicType",
                                Request:     "google.protobuf.Empty",
                                Response:    "BasicType",
                            },
                        },
                    },
                },
            },
            &pfg.ProtoWriterOpts{
                IndentWidth: 4,
            },
        )
    
        pw.ToFile("./test.proto")
    }

    Check following for more comprehensive examples: