-
Notifications
You must be signed in to change notification settings - Fork 2
/
json.go
110 lines (91 loc) · 2.58 KB
/
json.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// json.go provides types & functions for json unmarshalling
package types
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
)
const (
// DateTimeFormat is the format used by the Navitia Api for use with time pkg.
DateTimeFormat string = "20060102T150405" // YYYYMMDDThhmmss
// DateFormat is when there is no time info
DateFormat string = "20060102"
)
// parseDateTime parses a time formatted under iso-date-time as indicated in the Navitia api.
// This is simply parsing a date formatted under the standard ISO 8601.
// If the given string is empty (i.e ""), then the zero value of time.Time will be returned
func parseDateTime(datetime string) (time.Time, error) {
// If there's no datetime given, just return the zero value
if datetime == "" || datetime == "not-a-date-time" {
return time.Time{}, nil
}
// If the datetime doesn't countain a "T", then it does not have time info
var format string
if strings.Contains(datetime, "T") {
format = DateTimeFormat
} else {
format = DateFormat
}
// Parse it
res, err := time.Parse(format, datetime)
if err != nil {
err = errors.Wrap(err, "parseDateTime: error while parsing datetime")
}
return res, err
}
// UnmarshalError is returned when unmarshalling fails
// It implements both error and github.com/pkg/errors's causer
type UnmarshalError struct {
// Type on which the unmarshaller where the error occurred works
Type string
// JSON Key where failure occurred
Key string
// Name of the key in package
Name string
// Value associated with the key
Value interface{}
// Message of the error
Message string
// Underlying error
Underlying error
// Full JSON data
JSON []byte
}
// Cause implements github.com/pkg/error's causer
func (err UnmarshalError) Cause() error {
return err.Underlying
}
// Error implements error
func (err UnmarshalError) Error() string {
msg := fmt.Sprintf(
"(*%s).UnmarshalJSON: Unmarshalling %s (json: \"%s\") with value \"%v\" failed",
err.Type,
err.Name,
err.Key,
err.Value)
if err.Message != "" {
msg += ": " + err.Message
}
if err.Underlying != nil {
msg += " [" + err.Cause().Error() + "]"
}
return msg
}
// unmarshalErrorer allows us to make better error messages
type unmarshalErrorMaker struct {
Type string
JSON []byte
}
// err creates a new UnmarshalError
func (gen unmarshalErrorMaker) err(underlyingErr error, name string, key string, value interface{}, message string) error {
return UnmarshalError{
Type: gen.Type,
Key: key,
Name: name,
Value: value,
Message: message,
Underlying: underlyingErr,
JSON: gen.JSON,
}
}