/
main.go
155 lines (139 loc) · 3.12 KB
/
main.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
package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"text/template"
_ "github.com/pipeproxy/pipe/init"
"github.com/pipeproxy/pipe/components/common/types"
"github.com/wzshiming/logger"
)
//go:generate ../../bin/go-bindata --nomemcopy --pkg main -o template.go ./template.go.tpl
func main() {
for _, g := range types.Global {
t := reflect.TypeOf(g).Elem()
g := newGen(t)
err := g.Write()
if err != nil {
logger.Log.Error(err, "write file")
}
}
}
func newGen(typ reflect.Type) *gen {
return &gen{typ: typ}
}
type gen struct {
typ reflect.Type
imports map[string]string
}
func (g *gen) data() interface{} {
imports := map[string]struct{}{}
dep := []string{
"github.com/pipeproxy/pipe/components/common/register",
"github.com/wzshiming/logger",
"github.com/pipeproxy/pipe/internal/ctxcache",
"context",
}
for _, d := range dep {
imports[d] = struct{}{}
}
methods := []interface{}{}
pkgPath := g.typ.PkgPath()
pkg := getImportName(pkgPath)
typName := g.typ.Name()
imports[pkgPath] = struct{}{}
j := g.typ.NumMethod()
for i := 0; i != j; i++ {
m := g.typ.Method(i)
args := []interface{}{}
inNum := m.Type.NumIn()
for i := 0; i != inNum; i++ {
typ := m.Type.In(i)
imports[typ.PkgPath()] = struct{}{}
name := strings.ToLower(typ.Name())
if name == "" {
name = string([]byte{'a' + byte(i)})
}
t := typ.String()
m := map[string]string{
"Name": name,
"Type": t,
}
args = append(args, m)
}
results := []interface{}{}
outNum := m.Type.NumOut()
for i := 0; i != outNum; i++ {
typ := m.Type.Out(i)
imports[typ.PkgPath()] = struct{}{}
name := strings.ToLower(typ.Name())
t := typ.String()
m := map[string]string{
"Name": name,
"Type": t,
}
if t == "error" {
imports["fmt"] = struct{}{}
m["Value"] = fmt.Sprintf(`fmt.Errorf("error %s.%s is none")`, pkg, typName)
}
results = append(results, m)
}
methods = append(methods, map[string]interface{}{
"FuncName": m.Name,
"Args": args,
"Results": results,
})
}
imp := []map[string]string{}
for i := range imports {
if i == "" {
continue
}
im := map[string]string{
"PkgPath": i,
"Alias": "",
}
//if strings.Count(i, "/") > 1 {
// im["Alias"] = getImportName(i)
//}
imp = append(imp, im)
}
return map[string]interface{}{
"Type": typName,
"PkgName": strings.ToLower(typName),
"Pkg": pkg,
"Imports": imp,
"Methods": methods,
}
}
func getImportName(p string) string {
p = filepath.Base(p)
p = strings.SplitN(p, ".", 2)[0]
return p
}
func (g *gen) Write() error {
buf := bytes.NewBuffer(nil)
data := g.data()
err := temp.Execute(buf, data)
if err != nil {
return err
}
src := buf.Bytes()
newSrc, err := format.Source(src)
if err == nil {
src = newSrc
} else {
logger.Log.Error(err, "format source code")
}
file := filepath.Join(g.typ.PkgPath(), strings.ToLower(g.typ.Name()), "init.go")
os.MkdirAll(filepath.Dir(file), 0755)
return ioutil.WriteFile(file, src, 0644)
}
var (
temp = template.Must(template.New("_").Parse(string(MustAsset("template.go.tpl"))))
)