Git Product home page Git Product logo

Comments (5)

Stebalien avatar Stebalien commented on June 16, 2024

Alternatively, allow marshal transform func to return some marker error/value?

from refmt.

warpfork avatar warpfork commented on June 16, 2024

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.

Stebalien avatar Stebalien commented on June 16, 2024

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.

warpfork avatar warpfork commented on June 16, 2024

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.

Stebalien avatar Stebalien commented on June 16, 2024

Well, for my usecase, types. In general, for fields, I'd just specify omitempty.

from refmt.

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.