-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
213 lines (188 loc) · 4.94 KB
/
utils.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
206
207
208
209
210
211
212
213
package config
import (
"bytes"
"net/http"
"strings"
"github.com/hashicorp/go-retryablehttp"
"github.com/xanzy/go-gitlab"
"gitlab.com/tozd/go/errors"
)
const (
// See: https://docs.gitlab.com/ee/api/#offset-based-pagination
maxGitLabPageSize = 100
)
// downloadFile downloads a file from url URL.
func downloadFile(url string) ([]byte, errors.E) {
client, _ := gitlab.NewClient("")
req, err := retryablehttp.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.WithStack(err)
}
if client.UserAgent != "" {
req.Header.Set("User-Agent", client.UserAgent)
}
buffer := bytes.Buffer{}
// TODO: Handle errors better.
// On error this tries to parse the error response as API error, which fails for arbitrary HTTP requests.
_, err = client.Do(req, &buffer)
if err != nil {
return nil, errors.WithStack(err)
}
return buffer.Bytes(), nil
}
// renameAnyField renames field named "from" to "to" anywhere in the arbitrary input
// structure, even if it is nested inside other maps or slices.
func renameAnyField(input interface{}, from, to string) {
switch in := input.(type) {
case []interface{}:
for _, v := range in {
renameAnyField(v, from, to)
}
case []map[string]interface{}:
for _, v := range in {
renameAnyField(v, from, to)
}
case map[string]interface{}:
renameMapField(in, from, to)
}
}
// renameMapField renames field named "from" to "to" anywhere in the map input
// structure, even if it is nested inside other maps or slices.
func renameMapField(input map[string]interface{}, from, to string) {
for key, value := range input {
renameAnyField(value, from, to)
if key == from {
input[to] = value
delete(input, key)
}
}
}
// removeField removes field named "field" anywhere in the arbitrary input
// structure, even if it is nested inside other maps or slices.
func removeField(input interface{}, field string) {
switch in := input.(type) {
case []interface{}:
for _, v := range in {
removeField(v, field)
}
case []map[string]interface{}:
for _, v := range in {
removeField(v, field)
}
case map[string]interface{}:
for key, value := range in {
removeField(value, field)
if key == field {
delete(in, key)
}
}
}
}
// removeFieldSuffix removes suffix from field names anywhere in the arbitrary
// input structure, even if it is nested inside other maps or slices.
func removeFieldSuffix(input interface{}, suffix string) {
if suffix == "" {
return
}
switch in := input.(type) {
case []interface{}:
for _, v := range in {
removeFieldSuffix(v, suffix)
}
case []map[string]interface{}:
for _, v := range in {
removeFieldSuffix(v, suffix)
}
case map[string]interface{}:
for key, value := range in {
removeFieldSuffix(value, suffix)
if strings.HasSuffix(key, suffix) {
in[strings.TrimSuffix(key, suffix)] = value
delete(in, key)
}
}
}
}
// castFloatsToInts casts all floats to ints anywhere in the arbitrary
// input structure, even if it is nested inside other maps or slices.
func castFloatsToInts(input interface{}) {
switch in := input.(type) {
case []interface{}:
for i, v := range in {
switch n := v.(type) {
case float64:
in[i] = int(n)
default:
castFloatsToInts(v)
}
}
case []map[string]interface{}:
for _, v := range in {
castFloatsToInts(v)
}
case map[string]interface{}:
for key, value := range in {
switch n := value.(type) {
case float64:
in[key] = int(n)
default:
castFloatsToInts(value)
}
}
}
}
// describeKeys adds comments for all keys in obj found in descriptions.
func describeKeys(obj map[string]interface{}, descriptions map[string]string) {
// We first make a copy of all existing keys and then add descriptions.
// This prevents us from trying to add descriptions for the (comment) keys
// we just added.
keys := []string{}
for key := range obj {
keys = append(keys, key)
}
for _, key := range keys {
d, ok := descriptions[key]
if ok {
obj["comment:"+key] = d
}
}
}
// convertNestedObjectsToIds converts a slice of objects to a slice of
// IDs from their "id" fields. If objects have "name" field, the value
// is set as a comment before the ID.
func convertNestedObjectsToIds(input interface{}) ([]interface{}, error) {
ids := []interface{}{}
if input == nil {
return ids, nil
}
list, ok := input.([]interface{})
if !ok {
return nil, errors.New("not a list")
}
for i, element := range list {
el, ok := element.(map[string]interface{})
if !ok {
errE := errors.New("not an object")
errors.Details(errE)["index"] = i
return nil, errE
}
name, ok := el["name"]
if ok {
n, ok := name.(string) //nolint:govet
if !ok {
errE := errors.New(`"name" not a string`)
errors.Details(errE)["index"] = i
return nil, errE
}
ids = append(ids, "comment:"+n)
}
id, ok := el["id"]
if !ok {
errE := errors.New(`"id" is missing`)
errors.Details(errE)["index"] = i
return nil, errE
}
ids = append(ids, id)
}
return ids, nil
}