-
Notifications
You must be signed in to change notification settings - Fork 0
/
gensvc.go
45 lines (38 loc) · 1.34 KB
/
gensvc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package generator
import (
_ "embed"
"fmt"
"path/filepath"
"strings"
conf "github.com/zhangbao138208/goctls/config"
"github.com/zhangbao138208/goctls/rpc/parser"
"github.com/zhangbao138208/goctls/util"
"github.com/zhangbao138208/goctls/util/format"
"github.com/zhangbao138208/goctls/util/pathx"
)
//go:embed svc.tpl
var svcTemplate string
// GenSvc generates the servicecontext.go file, which is the resource dependency of a service,
// such as rpc dependency, model dependency, etc.
func (g *Generator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Config, c *ZRpcContext) error {
dir := ctx.GetSvc()
svcFilename, err := format.FileNamingFormat(cfg.NamingFormat, "service_context")
if err != nil {
return err
}
fileName := filepath.Join(dir.Filename, svcFilename+".go")
text, err := pathx.LoadTemplate(category, svcTemplateFile, svcTemplate)
if err != nil {
return err
}
imports := strings.Builder{}
imports.WriteString(fmt.Sprintf("\t\"%v\"\n", ctx.GetConfig().Package))
if c.Ent {
imports.WriteString(fmt.Sprintf("\t\"%s/ent\"\n\n", ctx.GetMain().Package))
imports.WriteString("\t\"github.com/zeromicro/go-zero/core/logx\"\n\t\"github.com/zeromicro/go-zero/core/stores/redis\"\n")
}
return util.With("svc").GoFmt(true).Parse(text).SaveTo(map[string]any{
"imports": imports.String(),
"isEnt": c.Ent,
}, fileName, false)
}