forked from Mirrors/jsonapi
dry off, error if tag does not contain two arguments
This commit is contained in:
parent
8de6c952ef
commit
7430fd0b0c
|
@ -42,6 +42,11 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
|
|||
|
||||
args := strings.Split(tag, ",")
|
||||
|
||||
if len(args) != 2 {
|
||||
er = errors.New(fmt.Sprintf("jsonapi tag, on %s, had two few arguments", fieldType.Name))
|
||||
return false
|
||||
}
|
||||
|
||||
if len(args) >= 1 && args[0] != "" {
|
||||
annotation := args[0]
|
||||
|
||||
|
|
|
@ -4,9 +4,28 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type BadModel struct {
|
||||
Id int `jsonapi:"primary"`
|
||||
}
|
||||
|
||||
func TestMalformedTag(t *testing.T) {
|
||||
out := new(BadModel)
|
||||
err := UnmarshalJsonApiPayload(samplePayload(), out)
|
||||
if err == nil {
|
||||
t.Fatalf("Did not error out with wrong number of arguments in tag")
|
||||
}
|
||||
|
||||
r := regexp.MustCompile(`two few arguments`)
|
||||
|
||||
if !r.Match([]byte(err.Error())) {
|
||||
t.Fatalf("The err was not due two two few arguments in a tag")
|
||||
}
|
||||
}
|
||||
|
||||
//func TestUnmarshalSetsId(t *testing.T) {
|
||||
//in := samplePayload()
|
||||
//out := new(Blog)
|
||||
|
@ -21,10 +40,8 @@ import (
|
|||
//}
|
||||
|
||||
func TestUnmarshalSetsAttrs(t *testing.T) {
|
||||
in := samplePayload()
|
||||
out := new(Blog)
|
||||
|
||||
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
||||
out, err := unmarshalSamplePayload()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -43,10 +60,8 @@ func TestUnmarshalSetsAttrs(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUnmarshalRelationships(t *testing.T) {
|
||||
in := samplePayload()
|
||||
out := new(Blog)
|
||||
|
||||
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
||||
out, err := unmarshalSamplePayload()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -64,10 +79,8 @@ func TestUnmarshalRelationships(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUnmarshalNestedRelationships(t *testing.T) {
|
||||
in := samplePayload()
|
||||
out := new(Blog)
|
||||
|
||||
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
||||
out, err := unmarshalSamplePayload()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -84,6 +97,17 @@ func TestUnmarshalNestedRelationships(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func unmarshalSamplePayload() (*Blog, error) {
|
||||
in := samplePayload()
|
||||
out := new(Blog)
|
||||
|
||||
if err := UnmarshalJsonApiPayload(in, out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func samplePayload() io.Reader {
|
||||
payload := &JsonApiOnePayload{
|
||||
Data: &JsonApiNode{
|
||||
|
|
|
@ -84,6 +84,11 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
|
|||
|
||||
args := strings.Split(tag, ",")
|
||||
|
||||
if len(args) != 2 {
|
||||
er = errors.New(fmt.Sprintf("jsonapi tag, on %s, had two few arguments", structField.Name))
|
||||
return false
|
||||
}
|
||||
|
||||
if len(args) >= 1 && args[0] != "" {
|
||||
annotation := args[0]
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
@ -40,6 +41,21 @@ func (b Blogs) GetData() []interface{} {
|
|||
return d
|
||||
}
|
||||
|
||||
func TestMalformedTagResposne(t *testing.T) {
|
||||
testModel := &BadModel{}
|
||||
_, err := MarshalJsonApiOnePayload(testModel)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("Did not error out with wrong number of arguments in tag")
|
||||
}
|
||||
|
||||
r := regexp.MustCompile(`two few arguments`)
|
||||
|
||||
if !r.Match([]byte(err.Error())) {
|
||||
t.Fatalf("The err was not due two two few arguments in a tag")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasPrimaryAnnotation(t *testing.T) {
|
||||
testModel := &Blog{
|
||||
Id: 5,
|
||||
|
|
Loading…
Reference in New Issue