fraenky8 / tables-to-go

convert your database tables to structs easily
MIT License
233 stars 42 forks source link

Required versus optional value mappings #13

Closed kaihendry closed 5 years ago

kaihendry commented 5 years ago

If a schema is say just age INT I think you set the type sql.NullInt64. But this generates gnarly JSON mappings like

    "Age": {
        "Int64": 0,
        "Valid": true
    }

My workaround is to then edit the generated type to for the optional value:

Age  int     `db:"age" json:"age,omitempty"`

However I've noticed that go sets 0 (ZERO) in my JSON to DB mapping with this "workaround".

INSERT INTO `users` (`id`, `name`, `age`) VALUES
(1, 'Arnie',    0);

If it was the original sql.NullInt64 the value in DB would be:

(2, 'Arnie 2',  NULL);

Which is more correct in terms of the DBMS, but then how does one handle the JSON ugliness?!

I also have a type issue when it comes to required values (name): https://s.natalian.org/2019-03-31/required.mp4

kaihendry commented 5 years ago

Ok, I'm also asking SO https://stackoverflow.com/questions/55437279/sql-nullint64-to-json

kaihendry commented 5 years ago

I think I will just use *types / pointers for the optional values to get my JSON as I want it. https://stackoverflow.com/a/55437560/4534