-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add support for a Scanner interface. #764
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
Conversation
So this is specifically for form data? |
It should work for any input type that |
Do you mean JSON codec takes care of calling |
Sorry, no. My mind is a bit of a muddle. This PR would directly affect three types of inputs:
Where it could potentially affect JSON, etc, is that by implementing the I hope things are clearer now. |
At this point, I believe this PR to be functionally complete. Further discussion or requests for clarification are welcome, of course. |
I spent some time thinking about this PR, and a couple issues came up in my mind.
|
I think this is a good solution to the problem. It will allow anyone to specify their own unmarshalling logic for their own types by implementing the Scan() method on the type. Right now if someone wanted to do something outside of the norm then they'd have to implement their own custom binder, which I think is totally un-necessary. It's a lot more work to maintain than simply maintaining a Scan function for the types they want to support, not to mention they may have already done that because they need it for SQL scanning anyway. |
After discussion with my own team, I decided that borrowing the Scanner interface isn't ideal, so have renamed the interface in this PR. Alternate naming suggestions are welcome. If this is accepted for merging, I should rebase to squash these commits. |
Bind data is not limited to forms, it can also bind query parameters, so naming it with form won't be appropriate. How about: type interface Unmarshaller {
Unmarshal(data string) error
} data could be |
@vishr Both great suggestions. I wonder, though, if |
I would say |
It's actually |
How about this? type Unmarshaler interface {
UnmarshalData([]byte) error
} |
Looks good to me.
…On Wed, 14 Dec 2016 at 17:08 Vishal Rana ***@***.***> wrote:
How about this?
type Unmarshaler interface {
UnmarshalData([]byte) error
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#764 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AB8uSZ0_WpNWVkcnv2r1FEW2dqcIqYFIks5rIBSGgaJpZM4LFtfs>
.
|
Update: Another point in favour of |
You make a good argument in favor of What about In any case, we've probably done enough bike shedding. Whichever your preference is, @vishr, is fine with me, and I'm happy to make the changes for this PR. |
PS: Isn't interface name |
Interface names don't collide, since they, like other data types, are per-package, and can even be private (unexported) and still work just as well. So we can call it whatever we want. |
I understand, I was concerned within the package as it is too general. In Echo, we usually prepend with the behavior like Log, Bind as package is flat. Let's call it |
Oh of course... within the package, yes, you may want other unmarshalers as well. |
This is inspired by interfaces like json.Unmarshaler, and can be used to unmarshal custom data types from form/query data.
I have added a mention in the documentation; let me know if it should be expanded, or further mentioned elsewhere. I also squished the commits, as they were becoming somewhat long-winded and verbose. |
At one point you suggested using Internally echo uses |
Lets keep it string, as param will always be string in our case. |
thanks for your contribution 👍 |
This duplicates the Scanner interface implemented by the standard library's sql package, extending echo's standard Binder to support arbitrary data types, so long as they implement this interface.
This solves #763.