json encoding of result: [] instead of null if empty
This commit is contained in:
parent
b7a12b427f
commit
3b58d2031f
|
@ -50,17 +50,24 @@ type IndiewebDataResult struct {
|
|||
}
|
||||
|
||||
func ResultFailure(data []*IndiewebData) IndiewebDataResult {
|
||||
return IndiewebDataResult{
|
||||
return emptyNilData(IndiewebDataResult{
|
||||
Status: "failure",
|
||||
Data: data,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ResultSuccess(data []*IndiewebData) IndiewebDataResult {
|
||||
return IndiewebDataResult{
|
||||
return emptyNilData(IndiewebDataResult{
|
||||
Status: "success",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
func emptyNilData(result IndiewebDataResult) IndiewebDataResult {
|
||||
if result.Data == nil {
|
||||
result.Data = make([]*IndiewebData, 0)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type IndiewebData struct {
|
||||
|
|
|
@ -2,12 +2,55 @@ package mf
|
|||
|
||||
import (
|
||||
"brainbaking.com/go-jamming/common"
|
||||
"encoding/json"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
"willnorris.com/go/microformats"
|
||||
)
|
||||
|
||||
func TestResultSuccessNonEmpty(t *testing.T) {
|
||||
arr := make([]*IndiewebData, 1)
|
||||
arr[0] = &IndiewebData{Author: IndiewebAuthor{Name: "Jaak"}}
|
||||
|
||||
data := ResultSuccess(arr)
|
||||
jsonData, err := json.Marshal(data)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := `{"status":"success","json":[{"author":{"name":"Jaak","picture":""},"name":"","content":"","published":"","url":"","type":"","source":"","target":""}]}`
|
||||
assert.Equal(t, expected, string(jsonData))
|
||||
}
|
||||
|
||||
func TestResultSuccessEmptyEncodesAsEmptyJSONArray(t *testing.T) {
|
||||
data := ResultSuccess(nil)
|
||||
jsonData, err := json.Marshal(data)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := `{"status":"success","json":[]}`
|
||||
assert.Equal(t, expected, string(jsonData))
|
||||
}
|
||||
|
||||
func TestResultFailureNonEmpty(t *testing.T) {
|
||||
arr := make([]*IndiewebData, 1)
|
||||
arr[0] = &IndiewebData{Author: IndiewebAuthor{Name: "Jaak"}}
|
||||
|
||||
data := ResultFailure(arr)
|
||||
jsonData, err := json.Marshal(data)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := `{"status":"failure","json":[{"author":{"name":"Jaak","picture":""},"name":"","content":"","published":"","url":"","type":"","source":"","target":""}]}`
|
||||
assert.Equal(t, expected, string(jsonData))
|
||||
}
|
||||
|
||||
func TestResultFailureEmptyEncodesAsEmptyJSONArray(t *testing.T) {
|
||||
data := ResultFailure(nil)
|
||||
jsonData, err := json.Marshal(data)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := `{"status":"failure","json":[]}`
|
||||
assert.Equal(t, expected, string(jsonData))
|
||||
}
|
||||
|
||||
func TestPublished(t *testing.T) {
|
||||
cases := []struct {
|
||||
label string
|
||||
|
|
Loading…
Reference in New Issue