Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conform UUID to Stringer interface #2055

Closed
quinn opened this issue Jun 23, 2024 · 6 comments
Closed

Conform UUID to Stringer interface #2055

quinn opened this issue Jun 23, 2024 · 6 comments

Comments

@quinn
Copy link

quinn commented Jun 23, 2024

Is your feature request related to a problem? Please describe.
I use sqlc, updated sql_package use pgx instead of previous version which used github.com/google/uuid. My code generally works as-is, except for some cases where I would pass an ID to something that expects a "stringer":

func (t T) String() string

For example, this makes it easy to tag logs with ID's:

logger = logger.With(zap.Stringer("thing", thing.ID))

Describe the solution you'd like
Add String method to UUID.

Describe alternatives you've considered

Additional context
here's the implementation from google's package:

func (uuid UUID) String() string {
	var buf [36]byte
	encodeHex(buf[:], uuid)
	return string(buf[:])
}

func encodeHex(dst []byte, uuid UUID) {
	hex.Encode(dst, uuid[:4])
	dst[8] = '-'
	hex.Encode(dst[9:13], uuid[4:6])
	dst[13] = '-'
	hex.Encode(dst[14:18], uuid[6:8])
	dst[18] = '-'
	hex.Encode(dst[19:23], uuid[8:10])
	dst[23] = '-'
	hex.Encode(dst[24:], uuid[10:])
}

I can make a PR if that would help expedite this.

@jackc
Copy link
Owner

jackc commented Jun 24, 2024

I guess we could add it, but pgtype.UUID isn't intended to be anything more that the minimum necessary to transfer UUIDs. I actually recommend using a 3rd party UUID type. Both the google one and the gofrs one work out of the box with pgx and there are extension packages for both that completely integrate them into pgx.

@quinn
Copy link
Author

quinn commented Jun 24, 2024

@jackc yep makes sense. thats what I ended up doing, the first example in the sqlc docs is for doing exactly this:

https://docs.sqlc.dev/en/stable/howto/overrides.html

@quinn quinn closed this as completed Jun 24, 2024
@andradei
Copy link

@jackc Is this documented in this package? There's a vague documentation on sqlc as linked by @quinn but it would be great to have this mentioned in the docs since pgtype.UUID does not have good API for users of the driver and google/uuid and others are much better on higher levels like web apps.

@jackc
Copy link
Owner

jackc commented Jul 10, 2024

@andradei The extension libraries are listed in the README. https://github.com/jackc/pgx/?tab=readme-ov-file#adapters-for-3rd-party-types. It's the same situation for decimal/numeric. pgx has a type whose sole purpose is to transfer values, but you really probably want to use shopspring/decimal.

@grachevko
Copy link
Contributor

grachevko commented Oct 9, 2024

#2145

It's not looking as a big deal especially we already have encode UUID function.
Looks worst to install 3rd party just for unexported simple function

@andradei
Copy link

andradei commented Oct 9, 2024

@grachevko Finally! That's very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants