prototmpl.go 967 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // ProtoTmpl returns an sample of a proto file
  21. func ProtoTmpl(out string) error {
  22. protoFilename := filepath.Base(out)
  23. serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
  24. text, err := util.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
  25. if err != nil {
  26. return err
  27. }
  28. dir := filepath.Dir(out)
  29. err = util.MkdirIfNotExist(dir)
  30. if err != nil {
  31. return err
  32. }
  33. err = util.With("t").Parse(text).SaveTo(map[string]string{
  34. "package": serviceName.Untitle(),
  35. "serviceName": serviceName.Title(),
  36. }, out, false)
  37. return err
  38. }