Comments (6)
Could you not just use a pointer to your custom type to allow for the same kind of nil checking?
For example, the following works as I would expect, and I think as you're describing:
package main
import (
"fmt"
"net/url"
"github.com/google/go-querystring/query"
)
type T int
func (t T) EncodeValues(key string, v *url.Values) error {
v.Add(key, fmt.Sprintf("_%d_", t))
return nil
}
type options struct {
*T `url:"t,omitempty"`
}
func main() {
v, _ := query.Values(options{})
fmt.Println(v.Encode()) // will output: ""
v, _ = query.Values(options{T: new(T)})
fmt.Println(v.Encode()) // will output: "t=_0_"
}
If that doesn't work for you, could you share a code snippet of what you're trying to do?
from go-querystring.
oh, the other thing you can do is implement the zeroable
interface by adding an IsZero() bool
method on your custom type. That way, you can do whatever kind of zero checking makes sense for that type. But again, I'm not sure if that covers your use case or not.
from go-querystring.
Sorry I wasn't clear enough. Was trying to do something like this https://play.golang.org/p/wnzO0oIJI5i
package main
import (
"fmt"
"net/url"
"github.com/google/go-querystring/query"
)
type T int
// EncodeValues encodes a 0 as false, 1 as true, and nil as unknown
func (t *T) EncodeValues(key string, v *url.Values) error {
if t == nil {
v.Add(key, "unknown")
return nil
}
if *t == 0 {
v.Add(key, "false")
return nil
}
if *t == 1 {
v.Add(key, "true")
return nil
}
return fmt.Errorf("value not supported")
}
type options struct {
*T `url:"t"`
}
func main() {
v, _ := query.Values(options{})
fmt.Println(v.Encode()) // will output: false, expected unknown
}
from go-querystring.
Got it, that makes sense.
That will definitely take a little bit of work to try and make it possible. I tried a naive approach, and keep running into places throughout the package that chokes on nil values because it assumes it gets handled earlier :)
from go-querystring.
Actually, I'm not even sure if this is possible at all. Since it's a nil pointer, I don't believe there's any way to know what type it would have pointed to is. I believe nil interfaces carry their type information along, but not nil pointers. I could be wrong on that, and will keep looking into it, just sort of thinking out loud here.
Yeah, scratch that (I think)
from go-querystring.
I'm actually trying to do the opposite to the examples you added into the tests. I'm trying to convert a bool false
to 0
and true
to 1
.
I'm having an issue with a non-pointer (e.g. T(false)
) not triggering EncodeValues()
to be called. Which means the false
is omitted from the encoded output as if it was never set, when it was.
Here's an example of what I'm trying:
https://goplay.tools/snippet/DMgWjMLCtu1
NOTE: In my example I could remove the
omitempty
from theTee
field's struct tag but then the first example where I don't set theTee
field would then be set to its zero value and I have no way to know if it was set to false or just omitted.
Not sure if you're able to offer any guidance on how best to achieve this. Thanks.
from go-querystring.
Related Issues (20)
- Encoder interface not respected for `interface{}` types HOT 2
- 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
- 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
- Custom `String` implementations are not respected HOT 7
- 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.