-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
76 lines (67 loc) · 1.7 KB
/
common.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
72
73
74
75
76
package cast
import (
"encoding/json"
"fmt"
"reflect"
"time"
)
/*
jsonStringToObject attempts to unmarshall a string as JSON into the object
passed as pointer.
*/
func jsonStringToObject(s string, v interface{}) error {
data := []byte(s)
return json.Unmarshal(data, v)
}
/*
indirect returns the value, after dereferencing as many times as necessary
to reach the base type (or nil).
From html/template/content.go
Copyright 2011 The Go Authors. All rights reserved.
*/
func indirect(a interface{}) interface{} {
if a == nil {
return nil
}
if t := reflect.TypeOf(a); t.Kind() != reflect.Ptr {
// Avoid creating a reflect.Value if it's not a pointer.
return a
}
v := reflect.ValueOf(a)
for v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return v.Interface()
}
/*
indirectToStringerOrError returns the value, after dereferencing as many
times as necessary to reach the base type (or nil) or an implementation of
fmt.Stringer or error.
From html/template/content.go
Copyright 2011 The Go Authors. All rights reserved.
*/
func indirectToStringerOrError(a interface{}) interface{} {
if a == nil {
return nil
}
var errorType = reflect.TypeOf((*error)(nil)).Elem()
var fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
v := reflect.ValueOf(a)
for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return v.Interface()
}
/*
parseDateWith
*/
func parseDateWith(s string, dates []string) (time.Time, error) {
var t time.Time
var err error
for _, dateType := range dates {
if t, err = time.Parse(dateType, s); err == nil {
return t, err
}
}
return t, fmt.Errorf("unable to parse date: %s", s)
}