-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
71 lines (62 loc) · 1.31 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ncserial
import (
"fmt"
"reflect"
"strconv"
)
//CovertToString convert interface to string
func CovertToString(value interface{}) string {
if value == nil {
return ""
}
var val string
v := reflect.ValueOf(value)
if v.Kind() == reflect.String {
val = v.String()
} else if v.Type() == stringPtrType {
val = v.Elem().String()
} else if v.Kind() == reflect.Bool {
if v.Bool() {
val = "on"
} else {
val = "off"
}
} else if v.Kind() == reflect.Int {
val = strconv.FormatInt(v.Int(), 10)
} else if v.Kind() == reflect.Float64 {
val = fmt.Sprintf("%g", v.Float())
} else if v.Kind() == reflect.Slice {
val = ""
len := v.Len()
for i := 0; i < len; i++ {
if i == (len - 1) {
val = val + v.Index(i).String()
} else {
val = val + v.Index(i).String() + " "
}
}
}
return val
}
//BuildDirective build directive with name , value
func BuildDirective(name string, value interface{}) Directive {
t, ok := value.(*Block)
if ok {
return (Directive)(t)
}
return NewKVOption(name, value)
}
//MergeDirectives merge two directives
func MergeDirectives(d1, d2 Directives) Directives {
for _, d := range d2 {
d1 = append(d1, d)
}
return d1
}
//MergeOptions merge two options
func MergeOptions(s1, s2 Options) Options {
for _, v := range s2 {
s1 = append(s1, v)
}
return s1
}