template.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package gen
  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/console"
  7. "github.com/tal-tech/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. type rpcTemplate struct {
  22. out string
  23. console.Console
  24. }
  25. func NewRpcTemplate(out string, idea bool) *rpcTemplate {
  26. return &rpcTemplate{
  27. out: out,
  28. Console: console.NewConsole(idea),
  29. }
  30. }
  31. func (r *rpcTemplate) MustGenerate(showState bool) {
  32. protoFilename := filepath.Base(r.out)
  33. serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
  34. err := util.With("t").Parse(rpcTemplateText).SaveTo(map[string]string{
  35. "package": serviceName.UnTitle(),
  36. "serviceName": serviceName.Title(),
  37. }, r.out, false)
  38. r.Must(err)
  39. if showState {
  40. r.Success("Done.")
  41. }
  42. }