Skip to content

Commit 666dd31

Browse files
committed
Refactor BuildId
1 parent a1e1b5d commit 666dd31

File tree

1 file changed

+53
-43
lines changed

1 file changed

+53
-43
lines changed

http_util.go

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -47,64 +47,68 @@ func BuildResourceName(s string) string {
4747
}
4848
return s3
4949
}
50-
func BuildId(r *http.Request, modelType reflect.Type, idNames []string, indexs map[string]int, sizeIgnoreLastUri int) (interface{}, error) {
50+
func MakeId(r *http.Request, modelType reflect.Type, idNames []string, indexes map[string]int, options... int) (map[string]interface{}, error){
5151
modelValue := reflect.New(modelType)
52-
if len(idNames) > 1 {
53-
mapKey := make(map[string]interface{})
54-
_, mapParams, err2 := getParamIds(r, idNames, sizeIgnoreLastUri)
55-
if err2 != nil {
56-
return nil, err2
57-
}
58-
for _, idName := range idNames {
59-
if idValue, ok := mapParams[idName]; ok {
60-
if len(strings.Trim(idValue, " ")) == 0 {
61-
return nil, fmt.Errorf("%v is required", idName)
52+
mapKey := make(map[string]interface{})
53+
_, mapParams, err2 := getParamIds(r, idNames, options...)
54+
if err2 != nil {
55+
return nil, err2
56+
}
57+
for _, idName := range idNames {
58+
if idValue, ok := mapParams[idName]; ok {
59+
if len(strings.Trim(idValue, " ")) == 0 {
60+
return nil, fmt.Errorf("%v is required", idName)
61+
}
62+
index, _ := indexes[idName]
63+
ifField := reflect.Indirect(modelValue).FieldByIndex([]int{index})
64+
idType := ifField.Type().String()
65+
switch idType {
66+
case "int64", "*int64":
67+
if id, err := strconv.ParseInt(idValue, 10, 64); err != nil {
68+
return nil, fmt.Errorf("%v is invalid", idName)
69+
} else {
70+
mapKey[idName] = id
6271
}
63-
index, _ := indexs[idName]
64-
ifField := reflect.Indirect(modelValue).FieldByIndex([]int{index})
65-
idType := ifField.Type().String()
66-
switch idType {
67-
case "int64", "*int64":
68-
if id, err := strconv.ParseInt(idValue, 10, 64); err != nil {
69-
return nil, fmt.Errorf("%v is invalid", idName)
70-
} else {
71-
mapKey[idName] = id
72-
}
73-
case "int", "int32", "*int32":
74-
if id, err := strconv.ParseInt(idValue, 10, 32); err != nil {
75-
return nil, fmt.Errorf("%v is invalid", idName)
76-
} else {
77-
mapKey[idName] = id
78-
}
79-
default:
80-
mapKey[idName] = idValue
72+
case "int", "int32", "*int32":
73+
if id, err := strconv.ParseInt(idValue, 10, 32); err != nil {
74+
return nil, fmt.Errorf("%v is invalid", idName)
75+
} else {
76+
mapKey[idName] = id
8177
}
82-
} else {
83-
return nil, fmt.Errorf("%v is required", idName)
78+
default:
79+
mapKey[idName] = idValue
8480
}
81+
} else {
82+
return nil, fmt.Errorf("%v is required", idName)
8583
}
86-
return mapKey, nil
84+
}
85+
return mapKey, nil
86+
}
87+
func BuildId(r *http.Request, modelType reflect.Type, idNames []string, indexes map[string]int, options... int) (interface{}, error) {
88+
if len(idNames) > 1 {
89+
return MakeId(r, modelType, idNames, indexes, options...)
8790
} else if len(idNames) == 1 {
88-
idValue, _, err1 := getParamIds(r, idNames, sizeIgnoreLastUri)
91+
modelValue := reflect.New(modelType)
92+
idValue, _, err1 := getParamIds(r, idNames, options...)
8993
if err1 != nil {
9094
return nil, err1
9195
}
92-
if idstr, ok := idValue.(string); ok {
93-
if len(strings.Trim(idstr, " ")) == 0 {
96+
if idStr, ok := idValue.(string); ok {
97+
if len(strings.Trim(idStr, " ")) == 0 {
9498
return nil, fmt.Errorf("%v is required", idNames[0])
9599
}
96-
index, _ := indexs[idNames[0]]
100+
index, _ := indexes[idNames[0]]
97101
ifField := reflect.Indirect(modelValue).FieldByIndex([]int{index})
98102
idType := ifField.Type().String()
99103
switch idType {
100104
case "int64", "*int64":
101-
if id, err := strconv.ParseInt(idstr, 10, 64); err != nil {
105+
if id, err := strconv.ParseInt(idStr, 10, 64); err != nil {
102106
return nil, fmt.Errorf("%v is invalid", idNames[0])
103107
} else {
104108
return id, nil
105109
}
106110
case "int", "int32", "*int32":
107-
if id, err := strconv.ParseInt(idstr, 10, 32); err != nil {
111+
if id, err := strconv.ParseInt(idStr, 10, 32); err != nil {
108112
return nil, fmt.Errorf("%v is invalid", idNames[0])
109113
} else {
110114
return id, nil
@@ -113,7 +117,7 @@ func BuildId(r *http.Request, modelType reflect.Type, idNames []string, indexs m
113117
return idValue, nil
114118
}
115119
} else {
116-
return nil, errors.New("error parser string get id by url")
120+
return nil, errors.New("error parser string get id by uri")
117121
}
118122
} else {
119123
return nil, errors.New("invalid model type: no id of this model type")
@@ -176,24 +180,30 @@ func GetIndexes(modelType reflect.Type) map[string]int {
176180
}
177181
return mapJsonNameIndex
178182
}
179-
func getParamIds(r *http.Request, idNames []string, sizeIgnoreLastUri int) (interface{}, map[string]string, error) {
183+
func getParamIds(r *http.Request, idNames []string, options... int) (interface{}, map[string]string, error) {
184+
offset := 0
185+
if len(options) > 0 && options[0] > 0 {
186+
offset = options[0]
187+
}
180188
sizeName := len(idNames)
181189
params := strings.Split(r.RequestURI, "/")
182190
// remove some item last array
183-
params = params[:len(params)-sizeIgnoreLastUri]
191+
params = params[:len(params)-offset]
184192
sizeParam := len(params)
185193
start := sizeParam - sizeName
186194
if sizeParam >= start {
187195
// get params
188196
params = params[start:sizeParam]
197+
mapParams := make(map[string]string)
189198
if sizeName == 1 {
190199
if len(params) != 1 {
191200
return nil, nil, errors.New("bad request")
192201
}
193-
return params[0], nil, nil
202+
// convert map param
203+
mapParams[idNames[0]] = params[0]
204+
return params[0], mapParams, nil
194205
}
195206
// convert map param
196-
mapParams := make(map[string]string)
197207
for i, v := range params {
198208
mapParams[idNames[i]] = v
199209
}

0 commit comments

Comments
 (0)