You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
805 B
Go
38 lines
805 B
Go
4 years ago
|
package generator
|
||
4 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"path/filepath"
|
||
|
|
||
4 years ago
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
||
4 years ago
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
const svcTemplate = `package svc
|
||
4 years ago
|
|
||
|
import {{.imports}}
|
||
|
|
||
4 years ago
|
type ServiceContext struct {
|
||
|
c config.Config
|
||
|
}
|
||
4 years ago
|
|
||
|
func NewServiceContext(c config.Config) *ServiceContext {
|
||
|
return &ServiceContext{
|
||
|
c:c,
|
||
|
}
|
||
|
}
|
||
|
`
|
||
|
|
||
4 years ago
|
func (g *defaultGenerator) GenSvc(ctx DirContext, _ parser.Proto) error {
|
||
|
dir := ctx.GetSvc()
|
||
|
fileName := filepath.Join(dir.Filename, formatFilename("service_context")+".go")
|
||
4 years ago
|
text, err := util.LoadTemplate(category, svcTemplateFile, svcTemplate)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return util.With("svc").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
|
||
4 years ago
|
"imports": fmt.Sprintf(`"%v"`, ctx.GetConfig().Package),
|
||
4 years ago
|
}, fileName, false)
|
||
|
}
|