prototmpl.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. message Request {
  12. string ping = 1;
  13. }
  14. message Response {
  15. string pong = 1;
  16. }
  17. service {{.serviceName}} {
  18. rpc Ping(Request) returns(Response);
  19. }
  20. `
  21. // ProtoTmpl returns a sample of a proto file
  22. func ProtoTmpl(out string) error {
  23. protoFilename := filepath.Base(out)
  24. serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
  25. text, err := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
  26. if err != nil {
  27. return err
  28. }
  29. dir := filepath.Dir(out)
  30. err = pathx.MkdirIfNotExist(dir)
  31. if err != nil {
  32. return err
  33. }
  34. err = util.With("t").Parse(text).SaveTo(map[string]string{
  35. "package": serviceName.Untitle(),
  36. "serviceName": serviceName.Title(),
  37. }, out, false)
  38. return err
  39. }