gensvc.go 603 B

12345678910111213141516171819202122232425262728293031
  1. package gen
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "github.com/tal-tech/go-zero/tools/goctl/util"
  6. )
  7. const svcTemplate = `package svc
  8. import {{.imports}}
  9. type ServiceContext struct {
  10. c config.Config
  11. }
  12. func NewServiceContext(c config.Config) *ServiceContext {
  13. return &ServiceContext{
  14. c:c,
  15. }
  16. }
  17. `
  18. func (g *defaultRpcGenerator) genSvc() error {
  19. svcPath := g.dirM[dirSvc]
  20. fileName := filepath.Join(svcPath, fileServiceContext)
  21. return util.With("svc").GoFmt(true).Parse(svcTemplate).SaveTo(map[string]interface{}{
  22. "imports": fmt.Sprintf(`"%v"`, g.mustGetPackage(dirConfig)),
  23. }, fileName, false)
  24. }