forked from Mirrors/jsonapi
fix relationships--add data--to comply w spec
This commit is contained in:
parent
99d5ee6428
commit
0525400acb
10
node.go
10
node.go
|
@ -12,3 +12,13 @@ type JsonApiNode struct {
|
||||||
Attributes map[string]interface{} `json:"attributes,omitempty"`
|
Attributes map[string]interface{} `json:"attributes,omitempty"`
|
||||||
Relationships map[string]interface{} `json:"realtionships,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"`
|
||||||
|
}
|
||||||
|
|
26
response.go
26
response.go
|
@ -98,20 +98,20 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
|
||||||
|
|
||||||
if isSlice {
|
if isSlice {
|
||||||
relationship, err := visitModelNodeRelationships(args[1], fieldValue)
|
relationship, err := visitModelNodeRelationships(args[1], fieldValue)
|
||||||
|
d := relationship[args[1]].Data
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
shallowNodes := make([]*JsonApiNode, 0)
|
shallowNodes := make([]*JsonApiNode, 0)
|
||||||
for k, v := range relationship {
|
for _, node := range d {
|
||||||
for _, node := range v {
|
included = append(included, node)
|
||||||
included = append(included, node)
|
|
||||||
|
|
||||||
shallowNode := *node
|
shallowNode := *node
|
||||||
shallowNode.Attributes = nil
|
shallowNode.Attributes = nil
|
||||||
shallowNodes = append(shallowNodes, &shallowNode)
|
shallowNodes = append(shallowNodes, &shallowNode)
|
||||||
}
|
|
||||||
|
|
||||||
node.Relationships[k] = shallowNodes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
node.Relationships[args[1]] = &JsonApiRelationshipMultipleNode{Data: shallowNodes}
|
||||||
} else {
|
} else {
|
||||||
er = err
|
er = err
|
||||||
return false
|
return false
|
||||||
|
@ -124,7 +124,7 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
|
||||||
|
|
||||||
included = append(included, relationship)
|
included = append(included, relationship)
|
||||||
|
|
||||||
node.Relationships[args[1]] = &shallowNode
|
node.Relationships[args[1]] = &JsonApiRelationshipSingleNode{Data: &shallowNode}
|
||||||
} else {
|
} else {
|
||||||
er = err
|
er = err
|
||||||
return false
|
return false
|
||||||
|
@ -147,8 +147,8 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) {
|
||||||
return node, included, nil
|
return node, included, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func visitModelNodeRelationships(relationName string, models reflect.Value) (map[string][]*JsonApiNode, error) {
|
func visitModelNodeRelationships(relationName string, models reflect.Value) (map[string]*JsonApiRelationshipMultipleNode, error) {
|
||||||
relationship := make(map[string][]*JsonApiNode)
|
m := make(map[string]*JsonApiRelationshipMultipleNode)
|
||||||
nodes := make([]*JsonApiNode, 0)
|
nodes := make([]*JsonApiNode, 0)
|
||||||
|
|
||||||
for i := 0; i < models.Len(); i++ {
|
for i := 0; i < models.Len(); i++ {
|
||||||
|
@ -160,7 +160,7 @@ func visitModelNodeRelationships(relationName string, models reflect.Value) (map
|
||||||
nodes = append(nodes, node)
|
nodes = append(nodes, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
relationship[relationName] = nodes
|
m[relationName] = &JsonApiRelationshipMultipleNode{Data: nodes}
|
||||||
|
|
||||||
return relationship, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ func TestRelations(t *testing.T) {
|
||||||
t.Fatalf("Current post relationship was not materialized")
|
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")
|
t.Fatalf("Did not materialize two posts")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue