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

Generics support #67

Closed
guregu opened this issue Oct 4, 2021 · 7 comments · Fixed by #77
Closed

Generics support #67

guregu opened this issue Oct 4, 2021 · 7 comments · Fixed by #77

Comments

@guregu
Copy link
Owner

guregu commented Oct 4, 2021

Any ideas or opinions on how to handle generics in this library?
I played around with creating a generic Null[T] type, but I wonder if it's even useful at that point, because we are essentially recreating the Optional type you might find in other languages. At what point is it going too far?
Maybe ideally we could make generic Null/Zero types and use the materialized version of it for each pre-existing type.

I'm also curious if we'll see a generic sql.Null[T] type in the standard library, which might obsolete a good chunk of this library, especially if this change lands: golang/go#5901 (which lets you easily specify custom marshalers for JSON, moving the responsibility from the object itself to the encoding site).

See also: golang/go#48702

@lucklo
Copy link

lucklo commented Dec 26, 2021

I think it's matter of time for community to be able to share useful feedback. Until 1.18 become stable I would suspect we wont have good perspective.
I think it wouldnt be a bad idea to create a new feature branch with "generics proposal". It's like testing new hammer design :)

@jinoos
Copy link

jinoos commented Mar 15, 2022

Interesting! :)

@guregu
Copy link
Owner Author

guregu commented Aug 17, 2023

Looks like we will have an official generic sql.Null[T] in Go 1.22: golang/go#60370
Should (hopefully) be possible to wrap it in a nice way, although I'm not sure about the logistics around converting the data within.

@LukaGiorgadze
Copy link

Hi guys, here's the implementation of go null with generics: https://github.com/lomsa-dev/gonull
supports custom types.

usage:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/lomsa-dev/gonull"
)

type MyCustomInt int
type MyCustomFloat32 float32

type Person struct {
	Name    string
	Age     gonull.Nullable[MyCustomInt]
	Address gonull.Nullable[string]
	Height  gonull.Nullable[MyCustomFloat32]
}

func main() {
	jsonData := []byte(`{"Name":"Alice","Age":15,"Address":null,"Height":null}`)

	var person Person
	err := json.Unmarshal(jsonData, &person)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Unmarshalled Person: %+v\n", person)

	marshalledData, err := json.Marshal(person)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Marshalled JSON: %s\n", string(marshalledData))
}

@cspeidel
Copy link

cspeidel commented Feb 7, 2024

Looks like we will have an official generic sql.Null[T] in Go 1.22: golang/go#60370 Should (hopefully) be possible to wrap it in a nice way, although I'm not sure about the logistics around converting the data within.

sql.Null[T] has landed.
golang/go#60370
https://go.dev/doc/go1.22

@guregu
Copy link
Owner Author

guregu commented Feb 8, 2024

Nice, I will look into a new release that includes support for that and the other types we're missing from the stdlib like uint

@guregu
Copy link
Owner Author

guregu commented Feb 11, 2024

Released in v5 https://github.com/guregu/null/releases/tag/v5.0.0
This is a minimal wrapper around sql.Null[T]
In general it's impossible to support TextMarshaler for everything so it omits that
Feel free to make an issue if you have any ideas/suggestions

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

Successfully merging a pull request may close this issue.

5 participants