@@ -15,22 +15,60 @@ const (
15
15
optReflect = "reflect"
16
16
)
17
17
18
- // Target represents the target of the directive
18
+ var (
19
+ runFuncs = map [string ]runFunc {
20
+ "ptr" : ptr ,
21
+ "getter" : getter ,
22
+ "setter" : setter ,
23
+ "filter" : filter ,
24
+ "mapper" : mapper ,
25
+ "sort" : sort ,
26
+ "stringer" : stringer ,
27
+ "new" : runNew ,
28
+ "equal" : equal ,
29
+ }
30
+ )
31
+
32
+ // Target represents the target of the directive.
19
33
type Target struct {
20
34
MetaFile * meta.File
21
35
RcvName , RcvType string
22
36
FldNames []string
23
37
FldType string
24
38
}
25
39
26
- // Ptr converts the receiver to a pointer for all subsequent directives
27
- func Ptr (tgt * Target ) {
40
+ type runFunc func (* Target , []string )
41
+
42
+ // RunAll runs all of the given directives.
43
+ func RunAll (ds []string , tgt * Target ) {
44
+ for i := range ds {
45
+ Run (ds [i ], tgt )
46
+ }
47
+ }
48
+
49
+ // Run runs the given directive.
50
+ func Run (d string , tgt * Target ) {
51
+ opts := strings .Split (d , "," )
52
+ d = opts [0 ]
53
+ opts = opts [1 :]
54
+
55
+ run , ok := runFuncs [d ]
56
+ if ! ok {
57
+ log .Printf ("Unknown directive: %s\n " , d )
58
+ return
59
+ }
60
+
61
+ run (tgt , opts )
62
+ }
63
+
64
+ // ptr converts the receiver to a pointer for all subsequent directives.
65
+ func ptr (tgt * Target , opts []string ) {
28
66
tgt .RcvType = "*" + tgt .RcvType
29
67
log .Printf ("Using pointer receiver: %s\n " , tgt .RcvType )
30
68
}
31
69
32
- // Getter generates a getter method for each name of the given field
33
- func Getter (tgt * Target ) {
70
+ // getter generates a getter method for each name of the given field.
71
+ func getter (tgt * Target , opts [] string ) {
34
72
for _ , fldNm := range tgt .FldNames {
35
73
method := upperFirst (fldNm )
36
74
if method == fldNm {
@@ -50,8 +88,8 @@ func Getter(tgt *Target) {
50
88
}
51
89
}
52
90
53
- // Setter generates a setter method for each name of the given field
54
- func Setter (tgt * Target ) {
91
+ // setter generates a setter method for each name of the given field.
92
+ func setter (tgt * Target , opts [] string ) {
55
93
elemType := strings .TrimPrefix (tgt .FldType , "[]" )
56
94
57
95
arg := argName (tgt .RcvName , elemType )
@@ -78,8 +116,8 @@ func Setter(tgt *Target) {
78
116
}
79
117
}
80
118
81
- // Filter generates a filter method for each name of the given field
82
- func Filter (tgt * Target , opts []string ) {
119
+ // filter generates a filter method for each name of the given field.
120
+ func filter (tgt * Target , opts []string ) {
83
121
elemType := strings .TrimPrefix (tgt .FldType , "[]" )
84
122
85
123
var isOmitField bool
@@ -112,15 +150,23 @@ func Filter(tgt *Target, opts []string) {
112
150
}
113
151
}
114
152
115
- // Map generates a mapper method for each name of the given field
116
- func Map (tgt * Target , result string , opts []string ) {
117
- elemType := strings .TrimPrefix (tgt .FldType , "[]" )
153
+ // mapper generates a mapper method for each name of the given field.
154
+ func mapper (tgt * Target , opts []string ) {
155
+ if len (opts ) < 1 {
156
+ log .Print ("skipping 'mapper' - must specify target type as first option\n " )
157
+ return
158
+ }
159
+
160
+ result := opts [0 ]
161
+ opts = opts [1 :]
118
162
119
163
sel := result
120
164
if resSubs := strings .SplitN (result , "." , 2 ); len (resSubs ) > 1 {
121
165
sel = resSubs [1 ]
122
166
}
123
167
168
+ elemType := strings .TrimPrefix (tgt .FldType , "[]" )
169
+
124
170
var isOmitField bool
125
171
for i := range opts {
126
172
if opts [i ] == optOmitField {
@@ -130,11 +176,6 @@ func Map(tgt *Target, result string, opts []string) {
130
176
}
131
177
132
178
for _ , fldNm := range tgt .FldNames {
133
- if elemType == tgt .FldType {
134
- log .Printf ("'map' not valid for field %s.%s - must be a slice\n " , tgt .RcvName , fldNm )
135
- continue
136
- }
137
-
138
179
var fldPart string
139
180
if ! isOmitField {
140
181
fldPart = upperFirst (fldNm )
@@ -155,8 +196,8 @@ func Map(tgt *Target, result string, opts []string) {
155
196
}
156
197
}
157
198
158
- // Sort generates sort methods for the first name of the given field
159
- func Sort (tgt * Target , opts []string ) {
199
+ // sort generates sort methods for the first name of the given field.
200
+ func sort (tgt * Target , opts []string ) {
160
201
log .Print ("Adding import: \" sort\" \n " )
161
202
tgt .MetaFile .Imports ["sort" ] = struct {}{}
162
203
@@ -192,8 +233,8 @@ func Sort(tgt *Target, opts []string) {
192
233
}
193
234
}
194
235
195
- // Stringer adds each name of the given field to the String() implementation
196
- func Stringer (tgt * Target ) {
236
+ // stringer adds each name of the given field to the String() implementation.
237
+ func stringer (tgt * Target , opts [] string ) {
197
238
log .Print ("Adding import: \" fmt\" \n " )
198
239
tgt .MetaFile .Imports ["fmt" ] = struct {}{}
199
240
@@ -227,8 +268,8 @@ func Stringer(tgt *Target) {
227
268
}
228
269
}
229
270
230
- // New adds each name of the given field to the New() implementation
231
- func New (tgt * Target ) {
271
+ // runNew adds each name of the given field to the New() implementation.
272
+ func runNew (tgt * Target , opts [] string ) {
232
273
method := "New" + upperFirst (tgt .RcvType )
233
274
for _ , fldNm := range tgt .FldNames {
234
275
log .Printf ("Adding to method: %s\n " , method )
@@ -256,8 +297,8 @@ func New(tgt *Target) {
256
297
}
257
298
}
258
299
259
- // Equal adds each name of the given field to the Equal() implementation
260
- func Equal (tgt * Target , opts []string ) {
300
+ // equal adds each name of the given field to the Equal() implementation.
301
+ func equal (tgt * Target , opts []string ) {
261
302
for _ , fldNm := range tgt .FldNames {
262
303
log .Print ("Adding to method: Equal\n " )
263
304
found := tgt .MetaFile .FilterMethods (
0 commit comments