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

Optionally replace sql.Null* types with pointers #1810

Closed
xeoncross opened this issue Aug 22, 2022 · 2 comments
Closed

Optionally replace sql.Null* types with pointers #1810

xeoncross opened this issue Aug 22, 2022 · 2 comments
Labels
accepted enhancement New feature or request

Comments

@xeoncross
Copy link
Contributor

What do you want to change?

Structs generated by sqlc which contain null fields use the sql.Null* types. However, it would be nice if there was an option to use a pointer instead. So instead of sql.NullString we could use *string. The reason has to do with serialization since sql.NullString is an object you must manually define your serialization format for.

Consider the following object

type User struct {
	Name  sql.NullString
	Email string
}

which JSON encoded looks like

{
    "Name": {
        "String": "john",
        "Valid": true
    },
    "Email": "john@example.com"
}

And JSON decoded looks like: Unmarshal type error: expected=sql.NullString, got=string, field=Name and json: cannot unmarshal string into Go struct field User.Name of type sql.NullString. We must manually wrap sql.NullString to add proper marshaling support for each transport like JSON:

// Nullable String that wraps sql.NullString
type NullString struct {
	sql.NullString
}

func (ns NullString) MarshalJSON() ([]byte, error) {
	if ns.Valid {
		return json.Marshal(ns.String)
	}
	return json.Marshal(nil)
}

func (ns *NullString) UnmarshalJSON(data []byte) error {
	var s *string
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	if s != nil {
		ns.Valid = true
		ns.String = *s
	} else {
		ns.Valid = false
	}
	return nil
}

Of course, we could also manually update the configuration for every single null type in our database using overriding but that seems like a lot of work when we could just use pointers instead.

If I submit a PR to add an option like emit_struct_field_pointers would you potentially accept it?

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

@xeoncross xeoncross added enhancement New feature or request triage New issues that hasn't been reviewed labels Aug 22, 2022
@kyleconroy
Copy link
Collaborator

We have a pull request that adds this in #1571. That PR is a bit stale at this point, but could be cleaned up.

@kyleconroy kyleconroy added accepted and removed triage New issues that hasn't been reviewed labels Aug 28, 2022
@kyleconroy
Copy link
Collaborator

Fixed by #1571 and #1950

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants