Comments (6)
What if we checked for emptiness by comparing against the zero value of the type? For example:
var empty typeName
if z.someField == empty { /* is empty */ }
from msgp.
The unfortunate issue with this is we don't have a way of knowing if this will compile, since things like byte slices cannot be compared:
package main
type SomeType struct{ A []byte }
type SomeParentType struct{ SomeField SomeType }
func main() {
var z SomeParentType
var empty SomeType
if z.SomeField == empty { /* is empty */
}
}
Yields the error:
./prog.go:11:5: invalid operation: z.SomeField == empty (struct containing []byte cannot be compared)
In Playground: https://go.dev/play/p/XufWVZe1Fgn
In my case, I'm not sure if every custom type I have is comparable, thus the idea with the interface. If you feel really strongly about the issue I could probably make it work to refactor some things so everything is comparable, but for my case it's certainly less ideal and a fair bit more work. The interface gives a lot of flexibility, and I'm pretty sure the type check is a fast lookup, maybe it would even optimize down to something static - I can do some decompiling if there's a question on the performance.
from msgp.
The only issue I can see with the interface is that someone somewhere may have already attached an IsEmpty
method to a particular type that means something different. So we might want to make the new behavior opt-in somehow just to make sure existing code continues to work exactly as it does now.
from msgp.
I see what you mean and agree having this behavior be opt-in would be the least likely to break things for other users.
Using struct tags, two options come to mind:
A. SomeField SomeType `msg:"some_field,omitisempty"`
("omitisempty" would act like "omitempty" but with this additional behavior of checking the interface)
B. SomeField SomeType `msg:"some_field,omitempty,isemptyintf"`
(where "isemptyintf" is essentially an option to the omitempty tag)
And in either case this would basically just trigger emitting struct comparisons as the interface check instead of ==
, and in all other ways would keep the same omitempty behavior. Obviously primitive types like int
won't have methods, but if someone decided to do type A int
and then uses A as field type, it seems like omitisempty
should trigger the isEmpty interface check since it's entirely possible to add an IsEmpty method to type A. I'll have to look more closely at that specific case, but I don't think it has a big impact on the overall design of this option regardless.
My preference would be to lean toward option A as it seems more concise, let me know if you feel differently. I was trying to think if there would be some other appropriate way to trigger this behavior such as a command line flag, but I'm thinking a struct tag is the most apropos.
If you're good with option A, I'll put together a PR.
from msgp.
Implemented omitisempty
tag in PR #326
from msgp.
Implemented in #334
from msgp.
Related Issues (20)
- `map[int]int` failed to parse
- Compilation error about unused variable in generated *_gen.go file if the struct only has a single property
- Output size configuration
- unnecessary wrapping HOT 2
- new cmd option to generate 1 file per package HOT 1
- Can we use the `json` tag to generate code
- Add Linux and Windows binaries into the release section HOT 2
- Add example for marshaling array of structs into documentation HOT 1
- Can we have a new release tag? HOT 3
- "no definitions in issue185_test.go" HOT 1
- Support for go generics HOT 8
- Add generator option to always produce pointer receivers HOT 3
- Cut v1.1.9 or v1.2.0 release HOT 1
- Extension interface is too generic
- Flattening not working with non-local types HOT 1
- How to using UUID ? (GitHub.com/google/uuid) HOT 1
- Make `shim` directive handle map key HOT 1
- time.Duration type does not work HOT 3
- Release v1.2.0 HOT 2
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 msgp.