-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
database/sql: add generic Null[T] #60370
Comments
Nullable[T]
see also #48702 |
Nullable[T]
Null[T]
Null[T]
This proposal has been added to the active column of the proposals project |
/cc @kardianos @bradfitz |
Looking at the existing NullInt64, etc they name the value field after the type (Int64 for NullInt64). We can't do that for the generic version - it needs a single field name for all the instances. Value is the obvious choice but that has to be a method. Val is the next obvious choice but then the fields are Val and Valid. Maybe X and Valid? |
Does someone want to write a CL and see how it looks? |
I suggest going with V and Valid, and don’t mind changing my CL to support this.
Could you please elaborate on why it needs to be a method? |
|
Generic version of NullString, NullInt64, etc. Fix golang#60370
I sent pull request with |
Change https://go.dev/cl/501700 mentions this issue: |
I feel like |
I think This abstraction in There are many proposals to add |
We have waited for generics. Do you saying that we should wait for sum type again? |
@methane Not exactly, the |
The database/sql Scan value type is not going to be a general option type that non-SQL users use. Make your own type elsewhere if that's what you want. But even then it won't be widely used by all the libraries so it'll always feel awkward and not idiomatic in Go. |
It should be Null[T] to match NullString etc. If those were named NullableString then we'd use Nullable[T]. This is only about sql. |
Have all concerns about this proposal been addressed? |
Based on the discussion above, this proposal seems like a likely accept. |
http://go.dev/cl/c/go/+/501700 missed go 1.21, but it should be able to get into 1.22. I made some comments on the CL. We still haven't picked a name for the value field. I know that |
I missed Russ's point about Val and Valid. Maybe |
http://go.dev/cl/c/go/+/501700 has been merged. This will be in 1.22. |
Change https://go.dev/cl/519555 mentions this issue: |
For #60370. Change-Id: Idae906ec7027be6d95f78bf43f7ce8f9d07e6c00 GitHub-Last-Rev: c645f0c GitHub-Pull-Request: #62033 Reviewed-on: https://go-review.googlesource.com/c/go/+/519555 TryBot-Bypass: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
Before it arrives: here's the implementation of go null with generics: https://github.com/lomsa-dev/gonull 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))
} |
I'm sorry that my comment comes late to this proposal, but I do agree with @leaxoy that |
@leaxoy @allochi We are discussing here about a type that has to implement interfaces |
One thing is not entirely clear for me, is there a point to use e.g. *NullString once you can use Null[T]? Wouldn't it be more clear for new comers if the old Null types were marked as obsolete? |
I don't think there is any use for NullString over Null[string]. |
Would it break anything for |
It would break |
是否会增加一个json string转换成结构体的代码实现呢,要不这个Null[T any]的意义是什么呢 |
database/sql
doesn't haveNullUInt64
. So drivers and ORMs may implement their ownNullUInt64
.Application developers would be confused which
NullUInt64
is returned by dynamic Scan methods.When
NullUInt64
was proposed in past, @a8m, @kardianos, @rsc said "wait for generic".#47953 (comment)
Now we have generics. So can we have it for now? Should it
Null[T]
orNullable[T]
?Here is quick implementation: https://go.dev/play/p/Jtx8hP0H1_Z
The text was updated successfully, but these errors were encountered: