/
input_parser.go
141 lines (105 loc) · 3.45 KB
/
input_parser.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
package use
import (
"github.com/mitchellh/mapstructure"
"reflect"
"strconv"
)
type inputParser struct {
method *Reflection
}
func newParamParser(method *Reflection) *inputParser {
return &inputParser{method: method}
}
func (i *inputParser) parse() []reflect.Value {
parsedParams := make([]reflect.Value, 0)
parsedParams = i.appendReceiver(parsedParams)
cnt := 0
for cnt < i.method.t.NumIn() {
currentInputParam := i.method.t.In(cnt)
if cnt == 0 && len(parsedParams) > 0 {
cnt++
continue
}
cnt++
inputKind := currentInputParam.String()
if i.method.injects.Has(inputKind) {
parsedParams = append(parsedParams, reflect.ValueOf(i.method.injects.Get(inputKind)))
continue
}
param := i.method.p.Next()
parsedParams = append(parsedParams, i.parseSingle(currentInputParam, param))
}
return parsedParams
}
func (i *inputParser) parseSingle(input reflect.Type, value interface{}) reflect.Value {
kind := input.Kind()
parsedParam := reflect.ValueOf(value)
if kind == reflect.String {
parsedParam = i.parseStringParam(value)
} else if kind == reflect.Int {
parsedParam = i.parseIntParam(value)
} else if kind == reflect.Bool {
parsedParam = i.parseBoolParam(value)
} else if (kind == reflect.Struct || kind == reflect.Ptr) && (parsedParam.Kind() == reflect.String || parsedParam.Kind() == reflect.Int) {
id := i.parseIntParam(value)
parsedParam = getRecordFromTypeAndId(input, int(id.Int()))
} else if kind == reflect.Struct {
form := i.parseFormValues(value)
destination := reflect.New(input).Elem().Interface()
reflectValue := reflect.ValueOf(destination)
destination = reflect.New(reflectValue.Type()).Interface()
mapstructure.Decode(form, destination)
parsedParam = reflect.ValueOf(destination).Elem()
}
return parsedParam
}
func getRecordFromTypeAndId(paramType reflect.Type, id int) reflect.Value {
destination := reflect.New(paramType).Elem().Interface()
reflectValue := reflect.ValueOf(destination)
destination = reflect.New(reflectValue.Type().Elem()).Interface()
DB().First(destination, id)
return reflect.ValueOf(destination)
}
func (i *inputParser) appendReceiver(arguments []reflect.Value) []reflect.Value {
amount := i.method.t.NumIn()
if amount > 0 {
first := i.method.t.In(0)
isPointer := first.Kind() == reflect.Ptr
if first.Kind() == reflect.Struct || isPointer {
if _, ok := first.MethodByName(i.method.Name()); ok {
newReceiver := reflect.New(first)
if !isPointer {
newReceiver = newReceiver.Elem()
}
arguments = append(arguments, newReceiver)
}
}
}
return arguments
}
func (i *inputParser) parseStringParam(param interface{}) reflect.Value {
parsedParam := reflect.ValueOf(param)
if reflect.ValueOf(param).Kind() == reflect.Int {
parsedParam = reflect.ValueOf(strconv.Itoa(param.(int)))
}
return parsedParam
}
func (i *inputParser) parseIntParam(param interface{}) reflect.Value {
parsedParam := reflect.ValueOf(param)
if reflect.ValueOf(param).Kind() == reflect.String {
converted, _ := strconv.Atoi(param.(string))
parsedParam = reflect.ValueOf(converted)
}
return parsedParam
}
func (i *inputParser) parseBoolParam(param interface{}) reflect.Value {
parsedParam := reflect.ValueOf(param)
if reflect.ValueOf(param).Kind() == reflect.String {
converted, _ := strconv.ParseBool(param.(string))
parsedParam = reflect.ValueOf(converted)
}
return parsedParam
}
func (i *inputParser) parseFormValues(param interface{}) interface{} {
return param
}