-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodec.go
More file actions
172 lines (149 loc) · 3.33 KB
/
codec.go
File metadata and controls
172 lines (149 loc) · 3.33 KB
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
package apigen
import (
"encoding/json"
"fmt"
"io"
"net/url"
"sort"
"strings"
)
type jsonType int
const (
jsonTypeNull jsonType = iota
jsonTypeBool
jsonTypeNumber
jsonTypeString
jsonTypeArray
jsonTypeObject
)
type decoder interface {
Decode(io.Reader) (_type, error)
}
type jsonDecoder struct{}
func (d *jsonDecoder) Decode(r io.Reader) (_type, error) {
var v interface{}
if err := json.NewDecoder(r).Decode(&v); err != nil {
return nil, err
}
switch v := v.(type) {
case map[string]interface{}:
return decodeJSONObject(v), nil
case []interface{}:
return decodeJSONArray(v), nil
default:
return nil, fmt.Errorf("unsupported top-level JSON type: %w", ErrUnimplemented)
}
}
func decodeJSONType(v interface{}) _type {
switch detectJSONType(v) {
case jsonTypeNull:
return emptyIfaceType
case jsonTypeObject:
return decodeJSONObject(v.(map[string]interface{}))
case jsonTypeArray:
return decodeJSONArray(v.([]interface{}))
case jsonTypeBool:
return typeBool
case jsonTypeString:
return typeString
case jsonTypeNumber:
return typeFloat64
default:
panic("unreachable")
}
}
func decodeJSONObject(o map[string]interface{}) *structType {
var s structType
for k, v := range o {
key := public(k)
field := &structField{
name: key,
tags: map[string][]string{"json": {k, "omitempty"}},
}
switch detectJSONType(v) {
case jsonTypeNull:
field._type = emptyIfaceType
case jsonTypeObject:
field._type = decodeJSONObject(v.(map[string]interface{}))
case jsonTypeArray:
field._type = decodeJSONArray(v.([]interface{}))
case jsonTypeBool:
field._type = typeBool
case jsonTypeString:
field._type = typeString
case jsonTypeNumber:
field._type = typeFloat64
}
s.fields = append(s.fields, field)
}
sort.Slice(s.fields, func(i, j int) bool {
return s.fields[i].name < s.fields[j].name
})
return &s
}
func decodeJSONArray(arr []interface{}) *sliceType {
if len(arr) == 0 {
return &sliceType{elemType: emptyIfaceType}
}
return &sliceType{elemType: decodeJSONType(arr[0])}
}
func detectJSONType(v interface{}) jsonType {
switch cv := v.(type) {
case nil:
return jsonTypeNull
case map[string]interface{}:
return jsonTypeObject
case []interface{}:
return jsonTypeArray
case bool:
return jsonTypeBool
case string:
return jsonTypeString
case float64:
return jsonTypeNumber
default:
panic(fmt.Sprintf("unknown type: %T", cv))
}
}
func structFromPathParams(h string, u *url.URL) *structType {
if h == "" {
return &structType{}
}
var (
t structType
left int
replaceArgs []string
)
runes := []rune(h)
for i, r := range runes {
switch r {
case '{':
left = i
case '}':
k := string(runes[left+1 : i]) // Param name without '{' and '}'.
t.fields = append(t.fields, &structField{
name: public(k), // TODO: Support snake case.
_type: typeString,
})
replaceArgs = append(replaceArgs, string(runes[left:i+1]), "%s")
}
}
u.Path = strings.NewReplacer(replaceArgs...).Replace(h)
return &t
}
func structFromQuery(q url.Values) *structType {
var s structType
for k, v := range q {
field := &structField{
name: public(k),
meta: map[string]string{"key": k},
}
if len(v) == 1 {
field._type = typeString
} else {
field._type = &sliceType{elemType: typeString}
}
s.fields = append(s.fields, field)
}
return &s
}