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

database/sql: NullString, NullInt64, NullFloat64, NullBool is not exported as real value #9719

Closed
mattn opened this issue Jan 29, 2015 · 8 comments

Comments

@mattn
Copy link
Member

@mattn mattn commented Jan 29, 2015

package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    "os"
)

type Foo struct {
    Id    int64          `json:"id"`
    Title sql.NullString `json:"title"`
}

func main() {
    db, _ := sql.Open("blah-driver", "blah-dsn")
    rows, _ := db.Query("select id, title from foo")
    for rows.Next() {
        var foo Foo
        rows.Scan(&foo.Id, &foo.Title)
        json.NewEncoder(os.Stdout).Encode(foo)
        fmt.Println()
    }
}

Expected:

{"id": 1, "title": "Go Book"}
{"id": 2, "title": null}
...
@mattn
Copy link
Member Author

@mattn mattn commented Jan 29, 2015

If getting agreements, I'll send https://gist.github.com/mattn/a42f0d5c0e135d4d03ee

@minux
Copy link
Member

@minux minux commented Jan 29, 2015

/cc @bradfitz

@johto
Copy link
Contributor

@johto johto commented Jan 29, 2015

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.

@mattn
Copy link
Member Author

@mattn mattn commented Jan 29, 2015

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"`
}
@mattn
Copy link
Member Author

@mattn mattn commented Jan 29, 2015

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"`
}
@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Jan 29, 2015

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.

@bradfitz bradfitz closed this Jan 29, 2015
@mattn
Copy link
Member Author

@mattn mattn commented Jan 29, 2015

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. :)

@mattn
Copy link
Member Author

@mattn mattn commented Jan 30, 2015

BTW, Why database/sql doesn't provide NullTime?

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.