Giter Club home page Giter Club logo

Comments (7)

willnorris avatar willnorris commented on August 22, 2024

because type Foo is a struct, the default behavior is for go-querystring to try and process the fields of that struct. In this case, it has no fields, so there's nothing to encode. If you want custom behavior, you would need to implement the query.Encoder interface.

The reason a value was being returned in #45 (I think) is because the field in question was an int, which defaults to its string representation.

from go-querystring.

willnorris avatar willnorris commented on August 22, 2024

This seems to do what you want: https://go.dev/play/p/K4rYQ7Oeq0k

from go-querystring.

zakcutner avatar zakcutner commented on August 22, 2024

Thanks for answering so quickly! I don't completely agree about #45 since they said

// got "level=info", want "level=1"

If it was using a default string representation, wouldn't level=1 be returned? In any case, the docs say that

All other values are encoded using their default string representation.

Sorry if I'm missing something but it seems to me that this behaviour conflicts with that.

from go-querystring.

willnorris avatar willnorris commented on August 22, 2024

ah, you're right about it not being the default string representation. But the difference in behavior is still due to using a struct versus non-struct field. The relevant code is here.

In your case, sv.Kind() of your Foo field does return a struct, so it is processed as a struct. In the case of #45, sv.Kind() is an int, so it gets processed by the valueString() func. Which as you point out, uses the String() method of the type if present, hence the "level=info".

The existing docs do talk about struct fields a little bit, but if there's a way we can describe this behavior better to make it more obvious, I'd welcome that.

from go-querystring.

willnorris avatar willnorris commented on August 22, 2024

Maybe it could be as simple as changing this line in the docs:

Nested structs are encoded including parent fields in value names for scoping.

to something like:

Nested structs have their fields processed recursively and are encoded including parent fields in value names for scoping.

Would that be clearer?

from go-querystring.

willnorris avatar willnorris commented on August 22, 2024

(And here's a quick example I did to double check myself that sv.Kind() returns the underlying kind, even for custom types... https://go.dev/play/p/MJUTVO5RZWn)

from go-querystring.

zakcutner avatar zakcutner commented on August 22, 2024

Thanks so much, yes I think that would be clearer 😄

from go-querystring.

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.