-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
123 lines (108 loc) · 2.45 KB
/
template.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
package main
// offsets
// 1: package name
// 2: type
const headerstart = `// Code generated by lesiw.io/moxie. DO NOT EDIT.
package %[1]s
import()
var _%[2]s = new(sync.Map)
type _%[2]sData struct {
mutex sync.Mutex
once sync.Once
`
// offsets
// 1: type
// 2: method name
// 3: method signature
const funcinfo = ` %[2]sMocks []func%[3]s
%[2]sCalls []_%[1]s_%[2]s_Call
`
// offsets
// 1: type
//
//nolint:lll
const headerend = `}
func _%[1]sPtrData(t *%[1]s) *_%[1]sData {
ptr := uintptr(unsafe.Pointer(t))
val, loaded := _%[1]s.LoadOrStore(ptr, new(_%[1]sData))
if !loaded {
val.(*_%[1]sData).once.Do(func() { runtime.SetFinalizer(t, func(_ *%[1]s) { _%[1]s.Delete(ptr) })})
}
return val.(*_%[1]sData)
}
`
// offsets
// 1: type
// 2: method name
// 3: structified parameters
const calltype = `type _%[1]s_%[2]s_Call struct {%[3]s}
`
// offsets
// 1: type
// 2: method name
// 3: method signature
// 4: arguments
// 5: origin type
// 6: call arguments
// 7: parameter types
// 8: result parameters
// 9: result types
// 10: result arguments
// 11: "return " if method has return values
//
//nolint:lll
const fn = `
func (_recv *%[1]s) %[3]s {
if _recv == nil {
panic("%[1]s.%[2]s: nil pointer receiver")
}
_dat := _%[1]sPtrData(_recv)
_dat.mutex.Lock()
_dat.%[2]sCalls = append(_dat.%[2]sCalls, _%[1]s_%[2]s_Call{%[6]s})
_fn := _recv.%[5]s.%[2]s
if len(_dat.%[2]sMocks) > 0 {
_fn = _dat.%[2]sMocks[0]
if len(_dat.%[2]sMocks) > 1 {
_dat.%[2]sMocks = _dat.%[2]sMocks[1:]
}
}
_dat.mutex.Unlock()
%[11]s_fn(%[4]s)
}
func (_recv *%[1]s) _%[2]s_Do(fn func(%[7]s) (%[9]s)) {
if _recv == nil {
panic("%[1]s.%[2]s: nil pointer receiver")
}
_dat := _%[1]sPtrData(_recv)
defer _dat.mutex.Unlock()
_dat.mutex.Lock()
if fn == nil {
_dat.%[2]sMocks = []func(%[7]s) (%[9]s){}
} else if len(_dat.%[2]sMocks) < 2 {
_dat.%[2]sMocks = []func(%[7]s) (%[9]s){fn, fn}
} else {
_dat.%[2]sMocks = _dat.%[2]sMocks[:len(_dat.%[2]sMocks)-1]
_dat.%[2]sMocks = append(_dat.%[2]sMocks, fn)
_dat.%[2]sMocks = append(_dat.%[2]sMocks, fn)
}
}
func (_recv *%[1]s) _%[2]s_Stub() {
_recv._%[2]s_Do(func(%[7]s) (%[8]s) {
return %[10]s
})
}
func (_recv *%[1]s) _%[2]s_Return(%[8]s) {
_recv._%[2]s_Do(func(%[7]s) (%[9]s) {
return %[10]s
})
}
func (_recv *%[1]s) _%[2]s_Calls() []_%[1]s_%[2]s_Call {
if _recv == nil {
panic("%[1]s.%[2]s: nil pointer receiver")
}
_dat := _%[1]sPtrData(_recv)
defer _dat.mutex.Unlock()
_dat.mutex.Lock()
return _dat.%[2]sCalls
}
`