/
option_template.go
48 lines (37 loc) · 1007 Bytes
/
option_template.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
package factory
import (
"github.com/Hyingerrr/mirco-esim/pkg/templates"
)
var confOptionTemplate = `
func With{{.StructName}}Conf(conf config.Config) {{.StructName}}Option {
return func({{.StructName|snakeToCamelLower|shorten}} {{.ReturnVarStr}}) {
{{.StructName|snakeToCamelLower|shorten}}.conf = conf
}
}
`
var loggerOptionTemplate = `
func With{{.StructName}}Logger(logger log.Logger) {{.StructName}}Option {
return func({{.StructName|snakeToCamelLower|shorten}} {{.ReturnVarStr}}) {
{{.StructName|snakeToCamelLower|shorten}}.logger = logger
}
}
`
type optionTpl struct {
StructName string
ReturnVarStr string
tpl templates.Tpl
}
func newOptionTpl(tpl templates.Tpl) *optionTpl {
ot := &optionTpl{}
ot.tpl = tpl
return ot
}
func (ot *optionTpl) String(structName, returnVarStr, tplName, tpl string) string {
ot.StructName = structName
ot.ReturnVarStr = returnVarStr
content, err := ot.tpl.Execute(tplName, tpl, ot)
if err != nil {
panic(err.Error())
}
return content
}