protoc-gen-go-micro/rpc.go
Vasiliy Tolstov 2c001628ff initial import
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-22 18:11:08 +03:00

40 lines
904 B
Go

package main
import (
"bytes"
"fmt"
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
}
// 1. Initialise a buffer to hold the generated code
var buf bytes.Buffer
// 2. Write the package name
pkg := fmt.Sprintf("package %s", file.GoPackageName)
buf.Write([]byte(pkg))
// 3. For each message add our Foo() method
for _, msg := range file.Proto.MessageType {
buf.Write([]byte(fmt.Sprintf(`func (x %s) Foo() string {
return "bar"}`, *msg.Name)))
}
// 4. Specify the output filename, in this case test.foo.go
filename := file.GeneratedFilenamePrefix + ".foo.go"
file := plugin.NewGeneratedFile(filename, ".")
// 5. Pass the data from our buffer to the plugin file struct
file.Write(buf.Bytes())
}
return nil
}