-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
176 lines (153 loc) · 5.07 KB
/
format.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
package main
import (
"fmt"
"go/ast"
"log"
"strings"
)
// FormatMode is used by formatting helpers
type FormatMode int
const (
// Formatting options
NeedName FormatMode = 1 << iota // Include name in result (some formatters will gen the name, others will error)
NeedType // NeedType ensures that the type of a field in included in the return
NeedError // NeedError adds `err` | `err error` to the end of the returned string
)
// FormatReceiver builds a string from the given ast receiver. NeedName
// should always be set.
func FormatReceiver(fieldList *ast.FieldList, mode FormatMode) string {
if fieldList == nil {
return ""
}
if len(fieldList.List) != 1 {
log.Fatalf("FormatReceiver: field list should only have 1 field")
}
var name string
if mode&NeedName != 0 {
if fieldList.List[0].Names == nil || len(fieldList.List[0].Names) == 0 {
log.Fatalf("FormatReceiver: NeedName but no name for receiver")
} else {
name = fieldList.List[0].Names[0].Name
}
}
var typ string
if mode&NeedType != 0 {
if fieldList.List[0].Type == nil {
log.Fatalf("FormatReceiver: NeedType but no type for receiver")
} else {
typ = formatType(fieldList.List[0].Type)
}
}
// Return a period after the name if we don't need type
// becuase this is probably being used for a method call (and not declaration)
var ret string
if mode&NeedType != 0 {
ret = fmt.Sprintf("(%s %s)", name, typ)
} else {
ret = fmt.Sprintf("%s.", name)
}
return strings.TrimSpace(ret)
}
// FormatParams builds a string from the given ast params. NeedError does not
// apply to this function. NeedName behaves like NeedType.
func FormatParams(fieldList *ast.FieldList, mode FormatMode) string {
if fieldList == nil {
return ""
}
var params []string
for _, field := range fieldList.List {
// Create a name variable. If NeedName is set then we need to
// error if we don't have a name.
var name string
if mode&NeedName != 0 {
if field.Names == nil || len(field.Names) == 0 {
log.Fatalf("FormatParams: NeedName but no name for param")
} else {
name = field.Names[0].Name
}
}
// Create a type variable. If NeedType is set then we need to
// error if we don't have a type.
var typ string
if mode&NeedType != 0 {
if field.Type == nil {
log.Fatalf("FormatParams: NeedType but no type for param")
} else {
typ = formatType(field.Type)
}
}
// Join the name & type together. We then trim any whitespace from the
// result to ensure that we don't have any extra spaces (like when only name is set)
param := fmt.Sprintf("%s %s", name, typ)
params = append(params, strings.TrimSpace(param))
}
return strings.Join(params, ", ")
}
// FormatResults builds a string from the given ast results. NeedName is
// handled differently than most other formatters. If NeedName is set
// a name can be generated, but if it is not set, the name still a part of
// the return if the field had it set. FormatResults also always skips the
// last field. It is assumed that the last field is the error.
func FormatResults(fieldList *ast.FieldList, mode FormatMode) string {
if fieldList == nil {
return ""
}
var results []string
for i, field := range fieldList.List {
if i == len(fieldList.List)-1 {
continue
}
// Create a name variable. If NeedName mode is set then we are allowed
// to generate a name, as this will most likely be used as part of an assignment
// call or return statement. Even if NeedName isn't set, we still return the
// name if the original field had one.
var name string
if mode&NeedName != 0 {
if field.Names == nil || len(field.Names) == 0 {
// Generate a name since we don't have one & we need one
field.Names = []*ast.Ident{ast.NewIdent(fmt.Sprintf("r%d", i))}
}
}
// We also want to always use the name of the field if it was set
if field.Names != nil && len(field.Names) > 0 {
name = field.Names[0].Name
}
// Create a type variable. If NeedType mode is set then we need to
// error if we don't have a type.
var typ string
if mode&NeedType != 0 {
if field.Type == nil {
log.Fatalf("FormatResults: NeedType but no type for result %d", i)
} else {
typ = formatType(field.Type)
}
}
// Join the name & type together. We then trim any whitespace from the
// result to ensure that we don't have any extra spaces (like when only name is set)
result := fmt.Sprintf("%s %s", name, typ)
results = append(results, strings.TrimSpace(result))
}
// If NeedError is set then we need to add an error return
if mode&NeedError != 0 {
if mode&NeedType != 0 {
results = append(results, "err error")
} else {
results = append(results, "err")
}
}
return strings.Join(results, ", ")
}
// formatType is a helper to format a type from a field
func formatType(typ ast.Expr) string {
switch t := typ.(type) {
case *ast.StarExpr:
return fmt.Sprintf("*%s", t.X)
case *ast.Ident:
return t.Name
case *ast.Ellipsis:
return fmt.Sprintf("...%s", t.Elt)
default:
log.Fatalf("formatFieldType: unknown type %T", t)
}
return "" // not reached but compiler complains
}