-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
106 lines (95 loc) · 2.9 KB
/
generator.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
package main
import (
"bytes"
"fmt"
"go/format"
"path/filepath"
"strconv"
"strings"
"text/template"
"github.com/stoewer/go-strcase"
)
type Generator struct {
c *Config // The configure
t *template.Template // The template instance which set the functions and parsed files
}
func NewGenerator(c *Config) *Generator {
return &Generator{
c: c,
}
}
// LoadTemplate loads the template file.
func (g *Generator) LoadTemplate() error {
templateFile := g.c.templateFile
basename := filepath.Base(templateFile)
t, err := template.New(basename).Funcs(g.funcMap()).ParseFiles(templateFile)
if err != nil {
return fmt.Errorf("can't parse template file %s. err:%w", templateFile, err)
}
g.t = t
return nil
}
func (g *Generator) Generate(p *Parser) ([]byte, error) {
resultList := p.ResultList()
templateData := map[string]interface{}{
"DoNotEdit": fmt.Sprintf("// DO NOT EDIT.; Code generated by \"constconv %s\"", g.c.execArgsStr),
"Extra": g.c.extraData,
"BasePackageName": p.BasePackageName(),
"Result": resultList[0], // syntax sugar for the first element of the resultList.
"ResultList": resultList,
}
var buf bytes.Buffer
if err := g.t.Execute(&buf, templateData); err != nil {
return nil, fmt.Errorf("template execution failed: %w", err)
}
return g.format(buf)
}
// format returns the gofmt-ed contents of the Generator's buffer.
func (g *Generator) format(buf bytes.Buffer) ([]byte, error) {
body := buf.Bytes()
src, err := format.Source(body)
if err != nil {
return body, fmt.Errorf("format error: %w\n=====BEGIN CURRENT CODE=====\n%s\n=====END CURRENT CODE=====\n", err, body)
}
return src, nil
}
// funcMap returns the template function map.
func (g *Generator) funcMap() template.FuncMap {
return template.FuncMap{
"SnakeCase": strcase.SnakeCase,
"KebabCase": strcase.KebabCase,
"LowerCamelCase": strcase.LowerCamelCase,
"UpperSnakeCase": strcase.UpperSnakeCase,
"UpperKebabCase": strcase.UpperKebabCase,
"UpperCamelCase": strcase.UpperCamelCase,
"HasPrefix": strings.HasPrefix,
"HasSuffix": strings.HasSuffix,
"Contains": strings.Contains,
"Title": strings.Title,
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
"TrimSpace": strings.TrimSpace,
"TrimPrefix": strings.TrimPrefix,
"TrimSuffix": strings.TrimSuffix,
"Trim": strings.Trim,
"TrimLeft": strings.TrimLeft,
"TrimRight": strings.TrimRight,
"Quote": strconv.Quote,
"Unquote": func(s string) string {
ret, err := strconv.Unquote(s)
if err != nil {
return s
}
return ret
},
"DropDot": func (s string) string {
return strings.ReplaceAll(s, ".", "")
},
"DropUnderscore": func (s string) string {
return strings.ReplaceAll(s, "_", "")
},
"DropHyphen": func (s string) string {
return strings.ReplaceAll(s, "-", "")
},
}
}