Git Product home page Git Product logo

Comments (7)

maxekman avatar maxekman commented on May 5, 2024

I would also be interested in any general suggestions on how this can be worked around. In our case we are trying to support JSON from an other UUID library which encodes nil as "".

from uuid.

pborman avatar pborman commented on May 5, 2024

Probably not ideal, but you could do something like https://play.golang.org/p/om22wkP5OAj

You could use github.com/pborman/uuid instead: https://play.golang.org/p/aZVmu5z0_31.

My concern here is that the uuid.Nil is actually a UUID while "" or JSON's nil implies there is no UUID at all. The UUID type has no way to express that, unless you use a pointer to UUID. This type of change may also break existing code as it would now treat "" and "00000000-0000-0000-0000-000000000000" as the same where before it did not. It is unfortunate that the standard JSON encoder/decoder does not enable the registration of customers encoding/decoding for existing types. (Apparently bson in mongodb does.)

The github.com/pborman/uuid does have the notion of a nonexistent UUID as a UUID is []byte instead of [16]byte. The google package originally was a fork of the pborman package to make uuid.UUID comparable and hence the ability to directly use as a key. The pborman package now is basically a wrapper around this package. (Both packages are by the same author.)

from uuid.

maxekman avatar maxekman commented on May 5, 2024

Thanks for the explanation!

The library I'm authoring (https://github.com/looplab/eventhorizon) just switched away from using our own UUID type in favour of this more widely adopted package, so I would rather not go back to a custom UUID type if possible.

Could it be an alternative (or even possible) to support parsing of "" as nil when the a UUID struct member is a pointer?

type S struct {
    ID *uuid.UUID
}

If that would be possible, client code will be forced to update all structs where UUIDs are optional using this convention.

from uuid.

pborman avatar pborman commented on May 5, 2024

Did you look at using github.com/pborman/uuid (as I mentioned, it is a wrapper around the google UUID package)? It appears your example works just fine when using the pborman version. https://play.golang.org/p/aZVmu5z0_31

I have no idea how prevalent this issue is in your code base. Is there a limited number of places this impacts you or is it external code that gets impacted?

from uuid.

maxekman avatar maxekman commented on May 5, 2024

Yes, I looked at github.com/pborman/uuid and it looks like it could work. However I would prefer to use github.com/google/uuid for now.

My main reason for supporting parsing "" is to be backward compatible with a relatively large MongoDB database in an external project. I could migrate all empty IDs to uuid.Nil and not be backward compatible in that regard, but due to the nature of the document store with some deeply nested documents it's very hard to guarantee that all IDs are properly migrated. I have to think some more on the trade offs here. Another option is to add support for empty UUID values in the existing custom BSON support: https://github.com/looplab/eventhorizon/blob/master/types/mongodb/uuid.go
Really cool that they have this support for encoding of external types, but quiet tricky to work with I have to say!

Thanks for the advice so far!

Also, feel free to close the issue if you wish.

from uuid.

pborman avatar pborman commented on May 5, 2024

Best of luck. If you have more questions, feel free to re-open or open a new issue.

from uuid.

skagget77 avatar skagget77 commented on May 5, 2024

I have a similar problem, I like this UUID library (due to UUIDs being comparable) but need to work with a misbehaving JSON based API. As a workaround I created a small wrapper to allow for unmarshaling of empty and malformed UUIDs:

https://github.com/skagget77/uuidnil

Maybe this can help you out as well.

from uuid.

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.