zrhoffman commented on code in PR #7708:
URL: https://github.com/apache/trafficcontrol/pull/7708#discussion_r1305565834
##########
traffic_ops/traffic_ops_golang/parameter/parameters.go:
##########
@@ -364,23 +363,28 @@ func CreateParameter(w http.ResponseWriter, r
*http.Request) {
// This code block decides if the request body is a slice of parameters
or a single object.
var params []tc.ParameterV5
- switch reflect.TypeOf(data).Kind() {
- case reflect.Slice:
- if err := json.Unmarshal(body, ¶ms); err != nil {
- api.HandleErr(w, r, tx, http.StatusBadRequest,
errors.New("error unmarshalling slice"), nil)
- return
+
+ // Try to unmarshal the data as a slice
+ var sliceData []map[string]interface{}
+ if err := json.Unmarshal(body, &sliceData); err == nil {
+ for _, item := range sliceData {
+ param := tc.ParameterV5{
+ ConfigFile: item["configFile"].(string),
+ Name: item["name"].(string),
+ Secure: item["secure"].(bool),
+ Value: item["value"].(string),
+ }
+
+ params = append(params, param)
}
- case reflect.Map:
- // If it is a single object it is still converted to a slice
for code simplicity.
+ } else {
+ // Try to unmarshal the data as a single object
Review Comment:
`sliceData` is unnecessary. Why not just unmarshal directly to `params`?
```go
if err := json.Unmarshal(body, ¶ms); err != nil {
var param tc.ParameterV5
if err := json.Unmarshal(body, ¶m); err != nil {
api.HandleErr(w, r, tx, http.StatusBadRequest, errors.New("error
unmarshalling single object"), nil)
return
}
params = append(params, param)
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]