Skip to content

Commit

Permalink
fixed conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
kubitre committed Feb 7, 2022
1 parent d980612 commit ca2cd04
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
31 changes: 30 additions & 1 deletion extractors/extract_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"net/http"
"reflect"

"github.com/spf13/cast"
)

type ParserType string
Expand Down Expand Up @@ -70,13 +72,40 @@ func prepareInlineStructFields(request *http.Request, value reflect.Value, prepa
if parsedValue.Kind() == reflect.ValueOf(nil).Kind() {
return errors.New("in request does not exist query param with name: " + parsedTag)
}
val.Set(parsedValue)
setValueToType(val, parsedValue.String())
}
}
}
return nil
}

func setValueToType(val reflect.Value, parsedValue string) error {
if val.CanSet() {
return errors.New("can not setting value to this field: " + val.Addr().String())
}
switch val.Kind() {
case reflect.Int:
val.Set(reflect.ValueOf(cast.ToInt(parsedValue)))
case reflect.Int16:
val.Set(reflect.ValueOf(cast.ToInt16(parsedValue)))
case reflect.Int32:
val.Set(reflect.ValueOf(cast.ToInt32(parsedValue)))
case reflect.Int64:
val.Set(reflect.ValueOf(cast.ToInt64(parsedValue)))
case reflect.Float32:
val.Set(reflect.ValueOf(cast.ToFloat32(parsedValue)))
case reflect.Float64:
val.Set(reflect.ValueOf(cast.ToFloat64(parsedValue)))
case reflect.String:
val.Set(reflect.ValueOf(parsedValue))
case reflect.Bool:
val.Set(reflect.ValueOf(cast.ToBool(parsedValue)))
default:
return errors.New("error while setting up")
}
return nil
}

func getValueInAllExtractors(parsedTag string, request *http.Request, extractors []extractor) reflect.Value {
for _, extractFunc := range extractors {
if res := extractFunc(parsedTag, request); res != nil {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ module github.com/kubitre/go_api_infra

go 1.17

require github.com/gorilla/mux v1.8.0

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/mux v1.8.0
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/spf13/cast v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
google.golang.org/protobuf v1.26.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
4 changes: 3 additions & 1 deletion response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (responser *ApiResponse) writeResponse(status int, body interface{}) {
}

responser.ResponseWriter.WriteHeader(status)
responser.ResponseWriter.Write(respBody)
if body != nil {
responser.ResponseWriter.Write(respBody)
}
}

func (responser *ApiResponse) Ok(entity interface{}) {
Expand Down

0 comments on commit ca2cd04

Please sign in to comment.