remove 'JsonApi' everywhere, since we're already in a pkg

This commit is contained in:
Sam Woodard 2015-07-10 09:07:12 -07:00
parent 5fc25e655a
commit 0becfab81d
5 changed files with 74 additions and 74 deletions

22
node.go
View File

@ -1,14 +1,14 @@
package jsonapi package jsonapi
type JsonApiOnePayload struct { type OnePayload struct {
Data *JsonApiNode `json:"data"` Data *Node `json:"data"`
Included []*JsonApiNode `json:"included,omitempty"` Included []*Node `json:"included,omitempty"`
Links *map[string]string `json:"links,omitempty"` Links *map[string]string `json:"links,omitempty"`
} }
type JsonApiManyPayload struct { type ManyPayload struct {
Data []*JsonApiNode `json:"data"` Data []*Node `json:"data"`
Included []*JsonApiNode `json:"included,omitempty"` Included []*Node `json:"included,omitempty"`
Links *map[string]string `json:"links,omitempty"` Links *map[string]string `json:"links,omitempty"`
} }
@ -16,19 +16,19 @@ type Models interface {
GetData() []interface{} GetData() []interface{}
} }
type JsonApiNode struct { type Node struct {
Type string `json:"type"` Type string `json:"type"`
Id string `json:"id"` Id string `json:"id"`
Attributes map[string]interface{} `json:"attributes,omitempty"` Attributes map[string]interface{} `json:"attributes,omitempty"`
Relationships map[string]interface{} `json:"relationships,omitempty"` Relationships map[string]interface{} `json:"relationships,omitempty"`
} }
type JsonApiRelationshipOneNode struct { type RelationshipOneNode struct {
Data *JsonApiNode `json:"data"` Data *Node `json:"data"`
Links *map[string]string `json:"links,omitempty"` Links *map[string]string `json:"links,omitempty"`
} }
type JsonApiRelationshipManyNode struct { type RelationshipManyNode struct {
Data []*JsonApiNode `json:"data"` Data []*Node `json:"data"`
Links *map[string]string `json:"links,omitempty"` Links *map[string]string `json:"links,omitempty"`
} }

View File

@ -11,17 +11,17 @@ import (
"time" "time"
) )
func UnmarshalJsonApiPayload(in io.Reader, model interface{}) error { func UnmarshalPayload(in io.Reader, model interface{}) error {
payload := new(JsonApiOnePayload) payload := new(OnePayload)
if err := json.NewDecoder(in).Decode(payload); err != nil { if err := json.NewDecoder(in).Decode(payload); err != nil {
return err return err
} }
return unmarshalJsonApiNode(payload.Data, reflect.ValueOf(model)) return unmarshalNode(payload.Data, reflect.ValueOf(model))
} }
func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error { func unmarshalNode(data *Node, model reflect.Value) error {
modelValue := model.Elem() modelValue := model.Elem()
modelType := model.Type().Elem() modelType := model.Type().Elem()
@ -130,7 +130,7 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
m := reflect.New(fieldValue.Type().Elem().Elem()) m := reflect.New(fieldValue.Type().Elem().Elem())
h := r.(map[string]interface{}) h := r.(map[string]interface{})
if err := unmarshalJsonApiNode(mapToJsonApiNode(h), m); err != nil { if err := unmarshalNode(mapToNode(h), m); err != nil {
er = err er = err
return false return false
} }
@ -143,7 +143,7 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
m := reflect.New(fieldValue.Type().Elem()) m := reflect.New(fieldValue.Type().Elem())
h := relationship["data"].(map[string]interface{}) h := relationship["data"].(map[string]interface{})
if err := unmarshalJsonApiNode(mapToJsonApiNode(h), m); err != nil { if err := unmarshalNode(mapToNode(h), m); err != nil {
er = err er = err
return false return false
} }
@ -166,8 +166,8 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
return nil return nil
} }
func mapToJsonApiNode(m map[string]interface{}) *JsonApiNode { func mapToNode(m map[string]interface{}) *Node {
node := &JsonApiNode{Type: m["type"].(string)} node := &Node{Type: m["type"].(string)}
if m["id"] != nil { if m["id"] != nil {
node.Id = m["id"].(string) node.Id = m["id"].(string)

View File

@ -14,7 +14,7 @@ type BadModel struct {
func TestMalformedTag(t *testing.T) { func TestMalformedTag(t *testing.T) {
out := new(BadModel) out := new(BadModel)
err := UnmarshalJsonApiPayload(samplePayload(), out) err := UnmarshalPayload(samplePayload(), out)
if err == nil { if err == nil {
t.Fatalf("Did not error out with wrong number of arguments in tag") t.Fatalf("Did not error out with wrong number of arguments in tag")
} }
@ -30,7 +30,7 @@ func TestUnmarshalSetsId(t *testing.T) {
in := samplePayloadWithId() in := samplePayloadWithId()
out := new(Blog) out := new(Blog)
if err := UnmarshalJsonApiPayload(in, out); err != nil { if err := UnmarshalPayload(in, out); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -101,7 +101,7 @@ func unmarshalSamplePayload() (*Blog, error) {
in := samplePayload() in := samplePayload()
out := new(Blog) out := new(Blog)
if err := UnmarshalJsonApiPayload(in, out); err != nil { if err := UnmarshalPayload(in, out); err != nil {
return nil, err return nil, err
} }
@ -109,8 +109,8 @@ func unmarshalSamplePayload() (*Blog, error) {
} }
func samplePayload() io.Reader { func samplePayload() io.Reader {
payload := &JsonApiOnePayload{ payload := &OnePayload{
Data: &JsonApiNode{ Data: &Node{
Type: "blogs", Type: "blogs",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{
"title": "New blog", "title": "New blog",
@ -118,16 +118,16 @@ func samplePayload() io.Reader {
"view_count": 1000, "view_count": 1000,
}, },
Relationships: map[string]interface{}{ Relationships: map[string]interface{}{
"posts": &JsonApiRelationshipManyNode{ "posts": &RelationshipManyNode{
Data: []*JsonApiNode{ Data: []*Node{
&JsonApiNode{ &Node{
Type: "posts", Type: "posts",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{
"title": "Foo", "title": "Foo",
"body": "Bar", "body": "Bar",
}, },
}, },
&JsonApiNode{ &Node{
Type: "posts", Type: "posts",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{
"title": "X", "title": "X",
@ -136,23 +136,23 @@ func samplePayload() io.Reader {
}, },
}, },
}, },
"current_post": &JsonApiRelationshipOneNode{ "current_post": &RelationshipOneNode{
Data: &JsonApiNode{ Data: &Node{
Type: "posts", Type: "posts",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{
"title": "Bas", "title": "Bas",
"body": "Fuubar", "body": "Fuubar",
}, },
Relationships: map[string]interface{}{ Relationships: map[string]interface{}{
"comments": &JsonApiRelationshipManyNode{ "comments": &RelationshipManyNode{
Data: []*JsonApiNode{ Data: []*Node{
&JsonApiNode{ &Node{
Type: "comments", Type: "comments",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{
"body": "Great post!", "body": "Great post!",
}, },
}, },
&JsonApiNode{ &Node{
Type: "comments", Type: "comments",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{
"body": "Needs some work!", "body": "Needs some work!",
@ -175,8 +175,8 @@ func samplePayload() io.Reader {
} }
func samplePayloadWithId() io.Reader { func samplePayloadWithId() io.Reader {
payload := &JsonApiOnePayload{ payload := &OnePayload{
Data: &JsonApiNode{ Data: &Node{
Id: "2", Id: "2",
Type: "blogs", Type: "blogs",
Attributes: map[string]interface{}{ Attributes: map[string]interface{}{

View File

@ -10,11 +10,11 @@ import (
"time" "time"
) )
func MarshalJsonApiManyPayload(w io.Writer, models Models) error { func MarshalManyPayload(w io.Writer, models Models) error {
d := models.GetData() d := models.GetData()
data := make([]*JsonApiNode, 0, len(d)) data := make([]*Node, 0, len(d))
incl := make([]*JsonApiNode, 0) incl := make([]*Node, 0)
for _, model := range d { for _, model := range d {
node, included, err := visitModelNode(model, true) node, included, err := visitModelNode(model, true)
@ -25,7 +25,7 @@ func MarshalJsonApiManyPayload(w io.Writer, models Models) error {
incl = append(incl, included...) incl = append(incl, included...)
} }
uniqueIncluded := make(map[string]*JsonApiNode) uniqueIncluded := make(map[string]*Node)
for i, n := range incl { for i, n := range incl {
k := fmt.Sprintf("%s,%s", n.Type, n.Id) k := fmt.Sprintf("%s,%s", n.Type, n.Id)
@ -36,7 +36,7 @@ func MarshalJsonApiManyPayload(w io.Writer, models Models) error {
} }
} }
payload := &JsonApiManyPayload{ payload := &ManyPayload{
Data: data, Data: data,
Included: incl, Included: incl,
} }
@ -48,27 +48,27 @@ func MarshalJsonApiManyPayload(w io.Writer, models Models) error {
return nil return nil
} }
func MarshalJsonApiOnePayloadEmbedded(model interface{}) (*JsonApiOnePayload, error) { func MarshalOnePayloadEmbedded(model interface{}) (*OnePayload, error) {
rootNode, _, err := visitModelNode(model, false) rootNode, _, err := visitModelNode(model, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp := &JsonApiOnePayload{Data: rootNode} resp := &OnePayload{Data: rootNode}
return resp, nil return resp, nil
} }
func MarshalJsonApiOnePayload(w io.Writer, model interface{}) error { func MarshalOnePayload(w io.Writer, model interface{}) error {
rootNode, included, err := visitModelNode(model, true) rootNode, included, err := visitModelNode(model, true)
if err != nil { if err != nil {
return err return err
} }
payload := &JsonApiOnePayload{Data: rootNode} payload := &OnePayload{Data: rootNode}
uniqueIncluded := make(map[string]*JsonApiNode) uniqueIncluded := make(map[string]*Node)
for i, n := range included { for i, n := range included {
k := fmt.Sprintf("%s,%s", n.Type, n.Id) k := fmt.Sprintf("%s,%s", n.Type, n.Id)
@ -88,11 +88,11 @@ func MarshalJsonApiOnePayload(w io.Writer, model interface{}) error {
return nil return nil
} }
func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiNode, error) { func visitModelNode(model interface{}, sideload bool) (*Node, []*Node, error) {
node := new(JsonApiNode) node := new(Node)
var er error var er error
var included []*JsonApiNode var included []*Node
modelType := reflect.TypeOf(model).Elem() modelType := reflect.TypeOf(model).Elem()
modelValue := reflect.ValueOf(model).Elem() modelValue := reflect.ValueOf(model).Elem()
@ -154,7 +154,7 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
} }
if included == nil { if included == nil {
included = make([]*JsonApiNode, 0) included = make([]*Node, 0)
} }
if isSlice { if isSlice {
@ -163,15 +163,15 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
if err == nil { if err == nil {
if sideload { if sideload {
shallowNodes := make([]*JsonApiNode, 0) shallowNodes := make([]*Node, 0)
for _, node := range d { for _, node := range d {
included = append(included, node) included = append(included, node)
shallowNodes = append(shallowNodes, cloneAndRemoveAttributes(node)) shallowNodes = append(shallowNodes, cloneAndRemoveAttributes(node))
} }
node.Relationships[args[1]] = &JsonApiRelationshipManyNode{Data: shallowNodes} node.Relationships[args[1]] = &RelationshipManyNode{Data: shallowNodes}
} else { } else {
node.Relationships[args[1]] = &JsonApiRelationshipManyNode{Data: d} node.Relationships[args[1]] = &RelationshipManyNode{Data: d}
} }
} else { } else {
er = err er = err
@ -182,9 +182,9 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
if err == nil { if err == nil {
if sideload { if sideload {
included = append(included, relationship) included = append(included, relationship)
node.Relationships[args[1]] = &JsonApiRelationshipOneNode{Data: cloneAndRemoveAttributes(relationship)} node.Relationships[args[1]] = &RelationshipOneNode{Data: cloneAndRemoveAttributes(relationship)}
} else { } else {
node.Relationships[args[1]] = &JsonApiRelationshipOneNode{Data: relationship} node.Relationships[args[1]] = &RelationshipOneNode{Data: relationship}
} }
} else { } else {
er = err er = err
@ -208,16 +208,16 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
return node, included, nil return node, included, nil
} }
func cloneAndRemoveAttributes(node *JsonApiNode) *JsonApiNode { func cloneAndRemoveAttributes(node *Node) *Node {
n := *node n := *node
n.Attributes = nil n.Attributes = nil
return &n return &n
} }
func visitModelNodeRelationships(relationName string, models reflect.Value, sideload bool) (map[string]*JsonApiRelationshipManyNode, error) { func visitModelNodeRelationships(relationName string, models reflect.Value, sideload bool) (map[string]*RelationshipManyNode, error) {
m := make(map[string]*JsonApiRelationshipManyNode) m := make(map[string]*RelationshipManyNode)
nodes := make([]*JsonApiNode, 0) nodes := make([]*Node, 0)
for i := 0; i < models.Len(); i++ { for i := 0; i < models.Len(); i++ {
node, _, err := visitModelNode(models.Index(i).Interface(), sideload) node, _, err := visitModelNode(models.Index(i).Interface(), sideload)
@ -228,12 +228,12 @@ func visitModelNodeRelationships(relationName string, models reflect.Value, side
nodes = append(nodes, node) nodes = append(nodes, node)
} }
m[relationName] = &JsonApiRelationshipManyNode{Data: nodes} m[relationName] = &RelationshipManyNode{Data: nodes}
return m, nil return m, nil
} }
func deleteNode(a []*JsonApiNode, i int) []*JsonApiNode { func deleteNode(a []*Node, i int) []*Node {
if i < len(a)-1 { if i < len(a)-1 {
a = append(a[:i], a[i+1:]...) a = append(a[:i], a[i+1:]...)
} else { } else {

View File

@ -42,7 +42,7 @@ func (b Blogs) GetData() []interface{} {
func TestMalformedTagResposne(t *testing.T) { func TestMalformedTagResposne(t *testing.T) {
testModel := &BadModel{} testModel := &BadModel{}
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
err := MarshalJsonApiOnePayload(out, testModel) err := MarshalOnePayload(out, testModel)
if err == nil { if err == nil {
t.Fatalf("Did not error out with wrong number of arguments in tag") t.Fatalf("Did not error out with wrong number of arguments in tag")
@ -63,11 +63,11 @@ func TestHasPrimaryAnnotation(t *testing.T) {
} }
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil { if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err) t.Fatal(err)
} }
resp := new(JsonApiOnePayload) resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil { if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err) t.Fatal(err)
@ -92,11 +92,11 @@ func TestSupportsAttributes(t *testing.T) {
} }
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil { if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err) t.Fatal(err)
} }
resp := new(JsonApiOnePayload) resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil { if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -120,11 +120,11 @@ func TestOmitsZeroTimes(t *testing.T) {
} }
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil { if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err) t.Fatal(err)
} }
resp := new(JsonApiOnePayload) resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil { if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -165,11 +165,11 @@ func TestRelations(t *testing.T) {
} }
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil { if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err) t.Fatal(err)
} }
resp := new(JsonApiOnePayload) resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil { if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -197,11 +197,11 @@ func TestNoRelations(t *testing.T) {
testModel := &Blog{Id: 1, Title: "Title 1", CreatedAt: time.Now()} testModel := &Blog{Id: 1, Title: "Title 1", CreatedAt: time.Now()}
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil { if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err) t.Fatal(err)
} }
resp := new(JsonApiOnePayload) resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil { if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -260,11 +260,11 @@ func TestMarshalMany(t *testing.T) {
} }
out := bytes.NewBuffer(nil) out := bytes.NewBuffer(nil)
if err := MarshalJsonApiManyPayload(out, data); err != nil { if err := MarshalManyPayload(out, data); err != nil {
t.Fatal(err) t.Fatal(err)
} }
resp := new(JsonApiManyPayload) resp := new(ManyPayload)
if err := json.NewDecoder(out).Decode(resp); err != nil { if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err) t.Fatal(err)
} }