forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.go
46 lines (43 loc) · 1.28 KB
/
structs.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 util
import (
"strconv"
"github.com/fatih/structs"
)
// ToStringMapStringFromStruct returns string[map]string from any struct.
// Use structs tag to change map keys. e.g. ServerName string `structs:"server_name"`
func ToStringMapStringFromStruct(obj interface{}) map[string]string {
conf := structs.Map(obj)
config := map[string]string{}
for x, y := range conf {
switch y.(type) {
case string:
config[x] = y.(string)
case int:
config[x] = strconv.Itoa(y.(int))
case int32:
config[x] = strconv.FormatInt(int64(y.(int32)), 10)
case int64:
config[x] = strconv.FormatInt(y.(int64), 10)
case bool:
config[x] = strconv.FormatBool(y.(bool))
case float64:
config[x] = strconv.FormatFloat(y.(float64), 'f', -1, 64)
case float32:
config[x] = strconv.FormatFloat(float64(y.(float32)), 'f', -1, 32)
case uint:
config[x] = strconv.FormatUint(uint64(y.(uint)), 10)
case uint8:
config[x] = strconv.FormatUint(uint64(y.(uint8)), 10)
case uint16:
config[x] = strconv.FormatUint(uint64(y.(uint16)), 10)
case uint32:
config[x] = strconv.FormatUint(uint64(y.(uint32)), 10)
case uint64:
config[x] = strconv.FormatUint(y.(uint64), 10)
case []byte:
// This is also the case for []uint8
config[x] = string(y.([]byte)[:])
}
}
return config
}