Comments (7)
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.
This seems to do what you want: https://go.dev/play/p/K4rYQ7Oeq0k
from go-querystring.
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.
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.
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.
(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.
Thanks so much, yes I think that would be clearer 😄
from go-querystring.
Related Issues (20)
- Support alternative struct tags (instead of `url`) HOT 6
- Allow arbitrary delimiter for slice/array type params
- Parsing query strings into a map HOT 3
- Support for Unmarshaler / Marshaler HOT 2
- "query string" for GitHub search HOT 1
- add `decode` method HOT 1
- Slice (Array) pointer does not result in array query values HOT 1
- Is there a way to write a custom marshaller for my custom type? HOT 1
- Unable to handle nil pointers when implementing Encoder HOT 6
- can you please update the tag? HOT 3
- Querystring not adding [] for the array types HOT 1
- Add support for JSON encoding a struct field
- Different behavior on encoding empty slice with any del tag and without HOT 2
- Supporting slices HOT 2
- Structure fields original order messed up HOT 2
- Support RFC Specific Encoding HOT 7
- How to encode nested structure by my way? HOT 1
- Support pointer method (*Type)EncodeValues on the Encoder interface HOT 2
- Add support for struct field name casing options
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-querystring.