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

[BEST PRACTICE] To use with unique identifier #1143

Open
brutalzinn opened this issue Feb 14, 2024 · 1 comment
Open

[BEST PRACTICE] To use with unique identifier #1143

brutalzinn opened this issue Feb 14, 2024 · 1 comment
Labels
question This issue is a question about the project

Comments

@brutalzinn
Copy link

brutalzinn commented Feb 14, 2024

Hello, guys. Thanks for this project be public.
I have a question about best case to uses with mssql.uniqueidentifier.
This is a small example of my struct and crud operations. I use bun to connect to database


type SomeStruct struct {
	ID         int                    `bun:"id"`
	ExternalId mssql.UniqueIdentifier `bun:"externalid"`
	IdPost     int                    `bun:"id_post"`
	IdAvatar   int                    `bun:"id_avatar"`
}

func (someStruct *SomeStruct) MarshalJSON() ([]byte, error) {
	type Alias SomeStruct
	return json.Marshal(&struct {
		ID string `json:"id"`
		*Alias
	}{
		ID:    someStruct.ExternalId.String(),
		Alias: (*Alias)(someStruct),
	})
}

I need use externalId as string everywhere at my project. And i need to cast mssql uniqueidentifier to string like i do at MarshalJson func. Is there a best practice to handle this without uses conversion everytime? I am thinking about change the unique identifier column to string. But i will lost much funcionalities of unique identifier be managed by sql server.

@dagood
Copy link
Member

dagood commented Feb 14, 2024

Hi @brutalzinn, happy to share our work. 🙂

For info about an established best practice with the go-mssqldb module, it might be better to ask at https://github.com/microsoft/go-mssqldb, or maybe even https://groups.google.com/g/golang-nuts or another broad resource listed at https://go.dev/help. The microsoft/go repository in particular is a fork of the Go compiler itself, so we might not know if there's a best practice already agreed upon by the community.

That said, I noticed that https://pkg.go.dev/github.com/microsoft/[email protected]#UniqueIdentifier implements json.Marshaler and json.Unmarshaler, so the module probably expects you to use those rather than converting it to a string yourself: https://go.dev/play/p/49AXUtn0BZb

@dagood dagood added the question This issue is a question about the project label Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question This issue is a question about the project
Projects
None yet
Development

No branches or pull requests

2 participants