sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[BUGFIX] data json sql

This commit is contained in:
Martin Geno 2017-05-06 15:41:12 +02:00
parent e7b365df5e
commit cd2f10b54d
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 41 additions and 5 deletions

View File

@ -5,7 +5,9 @@ import (
"encoding/json" "encoding/json"
) )
type JsonNullInt64 sql.NullInt64 type JsonNullInt64 struct {
sql.NullInt64
}
func (v JsonNullInt64) MarshalJSON() ([]byte, error) { func (v JsonNullInt64) MarshalJSON() ([]byte, error) {
if v.Valid { if v.Valid {

30
lib/data/main_test.go Normal file
View File

@ -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)
}

View File

@ -30,7 +30,8 @@ func blogpostAdd(w http.ResponseWriter, r *http.Request) {
blogpost.BlogID = blog.ID blogpost.BlogID = blog.ID
if blogpost.Preview != nil { 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 blogpost.Preview = nil
} }
@ -71,7 +72,8 @@ func blogpostEdit(w http.ResponseWriter, r *http.Request) {
blogpost.ID = blogpostid blogpost.ID = blogpostid
if blogpost.Preview != nil { 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 blogpost.Preview = nil
} }

View File

@ -43,7 +43,8 @@ func menuAdd(w http.ResponseWriter, r *http.Request) {
menuEntry.WebsiteID = ctx.Value("websiteid").(int64) menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
if menuEntry.URL != nil { 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 menuEntry.URL = nil
} }
@ -77,7 +78,8 @@ func menuEdit(w http.ResponseWriter, r *http.Request) {
menuEntry.ID = menuid menuEntry.ID = menuid
if menuEntry.URL != nil { 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 menuEntry.URL = nil
} }