-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
types.go
174 lines (153 loc) · 4.16 KB
/
types.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package types
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"time"
null "gopkg.in/guregu/null.v3"
)
func NullDecoder(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
typeFrom := f.String()
typeTo := t.String()
expectedType := ""
switch typeTo {
case "null.String":
if typeFrom == reflect.String.String() {
return null.StringFrom(data.(string)), nil
}
expectedType = reflect.String.String()
case "null.Bool":
if typeFrom == reflect.Bool.String() {
return null.BoolFrom(data.(bool)), nil
}
expectedType = reflect.Bool.String()
case "null.Int":
if typeFrom == reflect.Int.String() {
return null.IntFrom(int64(data.(int))), nil
}
if typeFrom == reflect.Int32.String() {
return null.IntFrom(int64(data.(int32))), nil
}
if typeFrom == reflect.Int64.String() {
return null.IntFrom(data.(int64)), nil
}
expectedType = reflect.Int.String()
case "null.Float":
if typeFrom == reflect.Float32.String() {
return null.FloatFrom(float64(data.(float32))), nil
}
if typeFrom == reflect.Float64.String() {
return null.FloatFrom(data.(float64)), nil
}
expectedType = reflect.Float32.String() + " or " + reflect.Float64.String()
case "types.NullDuration":
if typeFrom == reflect.String.String() {
var d NullDuration
err := d.UnmarshalText([]byte(data.(string)))
return d, err
}
expectedType = reflect.String.String()
}
if expectedType != "" {
return data, fmt.Errorf("expected '%s', got '%s'", expectedType, typeFrom)
}
return data, nil
}
// Duration is an alias for time.Duration that de/serialises to JSON as human-readable strings.
type Duration time.Duration
func (d Duration) String() string {
return time.Duration(d).String()
}
func (d *Duration) UnmarshalText(data []byte) error {
v, err := time.ParseDuration(string(data))
if err != nil {
return err
}
*d = Duration(v)
return nil
}
func (d *Duration) UnmarshalJSON(data []byte) error {
if len(data) > 0 && data[0] == '"' {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
v, err := time.ParseDuration(str)
if err != nil {
return err
}
*d = Duration(v)
} else {
var v time.Duration
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*d = Duration(v)
}
return nil
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
// NullDuration is a nullable Duration, in the same vein as the nullable types provided by
// package gopkg.in/guregu/null.v3.
type NullDuration struct {
Duration
Valid bool
}
// NewNullDuration is a simple helper constructor function
func NewNullDuration(d time.Duration, valid bool) NullDuration {
return NullDuration{Duration(d), valid}
}
// Creates a valid NullDuration from a time.Duration.
func NullDurationFrom(d time.Duration) NullDuration {
return NullDuration{Duration(d), true}
}
func (d *NullDuration) UnmarshalText(data []byte) error {
if len(data) == 0 {
*d = NullDuration{}
return nil
}
if err := d.Duration.UnmarshalText(data); err != nil {
return err
}
d.Valid = true
return nil
}
func (d *NullDuration) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte(`null`)) {
d.Valid = false
return nil
}
if err := json.Unmarshal(data, &d.Duration); err != nil {
return err
}
d.Valid = true
return nil
}
func (d NullDuration) MarshalJSON() ([]byte, error) {
if !d.Valid {
return []byte(`null`), nil
}
return d.Duration.MarshalJSON()
}