Comments (13)
Any reason you can't simply set the field to ""
before passing the struct to the decoder?
from schema.
I may be misunderstanding, but on lines 139-141 in decoder.go, it expressly ignores the value when it is set to ""
. This is not the same as unsetting the value. In my code, I'm taking a pre-populated struct and having the decoder overwrite the struct. If any of the incoming data is set to ""
, it'll just skip it instead of setting the written-to struct as empty string, which leaves the original value in place. Let me know if what you said is not what I seemed to have interpreted.
from schema.
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 ""
, you want it to blank out the corresponding field in the struct. The current behaviour just ignores the form value and leaves the field in the struct intact.
Is that the gist of it?
from schema.
That is correct.
from schema.
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.
from schema.
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 schema
code, but it seems that the absence of a field is able to be known and that it could work out. I'll see about making a workable solution and we can discuss if it is a reasonable solution. I just want to be sure there wasn't already a best practice that doesn't suck (like pre-pre-processing my inputs, for example). Thanks.
from schema.
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.
from schema.
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.
from schema.
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?
from schema.
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.
from schema.
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.
from schema.
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?
from schema.
Tests would definitely help, thanks.
from schema.
Related Issues (20)
- [bug] example links to itself for more documentation, no other documentation? HOT 2
- [bug] help Empty strings in an array
- [bug] Registered encoder doesn't work for struct pointer types
- [bug] Nested fields not encoded in dot format HOT 1
- [bug] Required check bypassed if there is another field whose prefix is the field HOT 3
- [question] Unable to decode slice of structs HOT 1
- [feature] New release/tag HOT 1
- [feature] default values for scheme struct fields HOT 13
- [feature] Use json struct tags if wanted HOT 2
- [question] "+" symbol HOT 6
- ⚠️ The Gorilla Toolkit is Looking for a New Maintainer HOT 10
- [bug] Unstable decoding of different keys that are case-folded to the same value HOT 1
- Not checking for nil might lead to nil pointer dereference HOT 2
- [question] Error converting into []byte or []uint8 HOT 2
- [bug] schema: converter not found for Page, when the struct name is same with field name. HOT 4
- [question] Why can't Encoder check and call fmt.Stringer.String() on element automatically?
- [BUG] v1.2.0 Cannot decode into slice of strings HOT 2
- [bug] need to skip unexported field
- [BUG] Nested struct with required field is not failing on decode
- [FEATURE] Documentation HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schema.