-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconversion.go
48 lines (44 loc) · 911 Bytes
/
conversion.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
package relic
import (
"fmt"
"time"
)
var DefaultDateLayout = "2006-01-02"
func AsString(stringLike interface{}) (string, error) {
switch s := stringLike.(type) {
case string:
return s, nil
case fmt.Stringer:
return s.String(), nil
default:
return "", fmt.Errorf("unsupported type for string: %t, must string or fmt.Stringer", s)
}
}
func AsDate(dateLike interface{}) (time.Time, error) {
switch d := dateLike.(type) {
case time.Time:
return d, nil
default:
s, err := AsString(d)
if err != nil {
return time.Time{}, err
}
return time.Parse(DefaultDateLayout, s)
}
}
func AsVersion(versionLike interface{}) (Version, error) {
switch v := versionLike.(type) {
case Version:
return v, nil
default:
s, err := AsString(v)
if err != nil {
return Version{}, err
}
if s == "" {
// for unreleased notes
return Version{}, nil
}
return ParseDatedVersion(s)
}
}