-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_code.go
139 lines (118 loc) · 3.54 KB
/
generate_code.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package autogql
import (
_ "embed"
"log"
"path"
"github.com/99designs/gqlgen/codegen"
"github.com/99designs/gqlgen/codegen/templates"
"github.com/Masterminds/sprig/v3"
)
func (ggs *AutoGqlPlugin) GenerateCode(data *codegen.Data) error {
err := ggs.generateResolverCode(data)
if err != nil {
return err
}
err = ggs.generateExtraFunctionsToModels(data)
if err != nil {
return err
}
err = ggs.generateFilterModelFunctions(data)
if err != nil {
return err
}
err = ggs.generateHookCode(data)
if err != nil {
return err
}
return ggs.generateDbCode(data)
}
//go:embed generate_code_resolver.go.tpl
var generateResolverCodeTemplate string
//go:embed generate_code_db.go.tpl
var generateDbCodeTemplate string
//go:embed generate_code_db_hook.go.tpl
var generateHookCodeTemplate string
//go:embed generate_code_model.go.tpl
var generateModelCodeTemplate string
//go:embed generate_code_filter_model.go.tpl
var generateFilterModelCodeTemplate string
func (ggs AutoGqlPlugin) generateDbCode(data *codegen.Data) error {
filename := path.Join(data.Config.Resolver.Package, "db/db_gen.go")
log.Println("generateDbCode", filename)
return templates.Render(templates.Options{
PackageName: "db",
Filename: filename,
Data: &GenerateData{
Data: data,
Handler: ggs.Handler,
},
GeneratedHeader: true,
Packages: data.Config.Packages,
Template: generateDbCodeTemplate,
Funcs: sprig.FuncMap(),
})
}
func (ggs AutoGqlPlugin) generateHookCode(data *codegen.Data) error {
filename := path.Join(data.Config.Resolver.Package, "db/hook_gen.go")
log.Println("generateHookCode", filename)
return templates.Render(templates.Options{
PackageName: "db",
Filename: filename,
Data: &GenerateData{
Data: data,
Handler: ggs.Handler,
},
GeneratedHeader: true,
Packages: data.Config.Packages,
Template: generateHookCodeTemplate,
Funcs: sprig.FuncMap(),
})
}
func (ggs AutoGqlPlugin) generateExtraFunctionsToModels(data *codegen.Data) error {
filename := path.Join(data.Config.Resolver.Package, data.Config.Model.Package, "models_autogql.go")
log.Println("generateExtraFunctionsToModels", filename)
return templates.Render(templates.Options{
PackageName: "model",
Filename: filename,
Data: &GenerateData{
Data: data,
Handler: ggs.Handler,
},
GeneratedHeader: true,
Packages: data.Config.Packages,
Template: generateModelCodeTemplate,
Funcs: sprig.FuncMap(),
})
}
func (ggs AutoGqlPlugin) generateFilterModelFunctions(data *codegen.Data) error {
filename := path.Join(data.Config.Resolver.Package, data.Config.Model.Package, "models_filter_autogql.go")
log.Println("generateFilterModelFunctions", filename)
return templates.Render(templates.Options{
PackageName: "model",
Filename: filename,
Data: &GenerateData{
Data: data,
Handler: ggs.Handler,
},
GeneratedHeader: true,
Packages: data.Config.Packages,
Template: generateFilterModelCodeTemplate,
Funcs: sprig.FuncMap(),
})
}
func (ggs *AutoGqlPlugin) generateResolverCode(data *codegen.Data) error {
filename := path.Join(data.Config.Resolver.Package, "autogql.resolvers.go")
log.Println("generateResolverCode", filename)
return templates.Render(templates.Options{
PackageName: "graph",
Filename: filename,
Data: &GenerateData{
Data: data,
Handler: ggs.Handler,
},
GeneratedHeader: true,
Packages: data.Config.Packages,
Template: generateResolverCodeTemplate,
Funcs: sprig.FuncMap(),
})
}