Comments (7)
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.
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.
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.
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.
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.
Best of luck. If you have more questions, feel free to re-open or open a new issue.
from uuid.
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)
- Equals method HOT 1
- Reference implementaiton for UUID v8 HOT 2
- Is there a method to generate empty UUID v4? HOT 2
- UUID with repeat ending HOT 2
- NullUUID Scan method returns valid true on empty string HOT 2
- [Documentation] uuid.Nil is not documented properly
- `IsZero()` and `ToNullUUID` Helper Functions HOT 1
- panic: uuid: Parse(): invalid UUID length: 0 HOT 1
- Should uuid.Nil marshal to null instead of all zeroes HOT 3
- Make uuid.Nil and other values as a constant HOT 1
- Cannot get the package, connection refused
- uuid.Parse allows invalid UUID's HOT 2
- recommended to add uuid v6 and v7 support HOT 1
- Validate UUID without creating UUID (and underlying byte array)
- Limit permissions for github workflows
- uuid.Parse Function Does Not Handle Leading/Trailing Spaces in UUIDs
- Monotonicity in UUIDv7 HOT 2
- proposal: add func uuid.Compare(a, b UUID) int
- Incorrect uuidv6 implementation HOT 1
- not able to install uuid package HOT 5
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 uuid.