-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
55 lines (49 loc) · 1.19 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
// Copyright 2017 Marin Basic <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gomercedesdealer
import (
"fmt"
"reflect"
"strings"
"unicode"
)
// covert struct fields to map of strings
func mapFromStruct(i interface{}) map[string]string {
params := make(map[string]string)
s := reflect.ValueOf(i).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
var value string
var key string = typeOfT.Field(i).Name
switch s.Field(i).Kind().String() {
case "slice":
value = strings.Join(s.Field(i).Interface().([]string), ",")
break
case "float64":
if s.Field(i).Interface().(float64) <= 0 {
break
}
value = fmt.Sprintf("%f", s.Field(i).Interface().(float64))
break
case "int":
if s.Field(i).Interface().(int) <= 0 {
break
}
value = fmt.Sprintf("%d", s.Field(i).Interface().(int))
break
default:
value = s.Field(i).Interface().(string)
}
if len(value) > 0 {
params[camelCase(key)] = value
}
}
return params
}
// first char to lower
func camelCase(s string) string {
a := []rune(s)
a[0] = unicode.ToLower(a[0])
return string(a)
}