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 159 160 161 162 163 164 165 166 167 168 169 170 171 172
| package proxy
import ( "bytes" "fmt" "go/ast" "go/format" "go/parser" "go/token" "strings" "text/template" )
func generate(file string) (string, error) { fset := token.NewFileSet() f, err := parser.ParseFile(fset, file, nil, parser.ParseComments) if err != nil { return "", err }
data := proxyData{ Package: f.Name.Name, }
cmap := ast.NewCommentMap(fset, f, f.Comments) for node, group := range cmap { name := getProxyInterfaceName(group) if name == "" { continue }
data.ProxyStructName = node.(*ast.GenDecl).Specs[0].(*ast.TypeSpec).Name.Name
obj := f.Scope.Lookup(name)
t := obj.Decl.(*ast.TypeSpec).Type.(*ast.InterfaceType)
for _, field := range t.Methods.List { fc := field.Type.(*ast.FuncType)
method := &proxyMethod{ Name: field.Names[0].Name, }
method.Params, method.ParamNames = getParamsOrResults(fc.Params) method.Results, method.ResultNames = getParamsOrResults(fc.Results)
data.Methods = append(data.Methods, method) } }
tpl, err := template.New("").Parse(proxyTpl) if err != nil { return "", err }
buf := &bytes.Buffer{} if err := tpl.Execute(buf, data); err != nil { return "", err }
src, err := format.Source(buf.Bytes()) if err != nil { return "", err }
return string(src), nil }
func getParamsOrResults(fields *ast.FieldList) (string, string) { var ( params []string paramNames []string )
for i, param := range fields.List { var names []string for _, name := range param.Names { names = append(names, name.Name) }
if len(names) == 0 { names = append(names, fmt.Sprintf("r%d", i)) }
paramNames = append(paramNames, names...)
param := fmt.Sprintf("%s %s", strings.Join(names, ","), param.Type.(*ast.Ident).Name, ) params = append(params, strings.TrimSpace(param)) }
return strings.Join(params, ","), strings.Join(paramNames, ",") }
func getProxyInterfaceName(groups []*ast.CommentGroup) string { for _, commentGroup := range groups { for _, comment := range commentGroup.List { if strings.Contains(comment.Text, "@proxy") { interfaceName := strings.TrimLeft(comment.Text, "// @proxy ") return strings.TrimSpace(interfaceName) } } } return "" }
const proxyTpl = ` package {{.Package}}
type {{ .ProxyStructName }}Proxy struct { child *{{ .ProxyStructName }} }
func New{{ .ProxyStructName }}Proxy(child *{{ .ProxyStructName }}) *{{ .ProxyStructName }}Proxy { return &{{ .ProxyStructName }}Proxy{child: child} }
{{ range .Methods }} func (p *{{$.ProxyStructName}}Proxy) {{ .Name }} ({{ .Params }}) ({{ .Results }}) { // before 这里可能会有一些统计的逻辑 start := time.Now()
{{ .ResultNames }} = p.child.{{ .Name }}({{ .ParamNames }})
// after 这里可能也有一些监控统计的逻辑 log.Printf("user login cost time: %s", time.Now().Sub(start))
return {{ .ResultNames }} } {{ end }} `
type proxyData struct { Package string ProxyStructName string Methods []*proxyMethod }
type proxyMethod struct { Name string Params string ParamNames string Results string ResultNames string }
|