-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
158 lines (139 loc) · 3.96 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package httptest
import (
"encoding/hex"
"fmt"
"path/filepath"
"strings"
"github.com/protogodev/httptest/client"
"github.com/protogodev/httptest/server"
protogocmd "github.com/protogodev/protogo/cmd"
"github.com/protogodev/protogo/generator"
"github.com/protogodev/protogo/parser"
"github.com/protogodev/protogo/parser/ifacetool"
)
func init() {
protogocmd.MustRegister(&protogocmd.Plugin{
Name: "httptest",
Cmd: protogocmd.NewGen(&Generator{}),
})
}
type Generator struct {
Mode string `name:"mode" required:"" enum:"server,client" help:"generation mode (server or client)"`
TestSpecFileName string `name:"spec" required:"" help:"the test specification in YAML"`
OutFileName string `name:"out" help:"output filename (default \"./<srcPkgName>_<mode>_test.go\")"`
Formatted bool `name:"fmt" default:"true" help:"whether to make the test code formatted"`
}
func (g *Generator) Generate(data *ifacetool.Data) (*generator.File, error) {
if g.OutFileName == "" {
g.OutFileName = fmt.Sprintf("./%s_%s_test.go", data.SrcPkgName, g.Mode)
}
var template string
var tmplData interface{}
switch g.Mode {
case "server":
spec, err := server.NewSpec(g.TestSpecFileName)
if err != nil {
return nil, err
}
tmplData = struct {
DstPkgName string
Data *ifacetool.Data
Spec *server.Spec
}{
DstPkgName: parser.PkgNameFromDir(filepath.Dir(g.OutFileName)),
Data: data,
Spec: spec,
}
template = server.Template
case "client":
spec, err := client.NewSpec(g.TestSpecFileName)
if err != nil {
return nil, err
}
tmplData = struct {
DstPkgName string
Data *ifacetool.Data
Spec *client.Spec
}{
DstPkgName: parser.PkgNameFromDir(filepath.Dir(g.OutFileName)),
Data: data,
Spec: spec,
}
template = client.Template
default:
panic(fmt.Errorf("bad mode: %s", g.Mode))
}
methodMap := make(map[string]*ifacetool.Method)
for _, method := range data.Methods {
methodMap[method.Name] = method
}
return generator.Generate(template, tmplData, generator.Options{
Funcs: map[string]interface{}{
"title": strings.Title,
"fmtArgCSV": func(csv string, format string) string {
if csv == "" {
return ""
}
sep := ", "
args := strings.Split(csv, sep)
var results []string
for _, a := range args {
r := strings.NewReplacer("$Name", a, ">Name", strings.Title(a))
results = append(results, r.Replace(format))
}
return strings.Join(results, sep)
},
"interfaceMethod": func(name string) *ifacetool.Method {
method, ok := methodMap[name]
if !ok {
return nil
}
return method
},
"goString": func(v interface{}) string {
return fmt.Sprintf("%#v", v)
},
"ctxParam": func(params []*ifacetool.Param) *ifacetool.Param {
for _, p := range params {
if p.TypeString == "context.Context" {
return p
}
}
return nil
},
"nonCtxParams": func(params []*ifacetool.Param) (out []*ifacetool.Param) {
for _, p := range params {
if p.TypeString != "context.Context" {
out = append(out, p)
}
}
return
},
"bodyToBytes": func(s string) string {
if s == "" {
// An empty string indicates a nil byte slice.
return "[]byte(nil)"
}
if strings.HasPrefix(s, "0x") {
// This is a hexadecimal string, decode it into bytes.
//
// Note that kun borrows the idea from eth2.0 to represent binary data
// as hex encoded strings, see https://github.com/ethereum/eth2.0-spec-tests/issues/5.
decoded, err := hex.DecodeString(s[2:])
if err != nil {
panic(err)
}
var hexes []string
for _, b := range decoded {
hexes = append(hexes, fmt.Sprintf("0x%x", b))
}
return fmt.Sprintf("[]byte{%s}", strings.Join(hexes, ", "))
}
// This is a normal string, leave it as is.
return fmt.Sprintf("[]byte(`%s`)", s)
},
},
Formatted: g.Formatted,
TargetFileName: g.OutFileName,
})
}