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: NullString, NullInt64, NullFloat64, NullBool is not exported as real value #9719
Comments
If getting agreements, I'll send https://gist.github.com/mattn/a42f0d5c0e135d4d03ee |
/cc @bradfitz |
Judging from the gist you want the sql.Null* types to implement json.Marshaler? Is that it? FWIW that idea has been rejected in the past. |
Hmm, it seems redundancy to me. type MyNullString sql.NullString
func (ns NullString) MarshalJSON() ([]byte, error) {
if ns.Valid {
return json.Marshal(ns.String)
}
return jsonNull, nil
}
type Foo struct {
Id int64 `json:"id"`
Title MyNullString `json:"title"`
} |
I don't have strong objection but when user want to use Null* on their codes, it will be more redundancy . type MyNullString sql.NullString
type MyNullInt64 sql.NullInt64
func (ns NullString) MarshalJSON() ([]byte, error) {
if ns.Valid {
return json.Marshal(ns.String)
}
return jsonNull, nil
}
func (n NullInt64) MarshalJSON() ([]byte, error) {
if n.Valid {
return json.Marshal(n.Int64)
}
return jsonNull, nil
}
type Foo struct {
Id int64 `json:"id"`
Title MyNullString `json:"title"`
Count MyNullInt64 `json:"count"`
} If applies this patch, it will be simply like below. type Foo struct {
Id int64 `json:"id"`
Title NullString `json:"title"`
Count NullInt64 `json:"count"`
} |
Let's leave it alone. I think it would be confusing to have json references in the sql package, like Russ said before. This isn't preventing you from doing anything. You just want to move your complexity into the standard library so it becomes everybody's complexity. If everybody wanted this, sure. But I'm not convinced this need is universal. Those types are just examples anyway. |
Ah, just now, I got nice idea for new product. Probably, I can provide few types which can be put in user's struct as field. And that can be converted as JSON but can be used for sql.Scan also. :) |
BTW, Why database/sql doesn't provide NullTime? |
Expected:
The text was updated successfully, but these errors were encountered: