Comments (8)
Also solved by #6 I think.
from go.
I thought long and hard about this one. In the notes on decode.go, I had these comments.
// Note: In decoding into containers, we just use the stream to UPDATE the container.
// This means that for a struct or map, we just update matching fields or keys.
// For a slice/array, we just update the first n elements, where n is the length of the
// stream.
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.
from go.
Oh, for my application the paramount property of the encoding is reflect.DeepEqual(data, Decode(Encode(data))
.
Could you perhaps add tests to the repository that define your required behaviour - perhaps I can make the code fulfill both requirements?
from go.
The full model is documented as:
// Note: In decoding into containers, we just use the stream to UPDATE the container.
// This means that for a struct or map, we just update matching fields or keys.
// For a slice/array, we just update the first n elements, where n is the length of the
// stream.
// However, if the encoded value is Nil in the stream, then we try to set
// to the corresponding "zero" value.
//
// Also, we must ensure that, if decoding into a nil interface{}, we set to a non-nil
// value except even if the container registers a length of 0.
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.
from go.
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.
from go.
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.
from go.
I am happy that you see my point, and am looking forward to your considered opinion on the matter :)
from go.
The latest commit should fix this for both maps and slices. Thanks for bringing up the issue.
from go.
Related Issues (20)
- codec.MsgpackHandle.RawToString is confusingly named HOT 2
- Go 1.20 - invalid memory address or nil pointer dereference HOT 5
- Msgpack deserialization with codecgen produces wrong result HOT 3
- Unexpected encoding with interface{} key using Canonical and MapKeyAsString.
- Decoding a slice of structs encoded as arrays into a slice of structs without the same number of fields HOT 2
- Duplicate key inside of a map triggers a "cannot decode unsigned integer" msgpack decode error HOT 1
- Efficient direct conversion between supported formats? HOT 2
- Expose Handle.Name() in some form through Encoder and Decoder for use by codec.Selfer HOT 1
- `io.ErrUnexpectedEOF` should be returned instead of `io.EOF` when only partial data is present
- Add `DecodeOptions.IntType` and `DecodeOptions.FloatType` ? HOT 1
- Docs do not seem to mention case-sensitive field handling
- mem leak? HOT 1
- Tests use rand.Intn(1) for boolean rand, but always returns 0
- Decoder accepts indefinite-length CBOR strings containing chunks with mismatched major types HOT 1
- UTF-8 validation isn't applied to CBOR text strings nested inside an indefinite-length text string HOT 2
- Add support for github.com/go-json-experiment/json / UnmarshalerV2 ? HOT 1
- runtime error with golang master (furture go 1.22)
- Leading zeros is not allowed in rfc7159
- attr.go
- [Question] Big Endian architecture
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 go.