-
Notifications
You must be signed in to change notification settings - Fork 7
/
template_models.go
205 lines (165 loc) · 4.87 KB
/
template_models.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2019 Twitch Interactive, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the License is
// located at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file. This file is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"fmt"
"strings"
)
// This file contains structs used in the wrapper generating templates.
// TypeMetadata stores metadata about a Go type.
type TypeMetadata struct {
// Name of the package this type is defined in.
PackageName string
// Path to the package this type is defined in.
PackagePath string
// Holds type information about this type
TypeInfo TypeInfo
// Imports of this type from all the methods
Imports []Import
// Methods of this type
Methods []Method
}
// Method represents a method function on a type
type Method struct {
// The name of the method on the type
Name string
// Input params
Params []TypeInfo
// Return results
Results []TypeInfo
// Whether this method is variadic
Variadic bool
}
// TypeInfo stores the name and whether it is an interface
type TypeInfo struct {
// The name of the type with or without the package qualifier. The qualifier is set appropriately
// Examples:
//
// "aws.Context" or "Context"
// "error would be "error"
Name string
// The name of the type without the package qualifier. Ex. "aws.Context" would be "Context"
NameWithoutQualifier string
// Whether this type is an interface
IsInterface bool
}
// Import represents a package import
type Import struct {
// ex. "github.com/aws/aws-sdk-go/service/dynamodb"
Path string
}
// ParamsSignature generates the signature for the methods params
// ex. "r0 aws.Context, r1 *dynamodb.BatchGetItemInput"
func (m Method) ParamsSignature(overrides ...string) string {
s := ""
mt := m.Params
l := len(mt)
for i := 0; i < l; i++ {
varName := fmt.Sprintf("p%d", i)
if i < len(overrides) {
varName = overrides[i]
}
if i == l-1 && m.Variadic {
const sliceChars = "[]"
// the qualfied name contains the "[]" at the beginning, so chop it off
s += fmt.Sprintf("%s ...%s", varName, mt[i].Name[len(sliceChars):])
} else {
s += fmt.Sprintf("%s %s", varName, mt[i].Name)
if i < l-1 {
s += ", "
}
}
}
return s
}
// CallSignatureWithClosure generates the signature for calling the embedded interface with a closure
func (m Method) CallSignatureWithClosure() string {
s := ""
mt := m.Params
l := len(mt)
for i := 0; i < l; i++ {
if i == l-1 && m.Variadic {
s += fmt.Sprintf("p%d...", i)
} else {
if i == 0 {
s += "ctx"
} else {
s += fmt.Sprintf("p%d", i)
}
if i < l-1 {
s += ", "
}
}
}
return s
}
// ResultsSignature generates the signature for the methods results
// ex. "(*dynamodb.BatchGetItemOutput, error)"
func (m Method) ResultsSignature() string {
mt := m.Results
if len(mt) == 1 {
return mt[0].Name
}
s := "("
l := len(mt)
for i := 0; i < l; i++ {
if i == l-1 {
s += mt[i].Name
} else {
s += mt[i].Name + ", "
}
}
return s + ")"
}
// ResultsClosureVariableDeclarations generates the variable declarations needed for making a call with a closure. These variables are needed
// outside the function scope (ex. (*circuit.Circuit).Run()).
// ex. "var r0 *dynamodb.UpdateItemInput
func (m Method) ResultsClosureVariableDeclarations() string {
s := ""
for i, t := range m.Results[:len(m.Results)-1] {
s += fmt.Sprintf("var r%d %s\n", i, t.Name)
}
return s
}
// HasOneMethodResultVariable returns whether there is exactly one return value
func (m Method) HasOneMethodResultVariable() bool {
return len(m.Results) == 1
}
// ResultsCircuitVariableAssignments generates the variable names needed when assigning the embedded interface method call.
// ex. "r0, err"
func (m Method) ResultsCircuitVariableAssignments() string {
s := ""
for i := range m.Results[:len(m.Results)-1] {
s += fmt.Sprintf("r%d, ", i)
}
s += "err"
return s
}
// ResultsClosureVariableReturns generates the variable names needed when returning the results of a closure wrapped method call.
// ex. "r0, err"
func (m Method) ResultsClosureVariableReturns() string {
s := ""
for i := range m.Results[:len(m.Results)-1] {
s += fmt.Sprintf("r%d, ", i)
}
return s
}
// IsWrappingSupported returns true only if the method supports context and returns an error.
func (m Method) IsWrappingSupported() bool {
if len(m.Params) == 0 || len(m.Results) == 0 {
return false
}
supportsContext := strings.HasSuffix(m.Params[0].Name, "Context")
returnsAnError := m.Results[len(m.Results)-1].Name == "error"
return supportsContext && returnsAnError
}