-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
144 lines (125 loc) · 4.04 KB
/
schema.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
package resource
import (
"encoding/json"
"io"
"net/http"
"regexp"
"sort"
"strings"
"github.com/qor/qor"
"github.com/qor/qor/responder"
)
func convertMapToMetaValues(values map[string]interface{}, metaors []Metaor) (*MetaValues, error) {
metaValues := &MetaValues{}
metaorMap := make(map[string]Metaor)
for _, metaor := range metaors {
metaorMap[metaor.GetName()] = metaor
}
for key, value := range values {
var metaValue *MetaValue
metaor := metaorMap[key]
switch result := value.(type) {
case map[string]interface{}:
if children, err := convertMapToMetaValues(result, metaor.GetMetas()); err == nil {
metaValue = &MetaValue{Name: key, Meta: metaor, MetaValues: children}
}
case []interface{}:
for _, r := range result {
if mr, ok := r.(map[string]interface{}); ok {
if children, err := convertMapToMetaValues(mr, metaor.GetMetas()); err == nil {
metaValue := &MetaValue{Name: key, Meta: metaor, MetaValues: children}
metaValues.Values = append(metaValues.Values, metaValue)
}
} else {
metaValue := &MetaValue{Name: key, Value: result, Meta: metaor}
metaValues.Values = append(metaValues.Values, metaValue)
break
}
}
default:
metaValue = &MetaValue{Name: key, Value: value, Meta: metaor}
}
if metaValue != nil {
metaValues.Values = append(metaValues.Values, metaValue)
}
}
return metaValues, nil
}
func ConvertJSONToMetaValues(reader io.Reader, metaors []Metaor) (*MetaValues, error) {
decoder := json.NewDecoder(reader)
values := map[string]interface{}{}
if err := decoder.Decode(&values); err == nil {
return convertMapToMetaValues(values, metaors)
} else {
return nil, err
}
}
var (
isCurrentLevel = regexp.MustCompile("^[^.]+$")
isNextLevel = regexp.MustCompile(`^(([^.\[\]]+)(\[\d+\])?)(?:\.([^.]+)+)$`)
)
func ConvertFormToMetaValues(request *http.Request, metaors []Metaor, prefix string) (*MetaValues, error) {
metaValues := &MetaValues{}
metaorsMap := map[string]Metaor{}
convertedNextLevel := map[string]bool{}
for _, metaor := range metaors {
metaorsMap[metaor.GetName()] = metaor
}
newMetaValue := func(key string, value interface{}) {
if strings.HasPrefix(key, prefix) {
var metaValue *MetaValue
key = strings.TrimPrefix(key, prefix)
if matches := isCurrentLevel.FindStringSubmatch(key); len(matches) > 0 {
name := matches[0]
metaValue = &MetaValue{Name: name, Value: value, Meta: metaorsMap[name]}
} else if matches := isNextLevel.FindStringSubmatch(key); len(matches) > 0 {
name := matches[1]
if _, ok := convertedNextLevel[name]; !ok {
convertedNextLevel[name] = true
metaor := metaorsMap[matches[2]]
if children, err := ConvertFormToMetaValues(request, metaor.GetMetas(), prefix+name+"."); err == nil {
metaValue = &MetaValue{Name: matches[2], Meta: metaor, MetaValues: children}
}
}
}
if metaValue != nil {
metaValues.Values = append(metaValues.Values, metaValue)
}
}
}
var sortedFormKeys []string
for key := range request.Form {
sortedFormKeys = append(sortedFormKeys, key)
}
sort.Strings(sortedFormKeys)
for _, key := range sortedFormKeys {
newMetaValue(key, request.Form[key])
}
if request.MultipartForm != nil {
sortedFormKeys = []string{}
for key := range request.MultipartForm.File {
sortedFormKeys = append(sortedFormKeys, key)
}
sort.Strings(sortedFormKeys)
for _, key := range sortedFormKeys {
newMetaValue(key, request.MultipartForm.File[key])
}
}
return metaValues, nil
}
func Decode(context *qor.Context, result interface{}, res Resourcer) (errs []error) {
var err error
var metaValues *MetaValues
metaors := res.GetMetas([]string{})
responder.With("html", func() {
metaValues, err = ConvertFormToMetaValues(context.Request, metaors, "QorResource.")
}).With("json", func() {
metaValues, err = ConvertJSONToMetaValues(context.Request.Body, metaors)
context.Request.Body.Close()
}).Respond(nil, context.Request)
if err != nil {
errs = append(errs, err)
}
errs = DecodeToResource(res, result, metaValues, context).Start()
return errs
}