prototmpl.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package generator
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "github.com/zeromicro/go-zero/tools/goctl/util"
  6. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  7. "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
  8. )
  9. const rpcTemplateText = `syntax = "proto3";
  10. package {{.package}};
  11. option go_package="./{{.package}}";
  12. message Request {
  13. string ping = 1;
  14. }
  15. message Response {
  16. string pong = 1;
  17. }
  18. service {{.serviceName}} {
  19. rpc Ping(Request) returns(Response);
  20. }
  21. `
  22. // ProtoTmpl returns a sample of a proto file
  23. func ProtoTmpl(out string) error {
  24. protoFilename := filepath.Base(out)
  25. serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
  26. text, err := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
  27. if err != nil {
  28. return err
  29. }
  30. dir := filepath.Dir(out)
  31. err = pathx.MkdirIfNotExist(dir)
  32. if err != nil {
  33. return err
  34. }
  35. err = util.With("t").Parse(text).SaveTo(map[string]string{
  36. "package": serviceName.Untitle(),
  37. "serviceName": serviceName.Title(),
  38. }, out, false)
  39. return err
  40. }