-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
Binding uuid.UUID using ShouldBind #2423
Comments
I have the same issue when attempting to bind with url-safe base64 values, I couldn't find a way to define a custom type handle the binding and error handling on its own and had to basically decode the string and handle the error in every handler. it's possible to use custom types when using Maybe It could be seen as API-breaking though, since |
I have this problem too, but I found solution! You can change uuid.UUID to string and place in binding uuid tag! |
If you use github.com/google/uuid package, you can use uuid.Parse example: reference : |
+1. Any way to serialization and deserialization uri bindding or query bindding? |
This function can be used to try to assign UUID to reflect.Value. package binding
import (
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func tryAssignUUID(s string, value reflect.Value) bool {
l := len(s)
if value.Kind() == reflect.Array &&
value.Len() == 16 &&
value.Index(0).Kind() == reflect.Uint8 &&
(l == 32 || l == 36 || l == 38 || l == 41 || l == 45) {
if uid, uErr := uuid.FromString(s); uErr == nil {
value.Set(reflect.ValueOf(uid))
return true
}
}
return false
}
func Test_tryAssignUUID(t *testing.T) {
mm := map[string][]string{
"id": []string{"df67786a-a543-4958-87ca-426954c626d5"},
"ids": []string{"df67786a-a543-4958-87ca-426954c626d5", "68dc3815-6ab5-4883-81a1-96eff25b659f"},
}
type UUIDs struct {
Id uuid.UUID `uri:"Id"`
Ids []uuid.UUID `uri:"Ids"`
}
var received UUIDs
if err := mapFormByTag(&received, mm, "uri"); err != nil {
t.Error(err)
}
expected := UUIDs{
Id: uuid.FromStringOrNil("df67786a-a543-4958-87ca-426954c626d5"),
Ids: []uuid.UUID{
uuid.FromStringOrNil("df67786a-a543-4958-87ca-426954c626d5"),
uuid.FromStringOrNil("68dc3815-6ab5-4883-81a1-96eff25b659f"),
},
}
assert.Equal(t, received, expected)
} To make it work with arrays I added a case reflect.Array:
if tryAssignUUID(val, value) {
return nil
}
return errUnknownType And modification for case reflect.Array:
if !ok {
vs = []string{opt.defaultValue}
}
if len(vs) != value.Len() {
if len(vs) == 1 && tryAssignUUID(vs[0], value) {
return true, nil
}
return false, fmt.Errorf("%q is not valid value for %s", vs, value.Type().String())
}
return true, setArray(vs, value, field) |
It would be preferable to merge #3045, though. |
Description
Binding uuid.UUID using ShouldBind gives error ["45e1f85e-bca5-458d-bd9c-c56edd8f847b"] is not valid value for uuid.UUID
How to reproduce
Used Postman to make request:
Content-Type: multipart/form-data
Body with form-data:
key: colorID
value: 45e1f85e-bca5-458d-bd9c-c56edd8f847b
Actual result
The text was updated successfully, but these errors were encountered: