-
Notifications
You must be signed in to change notification settings - Fork 6
/
encoding.go
178 lines (144 loc) · 3.49 KB
/
encoding.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2021-present Airheart, Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package duffel
import (
"fmt"
"io"
"strconv"
"time"
"github.com/airheartdev/duffel/iso8601"
"github.com/segmentio/encoding/json"
)
type (
Date time.Time
DateTime time.Time
Duration time.Duration
Distance float64
)
const DateFormat = "2006-01-02"
func (t Date) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format(DateFormat))
return []byte(stamp), nil
}
func (t Date) String() string {
return time.Time(t).Format(DateFormat)
}
// UnmarshalJSON implements the json.Unmarshaler from date string to time.Time
func (t *Date) UnmarshalJSON(b []byte) error {
str, err := parseJSONBytesToString(b)
if err != nil {
if err == ErrNullValue {
return nil
}
return err
}
stamp, err := time.Parse("2006-01-02", str)
if err != nil {
return err
}
*t = Date(stamp)
return nil
}
func (t DateTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format(time.RFC3339Nano))
return []byte(stamp), nil
}
func (t DateTime) String() string {
return time.Time(t).Format(time.RFC3339)
}
var timeFormats = []string{
"2006-01-02T15:04:05",
"2006-01-02",
time.RFC3339,
time.RFC3339Nano,
}
// UnmarshalJSON implements the json.Unmarshaler from date string to time.Time
func (t *DateTime) UnmarshalJSON(b []byte) error {
str, err := parseJSONBytesToString(b)
if err != nil {
if err == ErrNullValue {
return nil
}
return err
}
stamp := time.Time{}
for _, format := range timeFormats {
stamp, err = time.Parse(format, str)
if err != nil {
continue
}
break
}
if stamp.IsZero() {
return fmt.Errorf("failed to parse timestamp: '%s'", str)
}
*t = DateTime(stamp)
return nil
}
func (t Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", iso8601.FormatDuration(time.Duration(t)))), nil
}
func (t Duration) String() string {
return iso8601.FormatDuration(time.Duration(t))
}
// UnmarshalGQL implements the graphql.Unmarshaler interface
func (d *Duration) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return fmt.Errorf("invalid duration value: %v", v)
}
dur, err := iso8601.ParseDuration(str)
if err != nil {
return err
}
*d = Duration(dur)
return nil
}
// MarshalGQL implements the graphql.Marshaler interface
func (d Duration) MarshalGQL(w io.Writer) {
fmt.Fprint(w, strconv.Quote(d.String()))
}
// UnmarshalJSON implements the json.Unmarshaler from date string to time.Time
func (t *Duration) UnmarshalJSON(b []byte) error {
f, err := parseJSONBytesToString(b)
if err != nil {
if err == ErrNullValue {
return nil
}
return err
}
d, err := iso8601.ParseDuration(f)
if err != nil {
return err
}
*t = Duration(d)
return nil
}
func (t Distance) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(fmt.Sprintf("%f", t))), nil
}
// UnmarshalJSON implements the json.Unmarshaler from date string to time.Time
func (t *Distance) UnmarshalJSON(b []byte) error {
f, err := parseJSONBytesToString(b)
if err != nil {
if err == ErrNullValue {
return nil
}
return err
}
d, err := strconv.ParseFloat(f, 16)
if err != nil {
return err
}
*t = Distance(d)
return nil
}
var ErrNullValue = fmt.Errorf("null value")
func parseJSONBytesToString(b []byte) (string, error) {
b = json.Unescape(b)
if len(b) == 0 {
return "", ErrNullValue
}
return string(b), nil
}