This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparams.go
83 lines (73 loc) · 1.62 KB
/
params.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
// Goro
//
// Created by Yakka
// http://theyakka.com
//
// Copyright (c) 2019 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package goro
type Parameters struct {
paramsMap map[string][]interface{}
}
func NewParametersWithMap(paramsMap map[string][]string) *Parameters {
interfaceMap := stringMapToInterfaceMap(paramsMap)
return &Parameters{
paramsMap: interfaceMap,
}
}
func (p *Parameters) Get(key string) []interface{} {
return p.paramsMap[key]
}
func (p *Parameters) GetStrings(key string) []string {
val := p.paramsMap[key]
if val == nil || len(val) == 0 {
return []string{}
}
outArr := make([]string, len(val))
for i, v := range val {
if n, ok := v.(string); ok {
outArr[i] = n
}
}
return outArr
}
func (p *Parameters) GetFirstString(key string) string {
strArr := p.GetStrings(key)
if len(strArr) == 0 {
return ""
}
return strArr[0]
}
func (p *Parameters) GetInts(key string) []int {
val := p.paramsMap[key]
if val == nil || len(val) == 0 {
return []int{}
}
outArr := make([]int, len(val))
for i, v := range val {
if n, ok := v.(int); ok {
outArr[i] = n
}
}
return outArr
}
func (p *Parameters) GetInt(key string) int {
intArr := p.GetInts(key)
if len(intArr) == 0 {
return 0
}
return intArr[0]
}
// converts a map of lists of strings to a generic interface
func stringMapToInterfaceMap(stringMap map[string][]string) map[string][]interface{} {
outMap := map[string][]interface{}{}
for k, v := range stringMap {
var outArr []interface{}
for _, vi := range v {
outArr = append(outArr, vi)
}
outMap[k] = outArr
}
return outMap
}