-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
68 lines (61 loc) · 1.48 KB
/
marshal.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
package tablewriter
import (
"encoding/json"
"reflect"
"time"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
type Marshaller interface {
Marshal() ([]byte, error)
}
///////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// Convert any value to a byte array
func marshal(v any, unixTime bool, timeLayout string, timeLocal bool) ([]byte, error) {
// Check for nil
if v == nil || (reflect.TypeOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil()) {
return nil, nil
}
// Use marshaller if implemented
if m, ok := v.(Marshaller); ok {
return m.Marshal()
}
switch v := v.(type) {
case string:
// By default, strings are not quoted
return []byte(v), nil
case time.Time:
// Return nil if zero time, else return formatted time
if v.IsZero() {
return nil, nil
}
if timeLocal {
v = v.Local()
}
return []byte(v.Format(timeLayout)), nil
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
if unixTime {
return marshal(time.Unix(reflect.ValueOf(v).Int(), 0), unixTime, timeLayout, timeLocal)
}
default:
if isNil(v) {
return nil, nil
}
}
// Default option
return json.Marshal(v)
}
// isNil returns true if a value is nil (for pointers, slices and maps)
func isNil(v any) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Map:
return rv.IsNil()
default:
return false
}
}