-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
56 lines (47 loc) · 1.33 KB
/
templates.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
package schema
import (
"text/template"
"github.com/cihangir/stringext"
)
// Helpers holds helpers for templates
var Helpers = template.FuncMap{
"AsComment": stringext.AsComment,
"JSONTagWithIgnored": stringext.JSONTagWithIgnored,
"goType": goType,
"GenerateValidator": generateValidator,
"ToLowerFirst": stringext.ToLowerFirst,
"ToUpperFirst": stringext.ToUpperFirst,
"DepunctWithInitialUpper": stringext.DepunctWithInitialUpper,
"DepunctWithInitialLower": stringext.DepunctWithInitialLower,
}
func generateValidator(s *Schema) string {
return ""
}
var templates *template.Template
func init() {
templates = template.New("package.tmpl").Funcs(Helpers)
templates = template.Must(Parse(templates))
}
var tmpls = map[string]string{"field.tmpl": `
{{AsComment .Definition.Description}}
{{DepunctWithInitialUpper .Name}} {{.Type}} {{JSONTagWithIgnored .Name .Required .Definition.Private .Type .Definition.Tags}}
`,
}
// Parse parses declared templates.
func Parse(t *template.Template) (*template.Template, error) {
for name, s := range tmpls {
if t == nil {
t = template.New(name)
}
var tmpl *template.Template
if name == t.Name() {
tmpl = t
} else {
tmpl = t.New(name)
}
if _, err := tmpl.Parse(s); err != nil {
return nil, err
}
}
return t, nil
}