-
Notifications
You must be signed in to change notification settings - Fork 295
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
Decoding empty list/map in stream into a nil list/map destination should update destination to empty list/map #7
Comments
Also solved by #6 I think. |
I thought long and hard about this one. In the notes on decode.go, I had these comments.
This means that if you try to decode an empty map stream into a map with 10 keys, the map with 10 keys is unchanged. If you try to decode an empty list stream into a slice with 10 elements, the slice with 10 elements is unchanged. If you try to decode a 2-element list stream into a slice with 10 elements, the first 2 elements of the slice are modified. If you try to decode an empty list stream into a nil slice, the nil slice is unchanged. I did this for consistency. There are many ways to tackle this, but this simple consistent model felt most natural to me. I understand that different folks may want to tackle this differently, but one way had to be chosen. I will move some of these notes to the package docs. |
Oh, for my application the paramount property of the encoding is Could you perhaps add tests to the repository that define your required behaviour - perhaps I can make the code fulfill both requirements? |
The full model is documented as:
To get what you want, decode into a non-nil value (e.g. decode into []byte{}, not []byte(nil)). reflect.DeepEqual should then work. I want to think about a better model for the tests using a cleaner table-driven model, so we don't add tests organically. Please bear with me while I find some time in the next few weeks to do it. When I do, I'd work on adding some tests for this. |
I see your reasons, but I have a big nested structure in the form of a struct type variable, where the slice is nested quite deep. Initializing this struct to let the slice be non-nil is a big code stink that I want to avoid :/ Basically, I want to use your project as a faster encoding/gob or encoding/json. |
Ah ... very valid point. That's a very important use-case that I don't account for. Let me think/work this through, and follow up by tomorrow. |
I am happy that you see my point, and am looking forward to your considered opinion on the matter :) |
The latest commit should fix this for both maps and slices. Thanks for bringing up the issue. |
The following code:
Produces:
Empty slices are not always nil..
The text was updated successfully, but these errors were encountered: