-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraySerialization.go
More file actions
68 lines (60 loc) · 1.39 KB
/
arraySerialization.go
File metadata and controls
68 lines (60 loc) · 1.39 KB
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
package https
import "fmt"
// ArraySerializationOption represents the type for request array serialization options.
type ArraySerializationOption int
// Constants for different request retry options.
const (
Indexed = iota
UnIndexed
Plain
Csv
Tsv
Psv
)
var ArraySerializationOptionStrings = [...]string{
"Indexed",
"UnIndexed",
"Plain",
"Csv",
"Tsv",
"Psv",
}
func (option ArraySerializationOption) getSeparator() rune {
switch option {
case Csv:
return ','
case Tsv:
return '\t'
case Psv:
return '|'
default:
return -1
}
}
func (option ArraySerializationOption) joinKey(keyPrefix string, index any) string {
if index == nil {
switch option {
case UnIndexed:
return fmt.Sprintf("%v[]", keyPrefix)
default:
return fmt.Sprintf("%v", keyPrefix)
}
}
indexedKey := fmt.Sprintf("%v", index)
return fmt.Sprintf("%v[%v]", keyPrefix, indexedKey)
}
func (option ArraySerializationOption) appendMap(result map[string][]string, param map[string][]string) {
for k, values := range param {
for _, value := range values {
option.append(result, k, value)
}
}
}
func (option ArraySerializationOption) append(result map[string][]string, key string, value string) {
separator := option.getSeparator()
if len(result[key]) > 0 && separator != -1 {
result[key][0] = fmt.Sprintf("%v%c%v", result[key][0], separator, value)
} else {
result[key] = append(result[key], value)
}
}