-
Notifications
You must be signed in to change notification settings - Fork 5
/
decode.go
138 lines (114 loc) · 3.14 KB
/
decode.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
package oas
import (
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"github.com/go-openapi/spec"
"github.com/hypnoglow/oas2/convert"
)
const (
tag = "oas"
)
// DecodeQuery decodes all query params by request operation spec to the dst.
func DecodeQuery(req *http.Request, dst interface{}) error {
oi, ok := getOperationInfo(req)
if ok {
return DecodeQueryParams(oi.params, req.URL.Query(), dst)
}
return errors.New("decode query: cannot find OpenAPI operation info in the request context")
}
// DecodeQueryParams decodes query parameters by their spec to the dst.
func DecodeQueryParams(ps []spec.Parameter, q url.Values, dst interface{}) error {
dv := reflect.ValueOf(dst)
if dv.Kind() != reflect.Ptr {
return fmt.Errorf("dst is not a pointer to struct (cannot modify)")
}
dv = dv.Elem()
if dv.Kind() != reflect.Struct {
return fmt.Errorf("dst is not a pointer to struct (cannot modify)")
}
fields := fieldMap(dv)
for _, p := range ps {
// No such tag in struct - no need to populate.
f, ok := fields[p.Name]
if !ok {
continue
}
vals, ok := q[p.Name]
if !ok {
// No such value in query.
// If there is default - use it, otherwise skip this value.
if p.Default == nil {
continue
}
// Default value can be in a weird format internally, e.g.
// when spec gets parsed default value for integer can be of
// type float64. So we cannot assign it directly. We need to
// proceed with conversion procedure.
vals = []string{fmt.Sprintf("%v", p.Default)}
}
// Convert value by type+format in parameter.
v, err := convert.Parameter(vals, &p)
if err != nil {
if p.Format != "" {
return fmt.Errorf(
"cannot use values %v as parameter %s with type %s and format %s",
vals,
p.Name,
p.Type,
p.Format,
)
}
return fmt.Errorf(
"cannot use values %v as parameter %s with type %s",
vals,
p.Name,
p.Type,
)
}
if err := set(v, f, dv); err != nil {
return err
}
}
return nil
}
func set(v interface{}, f reflect.StructField, dst reflect.Value) error {
// Check if tag in struct can accept value of type v.
if !isAssignable(f, v) {
return fmt.Errorf("value of type %s is not assignable to field %s of type %s", reflect.TypeOf(v).String(), f.Name, f.Type.String())
}
fieldVal := dst.FieldByName(f.Name)
if !fieldVal.CanSet() {
return fmt.Errorf("field %s of type %s is not settable", f.Name, dst.Type().Name())
}
if f.Type.Kind() == reflect.Ptr {
fieldVal.Set(reflect.New(f.Type.Elem()))
fieldVal.Elem().Set(reflect.ValueOf(v))
} else {
fieldVal.Set(reflect.ValueOf(v))
}
return nil
}
func isAssignable(field reflect.StructField, value interface{}) bool {
if field.Type.Kind() == reflect.Ptr {
return reflect.TypeOf(value).AssignableTo(field.Type.Elem())
}
return reflect.TypeOf(value).AssignableTo(field.Type)
}
// fieldMap returns v fields mapped by their tags.
func fieldMap(rv reflect.Value) map[string]reflect.StructField {
rt := rv.Type()
m := make(map[string]reflect.StructField)
n := rt.NumField()
for i := 0; i < n; i++ {
f := rt.Field(i)
tag, ok := f.Tag.Lookup(tag)
if !ok {
continue
}
m[tag] = f
}
return m
}