-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfields.go
46 lines (36 loc) · 1.05 KB
/
fields.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
package webutils
func MappedFields(fields []string, fieldsToMappedFields map[string]string) []string {
responseColumns := []string{}
columnExists := make(map[string]struct{})
for _, field := range fields {
if column, ok := fieldsToMappedFields[field]; ok {
if _, ok := columnExists[column]; !ok {
responseColumns = append(responseColumns, column)
columnExists[column] = struct{}{}
}
}
}
if len(responseColumns) == 0 {
return []string{}
}
return responseColumns
}
// IncludesAnyReturnField returns true if returnFields is empty or includes at least one of fields
func IncludesAnyReturnField(returnFields []string, fields ...string) bool {
if len(returnFields) == 0 {
return true
}
for _, field := range fields {
if ContainsString(returnFields, field) {
return true
}
}
return false
}
// ReturnFieldsWith appends a field to fields slice if not already appended
func ReturnFieldsWith(fields []string, field string) []string {
if !IncludesAnyReturnField(fields, field) {
fields = append(fields, field)
}
return fields
}