Comments (5)
Alternatively, allow marshal transform func to return some marker error/value?
from refmt.
So to recap, the matrix of transforms that seem potentially reasonable here are:
- when populating a struct from a null token:
- A1: results in zero struct (this can be seen as "ignore" behavior);
- A2: or, results in error
- when marshalling a struct which is zero-valued:
- B1: result in null (even when it's not a pointer; this is Somewhat Odd, but we can define it to be so)
- B2: result in tokens for an empty map (this is normal)
- B3: result in nothing at all (the omitempty behavior)
(Did I miss any?)
So we currently have A1 and B2 by default, and B3 can be configured.
We're missing the ability to configure A2 at all (and that seems wrong! It's possible A2 should be the default, really...); and we're missing B1. Yep.
And we could also try to get B1 by making marshal transform funcs support returning a sentinel value that means null, and that seems also like it would be reasonable to support but also seems like the less general case.
So then, thinking outloud, is ZeroAsNull(bool)
actually enough?
- default: A2, B2 (n.b. this would be a change to the default)
- if omitempty: A2, B3
- if zeroAsNull: A1, B1
- if omitempty&&zeroAsNull: A1, B3
With just these parameters, some of the other combos like B2+A1 would be unreachable. Is that desirable? Or do we need more parameters for these behaviors?
from refmt.
With just these parameters, some of the other combos like B2+A1 would be unreachable. Is that desirable? Or do we need more parameters for these behaviors?
Well, this is the current default. It's also what go does by default. (Not sure if it should be but users will be confused if it isn't)
One solution is to just define both ZeroAsNull
(defaults to false) and NullAsZero
(defaults to true).
There's actually another dimension here: tags.
Without tags (and with #37) fixed, I could just write a pair of transform functions to/from interface{}
. The issue with that is I also need to be able to say "tag this only if I transform to type X, not type Y".
Really, I'm starting to become more and more convinced that using either *cid.Cid
or omitempty
is what we want but users may not be so happy with that.
from refmt.
Another question came to mind: Do we want these behaviors to be specified at the site of fields (as omitempty is), or on types?
from refmt.
Well, for my usecase, types. In general, for fields, I'd just specify omitempty
.
from refmt.
Related Issues (20)
- Things fail with weird errors if your atlas doesnt know about all your types
- cbor output is not canonical HOT 7
- Is it possible to defer unmarshaling? HOT 1
- [cbor] Unable to roundtrip map[string]*CustomStruct HOT 2
- Panics and OOMs in cbor decoding HOT 8
- Option to encode nil slice/map as []/{}
- The handling of nil byte slices is different than the handling of other types of nil slices HOT 2
- blocking parallel execution? HOT 8
- Allow pointers when using UseTag HOT 3
- [cbor] Unable to use numbers as map keys HOT 5
- Using a transform function to/from an interface cause a nil pointer dereference
- Can't round trip nil slice HOT 1
- Fails to decode a valid CBOR object HOT 2
- Remove random binaries from git repo HOT 1
- Support for arbitrary structs without explicitly registering them HOT 2
- Possible to create my own raw marshal/unmarshal? HOT 6
- Support skipping unknown fields
- No go.mod, so modern idioms for building and testing fail?
- .gitmodules has extra trailing slashes that break "git submodule update --init"
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 refmt.