protobuf-go/cmd/protoc-gen-go/main.go
Damien Neil 082ce923d3 protogen: compute package names, import paths, generated filenames
Copy/duplicate the logic in github.com/golang/protobuf for computing
package names and import paths and the names of generated files.

This is all sufficiently complicated that the code is the best
documentation. In practice, users should always set a go_package option
containing an import path in every file and pass the
paths=source_relative generator flag to get reasonable behavior.

Change-Id: I34ae38fcc8db6909a4b25b16c73b982a7bad0463
Reviewed-on: https://go-review.googlesource.com/133876
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2018-09-07 17:58:10 +00:00

49 lines
1.0 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// The protoc-gen-go binary is a protoc plugin to generate a Go protocol
// buffer package.
package main
import (
"google.golang.org/proto/protogen"
)
func main() {
protogen.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
genFile(gen, f)
}
return nil
})
}
func genFile(gen *protogen.Plugin, f *protogen.File) {
g := gen.NewGeneratedFile(f.GeneratedFilenamePrefix+".pb.go", f.GoImportPath)
g.P("// Code generated by protoc-gen-go. DO NOT EDIT.")
g.P("// source: ", f.Desc.Path())
g.P()
g.P("package ", f.GoPackageName)
g.P()
for _, m := range f.Messages {
genMessage(gen, g, m)
}
// TODO: Everything.
}
func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, m *protogen.Message) {
g.P("type ", m.GoIdent, " struct {")
g.P("}")
g.P()
for _, nested := range m.Messages {
genMessage(gen, g, nested)
}
}