Skip to content

Commit

Permalink
Move interface assertions under static flags
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Feb 8, 2023
1 parent 6a176e8 commit f931b97
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/swaggest/jsonschema-go v0.3.47
github.com/swaggest/openapi-go v0.2.28
github.com/swaggest/swgui v1.5.1
github.com/swaggest/usecase v1.2.0
github.com/swaggest/usecase v1.2.1
github.com/valyala/fasthttp v1.44.0
)

Expand Down
4 changes: 2 additions & 2 deletions _examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ github.com/swaggest/refl v1.1.0 h1:a+9a75Kv6ciMozPjVbOfcVTEQe81t2R3emvaD9oGQGc=
github.com/swaggest/refl v1.1.0/go.mod h1:g3Qa6ki0A/L2yxiuUpT+cuBURuRaltF5SDQpg1kMZSY=
github.com/swaggest/swgui v1.5.1 h1:CXJ2l9n1nEz2AJXl/FHDGD5ZtlKnTRJb2p86njBfXfo=
github.com/swaggest/swgui v1.5.1/go.mod h1:lKfAIKTFBStjUtyXz56OQpXgQtpGZAWWIBv4QJtxRQA=
github.com/swaggest/usecase v1.2.0 h1:cHVFqxIbHfyTXp02JmWXk+ZADaSa87UZP+b3qL5Nz90=
github.com/swaggest/usecase v1.2.0/go.mod h1:oc5+QoAxG3Et5Gl9lRXgEOm00l4VN9gdVQSMIa5EeLY=
github.com/swaggest/usecase v1.2.1 h1:XYVdK9tK2KCPglTflUi7aWBrVwIyb58D5mvGWED7pNs=
github.com/swaggest/usecase v1.2.1/go.mod h1:5ccwVsLJ9eQpU4m0AGTM444pdqSPQBiocIwMmdRH9lQ=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.44.0 h1:R+gLUhldIsfg1HokMuQjdQ5bh9nuXHPIfvkYUu9eR5Q=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/swaggest/jsonschema-go v0.3.47
github.com/swaggest/openapi-go v0.2.28
github.com/swaggest/refl v1.1.0
github.com/swaggest/usecase v1.2.0
github.com/swaggest/usecase v1.2.1
)

require (
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ github.com/bool64/dev v0.2.5/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zR
github.com/bool64/dev v0.2.10/go.mod h1:/csLrm+4oDSsKJRIVS0mrywAonLnYKFG8RvGT7Jh9b8=
github.com/bool64/dev v0.2.16/go.mod h1:/csLrm+4oDSsKJRIVS0mrywAonLnYKFG8RvGT7Jh9b8=
github.com/bool64/dev v0.2.17/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.20/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.22/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.24 h1:xptlKivPh870W3Xc9szPcM7wkFmTMuHT8rc0nu7dITk=
github.com/bool64/dev v0.2.24/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
Expand Down Expand Up @@ -82,8 +81,8 @@ github.com/swaggest/openapi-go v0.2.28 h1:PRgJTqRfpsWte7qPqMFJooLu8cWyL91FhpYH2k
github.com/swaggest/openapi-go v0.2.28/go.mod h1:2RAaFLmRFQiR8TQJyIZzh2NTY1ihk3/WxYJFxQ+TfFU=
github.com/swaggest/refl v1.1.0 h1:a+9a75Kv6ciMozPjVbOfcVTEQe81t2R3emvaD9oGQGc=
github.com/swaggest/refl v1.1.0/go.mod h1:g3Qa6ki0A/L2yxiuUpT+cuBURuRaltF5SDQpg1kMZSY=
github.com/swaggest/usecase v1.2.0 h1:cHVFqxIbHfyTXp02JmWXk+ZADaSa87UZP+b3qL5Nz90=
github.com/swaggest/usecase v1.2.0/go.mod h1:oc5+QoAxG3Et5Gl9lRXgEOm00l4VN9gdVQSMIa5EeLY=
github.com/swaggest/usecase v1.2.1 h1:XYVdK9tK2KCPglTflUi7aWBrVwIyb58D5mvGWED7pNs=
github.com/swaggest/usecase v1.2.1/go.mod h1:5ccwVsLJ9eQpU4m0AGTM444pdqSPQBiocIwMmdRH9lQ=
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff h1:7YqG491bE4vstXRz1lD38rbSgbXnirvROz1lZiOnPO8=
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff/go.mod h1:sw49aWDqNdRJ6DYUtIQiaA3xyj2IL9tjeNYmX2ixwcU=
github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=
Expand Down
18 changes: 12 additions & 6 deletions request/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,26 @@ func makeDecoder(in rest.ParamIn, formDecoder *form.Decoder, decoderFunc decoder

// decoder extracts Go value from *http.Request.
type decoder struct {
decoders []valueDecoderFunc
in []rest.ParamIn
decoders []valueDecoderFunc
in []rest.ParamIn
isReqLoader bool
isReqSetter bool
}

var _ nethttp.RequestDecoder = &decoder{}

// Decode populates and validates input with data from http request.
func (d *decoder) Decode(r *http.Request, input interface{}, validator rest.Validator) error {
if i, ok := input.(Setter); ok {
i.SetRequest(r)
if d.isReqSetter {
if i, ok := input.(Setter); ok {
i.SetRequest(r)
}
}

if i, ok := input.(Loader); ok {
return i.LoadFromHTTPRequest(r)
if d.isReqLoader {
if i, ok := input.(Loader); ok {
return i.LoadFromHTTPRequest(r)
}
}

for i, decode := range d.decoders {
Expand Down
44 changes: 29 additions & 15 deletions request/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,16 @@ func (df *DecoderFactory) MakeDecoder(
input interface{},
customMapping rest.RequestMapping,
) nethttp.RequestDecoder {
m := decoder{
decoders: make([]valueDecoderFunc, 0),
in: make([]rest.ParamIn, 0),
}
d := initDecoder(input)

if df.ApplyDefaults && refl.HasTaggedFields(input, defaultTag) {
df.makeDefaultDecoder(input, &m)
df.makeDefaultDecoder(input, &d)
}

cm := df.prepareCustomMapping(input, customMapping)

if len(cm) > 0 {
df.makeCustomMappingDecoder(cm, &m)
df.makeCustomMappingDecoder(cm, &d)
}

for in, formDecoder := range df.formDecoders {
Expand All @@ -118,8 +115,8 @@ func (df *DecoderFactory) MakeDecoder(

if refl.HasTaggedFields(input, string(in)) {
df.jsonParams(formDecoder, in, input)
m.decoders = append(m.decoders, makeDecoder(in, formDecoder, df.decoderFunctions[in]))
m.in = append(m.in, in)
d.decoders = append(d.decoders, makeDecoder(in, formDecoder, df.decoderFunctions[in]))
d.in = append(d.in, in)
}
}

Expand All @@ -128,7 +125,7 @@ func (df *DecoderFactory) MakeDecoder(
_, forceRequestBody := input.(openapi3.RequestBodyEnforcer)

if method != http.MethodPost && method != http.MethodPut && method != http.MethodPatch && !forceRequestBody {
return &m
return &d
}

hasFormData := refl.HasTaggedFields(input, formDataTag)
Expand All @@ -137,20 +134,37 @@ func (df *DecoderFactory) MakeDecoder(
if refl.HasTaggedFields(input, jsonTag) || refl.FindEmbeddedSliceOrMap(input) != nil ||
refl.IsSliceOrMap(input) || refl.IsScalar(input) {
if df.JSONReader != nil {
m.decoders = append(m.decoders, decodeJSONBody(df.JSONReader, hasFormData))
d.decoders = append(d.decoders, decodeJSONBody(df.JSONReader, hasFormData))
} else {
m.decoders = append(m.decoders, decodeJSONBody(readJSON, hasFormData))
d.decoders = append(d.decoders, decodeJSONBody(readJSON, hasFormData))
}

m.in = append(m.in, rest.ParamInBody)
d.in = append(d.in, rest.ParamInBody)
}

if hasFileFields(input, fileTag) || hasFileFields(input, formDataTag) {
m.decoders = append(m.decoders, decodeFiles)
m.in = append(m.in, rest.ParamInFormData)
d.decoders = append(d.decoders, decodeFiles)
d.in = append(d.in, rest.ParamInFormData)
}

return &d
}

func initDecoder(input interface{}) decoder {
d := decoder{
decoders: make([]valueDecoderFunc, 0),
in: make([]rest.ParamIn, 0),
}

if _, ok := input.(Loader); ok {
d.isReqLoader = true
}

if _, ok := input.(Setter); ok {
d.isReqSetter = true
}

return &m
return d
}

func (df *DecoderFactory) prepareCustomMapping(input interface{}, customMapping rest.RequestMapping) rest.RequestMapping {
Expand Down

0 comments on commit f931b97

Please sign in to comment.