-
Notifications
You must be signed in to change notification settings - Fork 84
Add type interface{} target as argument to Field.Binder? #17
Comments
How about naming the Binder function and doing this: // proposed revision of https://gist.github.com/wmark/73ee2abe30e13af93a18#file-mailgun-go-L62-L70
&f.Signature: binding.Field{
Form: "signature",
Binder: bindHexField
}
// This function is now reusable!
func bindHexField(fieldName string, formVals []string, errs binding.Errors) binding.Errors {
messageMAC, err := hex.DecodeString(formVals[0])
if err != nil {
errs.Add([]string{fieldName}, binding.DeserializationError, err.Error())
} else {
f.Signature = messageMAC
}
return errs
} Now the |
Yes, The two variants are, (1): func BindHexEncodedField(field *[]byte) func(string, []string, binding.Errors) binding.Errors {
return func(fieldName string, formVals []string, errs binding.Errors) binding.Errors {
var err error // we cannot use := below because some versions of Golang would override the enclosed "field"
*field, err = hex.DecodeString(formVals[0])
if err != nil {
errs.Add([]string{fieldName}, binding.DeserializationError, err.Error())
}
return errs
}
}
…
func (f *MailgunParsedMessage) FieldMap() binding.FieldMap {
return binding.FieldMap{
…
&f.Signature: binding.Field{
Form: "signature",
Binder: BindHexEncodedField(&f.Signature),
},
…
}
} Obviously we should turn Variant (2) requires modifications to Field.Binder, bindForm and is: func HexEncodedFieldBinder(someField interface{}, fieldName string, formVals []string, errs binding.Errors) binding.Errors {
field := someField.(*[]bytes)
*field, err := hex.DecodeString(formVals[0])
if err != nil {
errs.Add([]string{fieldName}, binding.DeserializationError, err.Error())
}
return errs
}
…
func (f *MailgunParsedMessage) FieldMap() binding.FieldMap {
return binding.FieldMap{
…
&f.Signature: binding.Field{
Form: "signature",
Binder: HexEncodedFieldBinder,
},
…
}
} Re (1): Closures add some overhead and result in work for the GC. Type conversion becomes trivial, though. Requires no modifications to the package. Which one do you like the best? Or is there another variant I missed? |
I really prefer the first option. It has the advantage of not requiring changes to any code in the wild that's already using |
Very well, I've updated the example. If Matt agrees I would like to introduce this to type FieldBinderFunc func(string, []string, Errors) Errors
// or
type BinderSignature func(string, []string, Errors) Errors I am open to suggestions for the name. ;-) |
FieldBinderFunc seems right to me. |
I prefer the first variation. By the way, if a line like type BinderFunc func(string, []string, Errors) Errors would help simplify things, I'm okay with that. So before we go forward, can you help me understand what you're adding to the package, exactly? It doesn't look like this requires any real changes to the code, so is it some pre-defined Binder funcs that are being added? |
Matt, I am not adding anything at without a consensus. The only thing we agreed on is the line you've mentioned above. Until I let go of this issue:
|
Okay, yeah, I'm good with adding that line. I just wasn't sure if you were also adding some other functions. About the signature which receives binding.Errors as an argument, I think that was deemed necessary early on so that errors could be persisted throughout the validation process for an entire request (without having to resort to sessions or global variables or something). |
As incentive to delve into this topic I've drafted up a Gist to illustrate the current state of Binding's usage in the wild. I will introduce some ideas to Binding referring to that example:
https://gist.github.com/wmark/73ee2abe30e13af93a18#file-mailgun-go-L62-L70
In order to create a library (or to add it to Binding itself) we have to add another argument to Field.Binder, by which the function can access
formVals[]
' destination. (Please note the suggested functions in the comments.)Or, do you think using closures would suffice here and in other use-cases?
The text was updated successfully, but these errors were encountered: