-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
117 lines (99 loc) · 3.13 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
107
108
109
110
111
112
113
114
115
116
117
package mocky
import (
"bytes"
"fmt"
"io"
"text/template"
"unicode"
"golang.org/x/tools/imports"
)
type Interface struct {
PackageName string
Name string
Methods []Method
}
type Method struct {
Name string
Args []Argument
Returns []Argument
}
type Argument struct {
Name string
Type string
}
func Generate(wtr io.Writer, c Interface) error {
funcs := template.FuncMap{
"IsNotLastArgument": func(vs []Argument, i int) bool {
return i < len(vs)-1
},
"Untitle": func(s string) string {
return titleCasing(s, unicode.ToLower)
},
"Title": func(s string) string {
return titleCasing(s, unicode.ToUpper)
},
}
tmpl, err := template.New("mocky").Funcs(funcs).Parse(mockTemplate)
if err != nil {
return err
}
buf := bytes.NewBuffer(nil)
err = tmpl.Execute(buf, c)
if err != nil {
return err
}
bs, err := imports.Process("", buf.Bytes(), nil)
if err != nil {
return fmt.Errorf("failed to format generated code: %w", err)
}
tot := 0
for tot < len(bs) {
n, err := wtr.Write(bs)
if err != nil {
return err
}
tot += n
}
return nil
}
// titleCasing uses runeCaser to set the casing of the first letter of s.
func titleCasing(s string, runeCaser func(rune) rune) string {
if len(s) == 0 {
return s
}
r := rune(s[0])
if !unicode.IsUpper(r) && !unicode.IsLower(r) {
return s
}
return string(runeCaser(r)) + s[1:]
}
var mockTemplate = `package {{ .PackageName }}
{{ $iname := .Name }}
type Mock{{.Name}} struct {
{{ range $method := .Methods }}{{ $method.Name }}Mock func({{ range $arg := $method.Args }}{{$arg.Name}} {{$arg.Type}},{{ end }})({{ range $ret := $method.Returns }}{{$ret.Name}} {{$ret.Type}}, {{ end }})
{{ $method.Name }}Calls []{{ $iname | Untitle }}{{ $method.Name }}Call
{{ end }}
}
{{ range $method := .Methods }}
type {{ $iname | Untitle }}{{ $method.Name }}Call struct {
{{ range $arg := $method.Args }}{{ $arg.Name | Title }} {{$arg.Type}}
{{ end }}
{{ range $i, $arg := $method.Returns }}Out{{$i}} {{$arg.Type}}
{{ end }}
}
func (_v *Mock{{$iname}}) {{ $method.Name }}({{ range $arg := $method.Args }}{{$arg.Name}} {{$arg.Type}},{{ end }})({{ range $ret := $method.Returns }}{{$ret.Name}} {{$ret.Type}}, {{ end }}) {
if _v.{{$method.Name}}Mock == nil {
msg := fmt.Sprintf("call to %T.{{$method.Name}}, but Mock{{$method.Name}} is not set", _v)
panic(msg)
}
_v.{{ $method.Name }}Calls = append(_v.{{ $method.Name }}Calls, {{ $iname | Untitle }}{{ $method.Name }}Call{
{{ range $i, $arg:= $method.Args }}{{ $arg.Name | Title }}: {{$arg.Name}},
{{ end }}
})
{{ if $method.Returns}}{{ range $i, $arg := $method.Returns}}out{{ $i }}{{ if IsNotLastArgument $method.Returns $i }},{{ end }}{{ end }} := {{ else }}{{ end }}_v.{{ $method.Name }}Mock({{ range $arg := $method.Args }}{{$arg.Name}}, {{ end }})
{{ range $i, $arg := $method.Returns}}_v.{{ $method.Name }}Calls[len(_v.{{ $method.Name }}Calls)-1].Out{{ $i }} = out{{ $i }}{{ if IsNotLastArgument $method.Returns $i }}
{{else}}{{end}}{{ end }}
{{ if $method.Returns}}return {{ range $i, $arg := $method.Returns}}out{{ $i }}{{ if IsNotLastArgument $method.Returns $i }},{{ end }}{{ end }}{{ end }}
}
{{ end }}
`