fix relationships--add data--to comply w spec

This commit is contained in:
Sam Woodard 2015-07-06 17:04:26 -07:00
parent 99d5ee6428
commit 0525400acb
3 changed files with 24 additions and 14 deletions

10
node.go
View File

@ -12,3 +12,13 @@ type JsonApiNode struct {
Attributes map[string]interface{} `json:"attributes,omitempty"`
Relationships map[string]interface{} `json:"realtionships,omitempty"`
}
type JsonApiRelationshipSingleNode struct {
Data *JsonApiNode `json:"data"`
Links *map[string]string `json:"links,omitempty"`
}
type JsonApiRelationshipMultipleNode struct {
Data []*JsonApiNode `json:"data"`
Links *map[string]string `json:"links,omitempty"`
}

View File

@ -98,20 +98,20 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
if isSlice {
relationship, err := visitModelNodeRelationships(args[1], fieldValue)
d := relationship[args[1]].Data
if err == nil {
shallowNodes := make([]*JsonApiNode, 0)
for k, v := range relationship {
for _, node := range v {
included = append(included, node)
for _, node := range d {
included = append(included, node)
shallowNode := *node
shallowNode.Attributes = nil
shallowNodes = append(shallowNodes, &shallowNode)
}
shallowNode := *node
shallowNode.Attributes = nil
shallowNodes = append(shallowNodes, &shallowNode)
node.Relationships[k] = shallowNodes
}
node.Relationships[args[1]] = &JsonApiRelationshipMultipleNode{Data: shallowNodes}
} else {
er = err
return false
@ -124,7 +124,7 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
included = append(included, relationship)
node.Relationships[args[1]] = &shallowNode
node.Relationships[args[1]] = &JsonApiRelationshipSingleNode{Data: &shallowNode}
} else {
er = err
return false
@ -147,8 +147,8 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
return node, included, nil
}
func visitModelNodeRelationships(relationName string, models reflect.Value) (map[string][]*JsonApiNode, error) {
relationship := make(map[string][]*JsonApiNode)
func visitModelNodeRelationships(relationName string, models reflect.Value) (map[string]*JsonApiRelationshipMultipleNode, error) {
m := make(map[string]*JsonApiRelationshipMultipleNode)
nodes := make([]*JsonApiNode, 0)
for i := 0; i < models.Len(); i++ {
@ -160,7 +160,7 @@ func visitModelNodeRelationships(relationName string, models reflect.Value) (map
nodes = append(nodes, node)
}
relationship[relationName] = nodes
m[relationName] = &JsonApiRelationshipMultipleNode{Data: nodes}
return relationship, nil
return m, nil
}

View File

@ -118,7 +118,7 @@ func TestRelations(t *testing.T) {
t.Fatalf("Current post relationship was not materialized")
}
if reflect.ValueOf(relations["posts"]).Len() != 2 {
if reflect.ValueOf(relations["posts"]).Elem().FieldByName("Data").Len() != 2 {
t.Fatalf("Did not materialize two posts")
}
}