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

Marshaling data into sql.NullString #45

Closed
odewahn opened this issue Aug 13, 2016 · 4 comments
Closed

Marshaling data into sql.NullString #45

odewahn opened this issue Aug 13, 2016 · 4 comments

Comments

@odewahn
Copy link

odewahn commented Aug 13, 2016

Hi. Any suggestions on how to handle marshaling JSON data into structures created by xo?

For example, I have the following table:

create table projects (
  id varchar(40) primary key default gen_random_uuid(),
  project_name varchar(256),
  docker_image_name varchar(256),
  project_homepage text,
  project_tags text,
  project_description text,
  publisher_id varchar(40),
  featured varchar(5) default 'false'
);

xo generates this structure:

type Project struct {
    ID                 string         `json:"id"`                  // id
    ProjectName        sql.NullString `json:"project_name"`        // project_name
    DockerImageName    sql.NullString `json:"docker_image_name"`   // docker_image_name
    ProjectHomepage    sql.NullString `json:"project_homepage"`    // project_homepage
    ProjectTags        sql.NullString `json:"project_tags"`        // project_tags
    ProjectDescription sql.NullString `json:"project_description"` // project_description
    PublisherID        sql.NullString `json:"publisher_id"`        // publisher_id
    Featured           sql.NullString `json:"featured"`            // featured

    // xo fields
    _exists, _deleted bool
}

I'm trying to marshal this json data (from a POST request I'm making) into a structure:

{
  "ID": "1234",
  "ProjectName": "test projects",
  "ProjectAuthor": "odewahn",
  "GitOrigin": "www.example.com",
  "DockerhubPullCommand": "project",
  "ProjectDescription": "this is a great projects",
  "ProjectTags": "test data",
  "Featured": "false"
}

Using this command:

    var project models.Project
    err := json.Unmarshal(dat, &project)

The problem I'm having its that the Marshal doesn't seem to work with these structures, or throws an error like

json: cannot unmarshal string into Go value of type sql.NullBool

I would appreciate any ideas or suggestions!

@kenshaw
Copy link
Member

kenshaw commented Aug 13, 2016

This isn't really an issue with xo -- you would have this problem no matter the way you chose to generate (or manually wrote) types for your models. Unfortunately, the json.Marshal and json.Unmarhsal aren't directly compatible with the sql.Null<Type> types.

You could write the json.Marshaler and json.Unmarshaler interfaces for the model types that would work with the types generated by xo. If you do, you could add that to the xo template.

It should be noted that directly marshaling a JSON type into a database model is a bad idea for non-trivial applications.

@kenshaw kenshaw closed this as completed Aug 13, 2016
@odewahn
Copy link
Author

odewahn commented Aug 15, 2016

Thanks for your quick response and suggestions. I've wound up instead adding NOT NULL DEFAULT '' to the various varchar/text fields. So, xo now casts them as string, rather than sql.NullString, which works great for what I wanted.

@msaron
Copy link

msaron commented Jan 14, 2017

Is there any way to change sql.NullString to *string? I use pointers to deal with null values as it is the cleanest solution.

@kenshaw
Copy link
Member

kenshaw commented Jan 14, 2017

No, not at the moment. I am in the middle of planning for a large (almost total) rewrite of xo, and there has been a lot of requests in general for better ways to specify / override the types that xo generates. As such, in the future I envision there will be significantly better ways to provide these options to xo in the future. At the moment, I would just suggest building your own, modified version locally and using that instead. Changing specific types for a database is relatively easy/straightforward, simply modify the appropriate loader for your database (in the loaders directory).

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

No branches or pull requests

3 participants