Skip to content

Instantly share code, notes, and snippets.

@zhaoyao
Created March 8, 2017 14:48
Show Gist options
  • Save zhaoyao/6829d840cdfcac251b8bbf80bf0329b4 to your computer and use it in GitHub Desktop.
Save zhaoyao/6829d840cdfcac251b8bbf80bf0329b4 to your computer and use it in GitHub Desktop.
Decode protoc -o files
package main
import (
"fmt"
"github.com/golang/protobuf/proto"
google_protobuf "github.com/golang/protobuf/protoc-gen-go/descriptor"
"io/ioutil"
"os"
)
func main() {
d, err := ioutil.ReadFile(os.Args[1]) // protoc -o output.pb.bin *.proto
if err != nil {
panic(err)
}
var fs google_protobuf.FileDescriptorSet
if err := proto.Unmarshal(d, &fs); err != nil {
panic(err)
}
for _, f := range fs.File {
fmt.Println(*f.Name)
fmt.Println(*f.Package)
fmt.Println("messages:")
for _, m := range f.MessageType {
fmt.Printf("\t%s\n", *m.Name)
for _, field := range m.Field {
fmt.Printf("\t\t%v %v = %v\n", field.Type.String(), *field.Name, *field.Number)
}
}
fmt.Println("services:")
for _, s := range f.Service {
fmt.Printf("rpc %s {\n", *s.Name)
for _, m := range s.Method {
fmt.Printf("\trpc %s(%s) returns (%s);\n", *m.Name, *m.InputType, *m.OutputType)
}
fmt.Println("}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment