-
Notifications
You must be signed in to change notification settings - Fork 225
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
Deliberately set empty field #17
Comments
Any reason you can't simply set the field to |
I may be misunderstanding, but on lines 139-141 in decoder.go, it expressly ignores the value when it is set to |
Okay, let's see if I understand correctly: You have a struct that is prefilled with data. If the form value for a field is Is that the gist of it? |
That is correct. |
Okay, I get it then :) So I think the best way to do this is to add an option to the decoder that will clear the fields. We can't change the default behaviour because it may break existing usage. |
In my use case, it's important that any omitted fields (from a form post, for example) are just ignored, yet any actually empty but submitted fields are cleared. Do you believe that such a thing can be done without the hackery I've applied in my not-to-be-used commit I've linked above? I honestly haven't thoroughly evaluated the |
It should work out that way since we iterate over the keys in the map you receive from the form. If the key is not there we will never try to change the field. |
This is actually partially addressed in #12 , but the solution there is not quite complete (eg: it changes the default behaviour, and no tests yet), which is why I haven't merged it. |
Oh, hah. Well I didn't even see that one apparently. That'd be probably what I want on my end. What would be helpful for moving that forward? Anything I can do? |
It looks untested and probably incomplete. I merged his changes into my HEAD^ and I'll see about adding the test cases I care about. My time today is rather tight, so I hope I actually complete it. I'm going to close this issue and let RangelReale's issue #12 carry this forward as it seems to have compatible goals. Thanks. |
Thanks for the feedback though, now I have a pretty good idea of how that PR should work. I'd like to add another function to the decoder like dec.ZeroEmpty(). When called it will set a flag on the decoder. If the decoder encounters an empty string, it will check the value of the flag. If the flag is set, then it will set the field to the zero value as it does in that PR, otherwise it will ignore it. |
Sounds good to me. Would it be helpful for me to write the tests that I'm concerned about, or shall I wait until you've made proposed changes? |
Tests would definitely help, thanks. |
I need a way to explicitly set a field value to empty and not have it be ignored by gorilla/schema when processed. It is important to not simply wipe out all missing fields, but only make empty the fields that were provided and somehow marked as intentionally empty.
Use case: struct that is pre-populated from a database entry that is then passed into schema.Decoder to apply matched fields from a web form.
Possibly if the fields exist in the incoming web form and are empty, they can be wiped out while leaving alone the fields in the struct that weren't referenced in the web form. Such a thing would need testing that I haven't myself done.
I've created a very hacky patch[1] to allow deliberately unsetting a field value when empty, instead of just ignoring it. In an application I'm building, I pull in a record from the database and then use schema to merge changes. When the data coming in from a web form is empty, I intend for the value to be wiped out, not just ignored. But because of how structs work, it's really not easy to tell the difference between absent field versus a deliberately empty value. In this very hacky patch, if it hits a string value of "[[[ERASE]]]" it will set the field value to "". It's not good enough and is bound to have loads of gotchas. Maybe there an alternative way to do this that I've missed; if so, sorry for the wishlist request.
[1] scjalliance@fdf73b8 (don't actually use this commit)
The text was updated successfully, but these errors were encountered: