GithubHelp home page GithubHelp logo

Comments (8)

zond avatar zond commented on August 17, 2024

Also solved by #6 I think.

from go.

ugorji avatar ugorji commented on August 17, 2024

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.

zond avatar zond commented on August 17, 2024

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.

ugorji avatar ugorji commented on August 17, 2024

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.

zond avatar zond commented on August 17, 2024

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.

ugorji avatar ugorji commented on August 17, 2024

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.

zond avatar zond commented on August 17, 2024

I am happy that you see my point, and am looking forward to your considered opinion on the matter :)

from go.

ugorji avatar ugorji commented on August 17, 2024

The latest commit should fix this for both maps and slices. Thanks for bringing up the issue.

from go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.