From cd2f10b54dbc5dea0be961bf997268023d04fba0 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Sat, 6 May 2017 15:41:12 +0200 Subject: [PATCH] [BUGFIX] data json sql --- lib/data/main.go | 4 +++- lib/data/main_test.go | 30 ++++++++++++++++++++++++++++++ modul/web/apiblogpost.go | 6 ++++-- modul/web/apimenu.go | 6 ++++-- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 lib/data/main_test.go diff --git a/lib/data/main.go b/lib/data/main.go index 768d2bf..18d0d22 100644 --- a/lib/data/main.go +++ b/lib/data/main.go @@ -5,7 +5,9 @@ import ( "encoding/json" ) -type JsonNullInt64 sql.NullInt64 +type JsonNullInt64 struct { + sql.NullInt64 +} func (v JsonNullInt64) MarshalJSON() ([]byte, error) { if v.Valid { diff --git a/lib/data/main_test.go b/lib/data/main_test.go new file mode 100644 index 0000000..89b83f8 --- /dev/null +++ b/lib/data/main_test.go @@ -0,0 +1,30 @@ +package data + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSQL(t *testing.T) { + assert := assert.New(t) + a := JsonNullInt64{} + a.Scan(300) + + assert.Equal(int64(300), a.Int64) + + a.UnmarshalJSON([]byte{'2', '3'}) + assert.Equal(int64(23), a.Int64) + + err := a.UnmarshalJSON([]byte{}) + assert.Error(err) + a.UnmarshalJSON([]byte{'n', 'u', 'l', 'l'}) + assert.False(a.Valid) + + value, _ := a.MarshalJSON() + assert.Equal([]byte{'n', 'u', 'l', 'l'}, value) + + a.UnmarshalJSON([]byte{'1', '4'}) + value, _ = a.MarshalJSON() + assert.Equal([]byte{'1', '4'}, value) +} diff --git a/modul/web/apiblogpost.go b/modul/web/apiblogpost.go index 47ab992..5eb0905 100644 --- a/modul/web/apiblogpost.go +++ b/modul/web/apiblogpost.go @@ -30,7 +30,8 @@ func blogpostAdd(w http.ResponseWriter, r *http.Request) { blogpost.BlogID = blog.ID if blogpost.Preview != nil { - blogpost.PreviewID = data.JsonNullInt64{Int64: blogpost.Preview.ID, Valid: true} + blogpost.PreviewID = data.JsonNullInt64{} + blogpost.PreviewID.Scan(blogpost.Preview.ID) blogpost.Preview = nil } @@ -71,7 +72,8 @@ func blogpostEdit(w http.ResponseWriter, r *http.Request) { blogpost.ID = blogpostid if blogpost.Preview != nil { - blogpost.PreviewID = data.JsonNullInt64{Int64: blogpost.Preview.ID, Valid: true} + blogpost.PreviewID = data.JsonNullInt64{} + blogpost.PreviewID.Scan(blogpost.Preview.ID) blogpost.Preview = nil } diff --git a/modul/web/apimenu.go b/modul/web/apimenu.go index de6ebed..809cc57 100644 --- a/modul/web/apimenu.go +++ b/modul/web/apimenu.go @@ -43,7 +43,8 @@ func menuAdd(w http.ResponseWriter, r *http.Request) { menuEntry.WebsiteID = ctx.Value("websiteid").(int64) if menuEntry.URL != nil { - menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true} + menuEntry.URLID = data.JsonNullInt64{} + menuEntry.URLID.Scan(menuEntry.URL.ID) menuEntry.URL = nil } @@ -77,7 +78,8 @@ func menuEdit(w http.ResponseWriter, r *http.Request) { menuEntry.ID = menuid if menuEntry.URL != nil { - menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true} + menuEntry.URLID = data.JsonNullInt64{} + menuEntry.URLID.Scan(menuEntry.URL.ID) menuEntry.URL = nil }