You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
920 B
Go
48 lines
920 B
Go
4 years ago
|
package generator
|
||
4 years ago
|
|
||
|
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);
|
||
|
}
|
||
|
`
|
||
|
|
||
4 years ago
|
func ProtoTmpl(out string) error {
|
||
|
protoFilename := filepath.Base(out)
|
||
4 years ago
|
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
|
||
|
text, err := util.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
|
||
4 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
dir := filepath.Dir(out)
|
||
|
err = util.MkdirIfNotExist(dir)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
4 years ago
|
|
||
|
err = util.With("t").Parse(text).SaveTo(map[string]string{
|
||
|
"package": serviceName.UnTitle(),
|
||
|
"serviceName": serviceName.Title(),
|
||
4 years ago
|
}, out, false)
|
||
|
return err
|
||
4 years ago
|
}
|