prototmpl.go 920 B

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