mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
227104d7d7
* Fix goctl completion expression * Fix code generation error if the pkg of pb/grpc is same to zrpc call client pkg * Remove deprecated comment on action goctl rpc new * Remove zrpc code generation on action goctl rpc proto * Remove zrpc code generation on action goctl rpc proto * Remove Generator interface Co-authored-by: anqiansong <anqiansong@bytedance.com>
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package generator
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util"
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
|
|
)
|
|
|
|
const rpcTemplateText = `syntax = "proto3";
|
|
|
|
package {{.package}};
|
|
option go_package="./{{.package}}";
|
|
|
|
message Request {
|
|
string ping = 1;
|
|
}
|
|
|
|
message Response {
|
|
string pong = 1;
|
|
}
|
|
|
|
service {{.serviceName}} {
|
|
rpc Ping(Request) returns(Response);
|
|
}
|
|
`
|
|
|
|
// ProtoTmpl returns a 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 := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
dir := filepath.Dir(out)
|
|
err = pathx.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
|
|
}
|