-
Notifications
You must be signed in to change notification settings - Fork 93
/
proxy.go
171 lines (157 loc) · 4.32 KB
/
proxy.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
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2017-2019 Massimiliano Ghilardi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* proxy.go
*
* Created on Mar 06, 2017
* Author Massimiliano Ghilardi
*/
package genimport
import (
"fmt"
"go/types"
"github.com/cosmos72/gomacro/base/paths"
)
type writeTypeOpts int
const (
writeMethodsAsFields writeTypeOpts = 1 << iota
writeForceParamNames
writeIncludeParamTypes
writeLastParamIsVariadic
)
func lastParamIsVariadic(variadic bool) writeTypeOpts {
if variadic {
return writeLastParamIsVariadic
}
return 0
}
func (gen *genimport) writeInterfaceProxy(pkgPath string, name string, t *types.Interface) {
fmt.Fprintf(gen.out, "\n// --------------- proxy for %s.%s ---------------\ntype %s%s struct {", pkgPath, name, gen.proxyprefix, name)
gen.writeInterfaceMethods(name, t, writeMethodsAsFields)
gen.out.WriteString("\n}\n")
gen.writeInterfaceMethods(name, t, writeForceParamNames)
}
func (gen *genimport) writeInterfaceMethods(name string, t *types.Interface, opts writeTypeOpts) {
if opts&writeMethodsAsFields != 0 {
fmt.Fprint(gen.out, "\n\tObject\tinterface{}") // will be used to retrieve object wrapped in the proxy
}
n := t.NumMethods()
for i := 0; i < n; i++ {
gen.writeInterfaceMethod(name, t.Method(i), opts)
}
}
func (gen *genimport) writeInterfaceMethod(interfaceName string, method *types.Func, opts writeTypeOpts) {
if !method.Exported() {
return
}
sig, ok := method.Type().(*types.Signature)
if !ok {
return
}
out := gen.out
params := sig.Params()
if opts&writeMethodsAsFields != 0 {
var param0 string
if opts&writeForceParamNames != 0 || isNamedTypeTuple(params) {
param0 = "_proxy_obj_ "
}
fmt.Fprintf(out, "\n\t%s_\tfunc(%sinterface{}", method.Name(), param0)
if params != nil && params.Len() != 0 {
out.WriteString(", ")
}
} else {
fmt.Fprintf(out, "func (P *%s%s) %s(", gen.proxyprefix, interfaceName, method.Name())
}
variadic := lastParamIsVariadic(sig.Variadic())
gen.writeTypeTuple(params, opts|variadic|writeIncludeParamTypes)
out.WriteString(") ")
results := sig.Results()
gen.writeTypeTupleOut(results)
if opts&writeMethodsAsFields != 0 {
return
}
out.WriteString(" {\n\t")
if results != nil && results.Len() > 0 {
out.WriteString("return ")
}
fmt.Fprintf(out, "P.%s_(P.Object", method.Name())
if params != nil && params.Len() != 0 {
out.WriteString(", ")
}
gen.writeTypeTuple(params, variadic|writeForceParamNames)
out.WriteString(")\n}\n")
}
func isNamedTypeTuple(tuple *types.Tuple) bool {
if tuple == nil || tuple.Len() == 0 {
return false
}
for i, n := 0, tuple.Len(); i < n; i++ {
if len(tuple.At(i).Name()) != 0 {
return true
}
}
return false
}
func (gen *genimport) writeTypeTupleOut(tuple *types.Tuple) {
if tuple == nil || tuple.Len() == 0 {
return
}
out := gen.out
ret0 := tuple.At(0)
if tuple.Len() > 1 || len(ret0.Name()) > 0 {
out.WriteString("(")
gen.writeTypeTuple(tuple, writeIncludeParamTypes)
out.WriteString(")")
} else {
types.WriteType(out, ret0.Type(), gen.packageNameQualifier)
}
}
func (gen *genimport) writeTypeTuple(tuple *types.Tuple, opts writeTypeOpts) {
opt := opts &^ writeLastParamIsVariadic
n := tuple.Len()
for i := 0; i < n; i++ {
if i != 0 {
gen.out.WriteString(", ")
}
if i == n-1 && opt != opts {
opt = opts // restore 'variadic' flag
}
gen.writeTypeVar(tuple.At(i), i, opt)
}
}
func (gen *genimport) writeTypeVar(v *types.Var, index int, opts writeTypeOpts) {
name := v.Name()
if len(name) == 0 && opts&writeForceParamNames != 0 {
name = fmt.Sprintf("unnamed%d", index)
}
out := gen.out
out.WriteString(name)
if opts&writeIncludeParamTypes != 0 {
if len(name) != 0 {
out.WriteString(" ")
}
t := v.Type()
if opts&writeLastParamIsVariadic != 0 {
out.WriteString("...")
t = t.(*types.Slice).Elem()
}
types.WriteType(out, t, gen.packageNameQualifier)
} else if len(name) != 0 && opts&writeLastParamIsVariadic != 0 {
out.WriteString("...")
}
}
func (gen *genimport) packageNameQualifier(pkg *types.Package) string {
path := pkg.Path()
name, ok := gen.pkgrenames[path]
if !ok {
name = paths.FileName(path)
}
return name
}