go-zero/tools/goctl/rpc/generator/prototmpl.go
kingxt e6ef1fca12
Code optimized (#523)
* optimized markdown generator

* optimized markdown generator

* optimized markdown generator

* add more comment

* add comment

* add comment

* add comments for rpc tool

* add comments for model tool

* add comments for model tool

* add comments for model tool

* add comments for config tool

* add comments for config tool

* add comments

* add comments

* add comments

* add comments

* add comment

* remove rpc main head info

* add comment

* optimized

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
2021-02-26 16:11:47 +08:00

49 lines
967 B
Go

package generator
import (
"path/filepath"
"strings"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
const rpcTemplateText = `syntax = "proto3";
package {{.package}};
message Request {
string ping = 1;
}
message Response {
string pong = 1;
}
service {{.serviceName}} {
rpc Ping(Request) returns(Response);
}
`
// ProtoTmpl returns an sample of a proto file
func ProtoTmpl(out string) error {
protoFilename := filepath.Base(out)
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
text, err := util.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
if err != nil {
return err
}
dir := filepath.Dir(out)
err = util.MkdirIfNotExist(dir)
if err != nil {
return err
}
err = util.With("t").Parse(text).SaveTo(map[string]string{
"package": serviceName.Untitle(),
"serviceName": serviceName.Title(),
}, out, false)
return err
}