-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
53 lines (46 loc) · 875 Bytes
/
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
package caption_json_formatter
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"reflect"
)
func Stringify(v interface{}) string {
var ret string
if IsObject(v) || isMap(v) {
ret = marshal(v)
} else {
ret = fmt.Sprint(v)
}
return ret
}
func IsObject(v interface{}) bool {
return reflect.ValueOf(v).Kind() == reflect.Struct
}
func marshal(o interface{}) string {
str, ok := o.(string)
if ok {
return str
}
data, err := json.Marshal(o)
if err != nil {
return fmt.Sprint(o)
}
return string(data)
}
func marshalIndent(o interface{}) string {
str, ok := o.(string)
if ok {
return str
}
m, err := json.MarshalIndent(o, "", " ")
if err != nil {
return fmt.Sprint(o)
}
return string(m)
}
func isMap(v interface{}) bool {
_, isMap := v.(map[string]interface{})
_, isLogFields := v.(logrus.Fields)
return isMap || isLogFields
}