Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Respect default schema value #49

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func bindSplitPartsToDestinationStruct(paramName string, parts []string, explode
// tell them apart. This code tries to fail, but the moral of the story is that
// you shouldn't pass objects via form styled query arguments, just use
// the Content parameter form.
func BindQueryParameter(style string, explode bool, required bool, paramName string,
func BindQueryParameter(style string, explode bool, required bool, defaultV any, paramName string,
queryParams url.Values, dest interface{}) error {

// dv = destination value.
Expand Down Expand Up @@ -373,10 +373,14 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
if !found {
if required {
return fmt.Errorf("query parameter '%s' is required", paramName)
} else if defaultV != nil {

} else {
// If an optional parameter is not found, we do nothing,
return nil
}

values = []string{fmt.Sprintf("%v", defaultV)}
}
err = bindSplitPartsToDestinationArray(values, output)
case reflect.Struct:
Expand All @@ -397,9 +401,13 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
if len(values) == 0 {
if required {
return fmt.Errorf("query parameter '%s' is required", paramName)
} else if defaultV != nil {

} else {
return nil
}

values = []string{fmt.Sprintf("%v", defaultV)}
}
if len(values) != 1 {
return fmt.Errorf("multiple values for single value parameter '%s'", paramName)
Expand All @@ -408,6 +416,8 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
if !found {
if required {
return fmt.Errorf("query parameter '%s' is required", paramName)
} else if defaultV != nil {

} else {
// If an optional parameter is not found, we do nothing,
return nil
Expand All @@ -430,9 +440,13 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
if !found {
if required {
return fmt.Errorf("query parameter '%s' is required", paramName)
} else if defaultV != nil {

} else {
return nil
}

values = []string{fmt.Sprintf("%v", defaultV)}
}
if len(values) != 1 {
return fmt.Errorf("parameter '%s' is not exploded, but is specified multiple times", paramName)
Expand All @@ -449,6 +463,8 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
if len(parts) == 0 {
if required {
return fmt.Errorf("query parameter '%s' is required", paramName)
} else if defaultV != nil {

} else {
return nil
}
Expand Down