-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
168 lines (146 loc) · 3.77 KB
/
response.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
// Copyright 2024 Collin Kreklow
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package t38c
import (
"time"
"github.com/tidwall/gjson"
)
// Response represents a database response.
type Response struct {
ID string
Object string
IDs []string
Objects []string
FieldNames map[string]int64
FieldValues []float64
Count int64
Cursor int64
TTL float64
Err string
Elapsed string
Ok bool
Live bool
Command string
Group string
Detect string
Key string
Time time.Time
fields int64
}
// UnmarshalText implements the ability to unmarshal a database
// response.
func (r *Response) UnmarshalText(b []byte) (err error) {
defer func() {
// since gjson.ForEach doesn't return errors, catch panics and
// return them as errors
r := recover()
if r != nil {
s, ok := r.(string)
if ok {
err = newErrorf(nil, "error unmarshaling response: %s", s)
} else {
err = newError(nil, "error unmarshaling response")
}
}
}()
if !gjson.ValidBytes(b) {
return newError(nil, "error unmarshaling response: not valid JSON")
}
gjson.ParseBytes(b).ForEach(r.parse)
return nil
}
// parse is an iterator function used in gjson.ForEach to parse the
// response JSON into the Response fields.
func (r *Response) parse(k, v gjson.Result) bool {
switch k.Str {
case "ok":
r.Ok = v.Bool()
case "id":
r.ID = v.Str
case "object":
if v.Type == gjson.JSON {
r.Object = v.Raw
} else {
r.Object = v.Str
}
case "ids":
v.ForEach(func(_, x gjson.Result) bool {
r.IDs = append(r.IDs, x.Str)
return true
})
case "objects":
v.ForEach(func(_, x gjson.Result) bool {
r.Objects = append(r.Objects, x.Raw)
return true
})
case "fields":
r.parsefields(v)
case "count":
r.Count = int64(v.Num)
case "cursor":
r.Cursor = int64(v.Num)
case "ttl":
r.TTL = v.Num
case "err":
r.Err = v.Str
case "elapsed":
r.Elapsed = v.Str
case "live":
r.Live = v.Bool()
case "command":
r.Command = v.Str
case "group":
r.Group = v.Str
case "detect":
r.Detect = v.Str
case "key":
r.Key = v.Str
case "time":
r.Time, _ = time.Parse(time.RFC3339Nano, v.Str)
default:
panic("unknown response value")
}
return true
}
// parsefields is an iterator function used in gjson.ForEach to parse
// the fields array or object into a map.
func (r *Response) parsefields(v gjson.Result) {
r.FieldNames = make(map[string]int64)
if v.IsArray() {
v.ForEach(func(_, x gjson.Result) bool {
r.FieldNames[x.Str] = r.fields
r.fields++
return true
})
return
}
if v.IsObject() {
v.ForEach(func(l, x gjson.Result) bool {
r.FieldNames[l.Str] = r.fields
r.fields++
r.FieldValues = append(r.FieldValues, x.Num)
return true
})
return
}
panic("unknown field type")
}