Creating a proto file from golang struct

I have a golang structure that contains links to some other structures. Is there an automated way to create a .proto file from structures?

For instance:

type A struct {
 a int
 b B
}

type B struct {
 c []C
}

type C struct {
 x int
}

should generate:

message A, B, Cetc. proto3 is preferred.

https://github.com/kubernetes/kubernetes/tree/master/cmd/libs/go2idl seems to have something related but undocumented. Any options?

+4
source share
1 answer

I find the package, create .proto files from Go: proteus source code (https://github.com/src-d/proteus)

Proteus/proʊtiΙ™s/- , .proto , Go.

, Go , , Go .proto, .

protobuf

  //proteus:generate
  type User struct {
        Model
        Username string
  }

  type Model struct {
        ID int
        CreatedAt time.Time
  }

protobuf.

  message User {
          int32 id = 1;
          google.protobuf.Timestamp created_at = 2;
          string username = 3;
  }

 go get -v gopkg.in/src-d/proteus.v1/...

.

 protoc binary installed on your path
 go get -u github.com/gogo/protobuf/...

, / protobuf Go, RPC- RPC . .

 proteus -f /path/to/protos/folder \
    -p my/go/package \
    -p my/other/go/package

, .

 proteus proto -f /path/to/output/folder \
    -p my/go/package \
    -p my/other/go/package
    --verbose

gRPC .

  proteus rpc -p my/go/package \
    -p my/other/go/package

. , , , Proteus , . Godoc .

+3

Source: https://habr.com/ru/post/1649933/


All Articles