Skip to content

Commit

Permalink
change operation signature
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Jul 9, 2023
1 parent ba3f9d3 commit bd6aa23
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
6 changes: 3 additions & 3 deletions operation_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var (
type DecodeOperationBody struct {
}

func (d *DecodeOperationBody) Decode(ctx DecodeContext, r *http.Request, field reflect.Value,
typ reflect.Type, tag *Tag) (bool, any, error) {
func (d *DecodeOperationBody) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {

if r.Body == nil {
return false, nil, nil
Expand All @@ -31,7 +31,7 @@ func (d *DecodeOperationBody) Decode(ctx DecodeContext, r *http.Request, field r
return false, nil, errors.New("body operation not allowed")
}
if ctx.IsBodyDecoded() {
return false, nil, fmt.Errorf("body was already decoded for type '%s'", typ.String())
return false, nil, fmt.Errorf("body was already decoded")
}

return decodeBody(ctx, r, field, tag)
Expand Down
6 changes: 3 additions & 3 deletions operation_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
type DecodeOperationForm struct {
}

func (d *DecodeOperationForm) Decode(ctx DecodeContext, r *http.Request, field reflect.Value,
typ reflect.Type, tag *Tag) (bool, any, error) {
func (d *DecodeOperationForm) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {
var form multipart.Form

err := r.ParseForm()
Expand All @@ -40,7 +40,7 @@ func (d *DecodeOperationForm) Decode(ctx DecodeContext, r *http.Request, field r

ctx.ValueUsed(OperationForm, tag.Name)

if field.Kind() == reflect.Slice {
if isList {
return true, values, nil
}
return true, values[0], nil
Expand Down
9 changes: 3 additions & 6 deletions operation_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ import (
type DecodeOperationHeader struct {
}

func (d *DecodeOperationHeader) Decode(ctx DecodeContext, r *http.Request, field reflect.Value,
typ reflect.Type, tag *Tag) (bool, any, error) {
func (d *DecodeOperationHeader) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {
values := r.Header.Values(tag.Name)

if len(values) == 0 {
return false, nil, nil
}

// only check slices/arrays for primitive types, otherwise "type UUID [16]byte" would be checked as an array
isPrimitive := field.Type().PkgPath() == ""

if isPrimitive && (field.Kind() == reflect.Slice || field.Kind() == reflect.Array) {
if isList {
return true, values, nil
}
return true, values[0], nil
Expand Down
6 changes: 3 additions & 3 deletions operation_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
type DecodeOperationPath struct {
}

func (d *DecodeOperationPath) Decode(ctx DecodeContext, r *http.Request, field reflect.Value,
typ reflect.Type, tag *Tag) (bool, any, error) {
func (d *DecodeOperationPath) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {
if ctx.PathValue() == nil {
return false, nil, fmt.Errorf("path value function not set for type '%s'", typ.Name())
return false, nil, fmt.Errorf("path value function not set")
}

ctx.ValueUsed(OperationPath, tag.Name)
Expand Down
9 changes: 3 additions & 6 deletions operation_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ import (
type DecodeOperationQuery struct {
}

func (d *DecodeOperationQuery) Decode(ctx DecodeContext, r *http.Request, field reflect.Value,
typ reflect.Type, tag *Tag) (bool, any, error) {
func (d *DecodeOperationQuery) Decode(ctx DecodeContext, r *http.Request, isList bool, field reflect.Value,
tag *Tag) (bool, any, error) {
if !r.URL.Query().Has(tag.Name) {
return false, nil, nil
}

// only check slices/arrays for primitive types, otherwise "type UUID [16]byte" would be checked as an array
isPrimitive := field.Type().PkgPath() == ""

if isPrimitive && (field.Kind() == reflect.Slice || field.Kind() == reflect.Array) {
if isList {
explode, err := tag.Options.BoolValue("explode", false)
if err != nil {
return false, nil, err
Expand Down

0 comments on commit bd6aa23

Please sign in to comment.