2015-07-05 11:59:30 -04:00
|
|
|
package jsonapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2015-07-06 17:35:17 -04:00
|
|
|
"time"
|
2015-07-05 11:59:30 -04:00
|
|
|
)
|
|
|
|
|
2015-07-07 12:52:38 -04:00
|
|
|
func MarshalJsonApiManyPayload(models Models) (*JsonApiManyPayload, error) {
|
|
|
|
d := models.GetData()
|
|
|
|
data := make([]*JsonApiNode, 0, len(d))
|
|
|
|
|
|
|
|
incl := make([]*JsonApiNode, 0)
|
|
|
|
|
|
|
|
for _, model := range d {
|
2015-07-08 16:28:38 -04:00
|
|
|
node, included, err := visitModelNode(model, true)
|
2015-07-07 12:52:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data = append(data, node)
|
|
|
|
incl = append(incl, included...)
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueIncluded := make(map[string]*JsonApiNode)
|
|
|
|
|
|
|
|
for i, n := range incl {
|
|
|
|
k := fmt.Sprintf("%s,%s", n.Type, n.Id)
|
|
|
|
if uniqueIncluded[k] == nil {
|
|
|
|
uniqueIncluded[k] = n
|
|
|
|
} else {
|
|
|
|
incl = deleteNode(incl, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &JsonApiManyPayload{
|
|
|
|
Data: data,
|
|
|
|
Included: incl,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
func MarshalJsonApiOnePayloadEmbedded(model interface{}) (*JsonApiOnePayload, error) {
|
|
|
|
rootNode, _, err := visitModelNode(model, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &JsonApiOnePayload{Data: rootNode}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-07 12:52:38 -04:00
|
|
|
func MarshalJsonApiOnePayload(model interface{}) (*JsonApiOnePayload, error) {
|
2015-07-08 16:28:38 -04:00
|
|
|
rootNode, included, err := visitModelNode(model, true)
|
2015-07-05 13:59:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-07 12:52:38 -04:00
|
|
|
resp := &JsonApiOnePayload{Data: rootNode}
|
2015-07-05 15:12:09 -04:00
|
|
|
|
|
|
|
uniqueIncluded := make(map[string]*JsonApiNode)
|
|
|
|
|
|
|
|
for i, n := range included {
|
|
|
|
k := fmt.Sprintf("%s,%s", n.Type, n.Id)
|
|
|
|
if uniqueIncluded[k] == nil {
|
|
|
|
uniqueIncluded[k] = n
|
|
|
|
} else {
|
2015-07-07 12:52:38 -04:00
|
|
|
included = deleteNode(included, i)
|
2015-07-05 15:12:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 13:59:35 -04:00
|
|
|
resp.Included = included
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2015-07-05 11:59:30 -04:00
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiNode, error) {
|
2015-07-05 13:59:35 -04:00
|
|
|
node := new(JsonApiNode)
|
2015-07-05 11:59:30 -04:00
|
|
|
|
2015-07-06 17:39:24 -04:00
|
|
|
var er error
|
2015-07-06 12:24:35 -04:00
|
|
|
var included []*JsonApiNode
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
modelType := reflect.TypeOf(model).Elem()
|
|
|
|
modelValue := reflect.ValueOf(model).Elem()
|
2015-07-05 11:59:30 -04:00
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
var i = 0
|
2015-07-05 13:59:35 -04:00
|
|
|
modelType.FieldByNameFunc(func(name string) bool {
|
2015-07-06 13:38:42 -04:00
|
|
|
fieldValue := modelValue.Field(i)
|
2015-07-06 17:35:17 -04:00
|
|
|
structField := modelType.Field(i)
|
2015-07-05 11:59:30 -04:00
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
i += 1
|
|
|
|
|
2015-07-06 17:35:17 -04:00
|
|
|
tag := structField.Tag.Get("jsonapi")
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-08 16:11:03 -04:00
|
|
|
if tag == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
args := strings.Split(tag, ",")
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-08 14:49:36 -04:00
|
|
|
if len(args) != 2 {
|
|
|
|
er = errors.New(fmt.Sprintf("jsonapi tag, on %s, had two few arguments", structField.Name))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
if len(args) >= 1 && args[0] != "" {
|
|
|
|
annotation := args[0]
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
if annotation == "primary" {
|
|
|
|
if len(args) >= 2 {
|
|
|
|
node.Id = fmt.Sprintf("%v", fieldValue.Interface())
|
|
|
|
node.Type = args[1]
|
|
|
|
} else {
|
2015-07-06 17:39:24 -04:00
|
|
|
er = errors.New("'type' as second argument required for 'primary'")
|
|
|
|
return false
|
2015-07-06 13:38:42 -04:00
|
|
|
}
|
|
|
|
} else if annotation == "attr" {
|
|
|
|
if node.Attributes == nil {
|
|
|
|
node.Attributes = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) >= 2 {
|
2015-07-06 17:35:17 -04:00
|
|
|
if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
|
2015-07-09 15:07:33 -04:00
|
|
|
isZeroMethod := fieldValue.MethodByName("IsZero")
|
|
|
|
isZero := isZeroMethod.Call(make([]reflect.Value, 0))[0].Interface().(bool)
|
|
|
|
if isZero {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:35:17 -04:00
|
|
|
unix := fieldValue.MethodByName("Unix")
|
|
|
|
val := unix.Call(make([]reflect.Value, 0))[0]
|
|
|
|
node.Attributes[args[1]] = val.Int()
|
|
|
|
} else {
|
|
|
|
node.Attributes[args[1]] = fieldValue.Interface()
|
|
|
|
}
|
2015-07-06 13:38:42 -04:00
|
|
|
} else {
|
2015-07-06 17:39:24 -04:00
|
|
|
er = errors.New("'type' as second argument required for 'primary'")
|
|
|
|
return false
|
2015-07-06 13:38:42 -04:00
|
|
|
}
|
|
|
|
} else if annotation == "relation" {
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-06 13:38:42 -04:00
|
|
|
isSlice := fieldValue.Type().Kind() == reflect.Slice
|
|
|
|
|
|
|
|
if (isSlice && fieldValue.Len() < 1) || (!isSlice && fieldValue.IsNil()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Relationships == nil {
|
|
|
|
node.Relationships = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if included == nil {
|
|
|
|
included = make([]*JsonApiNode, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isSlice {
|
2015-07-08 16:28:38 -04:00
|
|
|
relationship, err := visitModelNodeRelationships(args[1], fieldValue, sideload)
|
2015-07-06 20:04:26 -04:00
|
|
|
d := relationship[args[1]].Data
|
2015-07-06 13:38:42 -04:00
|
|
|
|
|
|
|
if err == nil {
|
2015-07-08 16:28:38 -04:00
|
|
|
if sideload {
|
|
|
|
shallowNodes := make([]*JsonApiNode, 0)
|
|
|
|
for _, node := range d {
|
|
|
|
included = append(included, node)
|
2015-07-06 13:38:42 -04:00
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
shallowNode := *node
|
|
|
|
shallowNode.Attributes = nil
|
|
|
|
shallowNodes = append(shallowNodes, &shallowNode)
|
2015-07-06 13:38:42 -04:00
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
}
|
2015-07-06 20:04:26 -04:00
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
node.Relationships[args[1]] = &JsonApiRelationshipManyNode{Data: shallowNodes}
|
|
|
|
} else {
|
|
|
|
node.Relationships[args[1]] = &JsonApiRelationshipManyNode{Data: d}
|
|
|
|
}
|
2015-07-05 13:59:35 -04:00
|
|
|
} else {
|
2015-07-06 17:39:24 -04:00
|
|
|
er = err
|
|
|
|
return false
|
2015-07-06 13:38:42 -04:00
|
|
|
}
|
|
|
|
} else {
|
2015-07-08 16:28:38 -04:00
|
|
|
relationship, _, err := visitModelNode(fieldValue.Interface(), sideload)
|
2015-07-06 13:38:42 -04:00
|
|
|
if err == nil {
|
2015-07-08 16:28:38 -04:00
|
|
|
if sideload {
|
|
|
|
shallowNode := *relationship
|
|
|
|
shallowNode.Attributes = nil
|
2015-07-05 15:12:09 -04:00
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
included = append(included, relationship)
|
2015-07-05 15:12:09 -04:00
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
node.Relationships[args[1]] = &JsonApiRelationshipOneNode{Data: &shallowNode}
|
|
|
|
} else {
|
|
|
|
node.Relationships[args[1]] = &JsonApiRelationshipOneNode{Data: relationship}
|
|
|
|
}
|
2015-07-06 13:38:42 -04:00
|
|
|
} else {
|
2015-07-06 17:39:24 -04:00
|
|
|
er = err
|
|
|
|
return false
|
2015-07-05 13:59:35 -04:00
|
|
|
}
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
2015-07-06 13:38:42 -04:00
|
|
|
|
|
|
|
} else {
|
2015-07-06 17:39:24 -04:00
|
|
|
er = errors.New(fmt.Sprintf("Unsupported jsonapi tag annotation, %s", annotation))
|
|
|
|
return false
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2015-07-06 17:39:24 -04:00
|
|
|
if er != nil {
|
|
|
|
return nil, nil, er
|
2015-07-05 13:59:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return node, included, nil
|
|
|
|
}
|
|
|
|
|
2015-07-08 16:28:38 -04:00
|
|
|
func visitModelNodeRelationships(relationName string, models reflect.Value, sideload bool) (map[string]*JsonApiRelationshipManyNode, error) {
|
2015-07-07 12:52:38 -04:00
|
|
|
m := make(map[string]*JsonApiRelationshipManyNode)
|
2015-07-05 13:59:35 -04:00
|
|
|
nodes := make([]*JsonApiNode, 0)
|
|
|
|
|
|
|
|
for i := 0; i < models.Len(); i++ {
|
2015-07-08 16:28:38 -04:00
|
|
|
node, _, err := visitModelNode(models.Index(i).Interface(), sideload)
|
2015-07-05 13:59:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes, node)
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
|
|
|
|
2015-07-07 12:52:38 -04:00
|
|
|
m[relationName] = &JsonApiRelationshipManyNode{Data: nodes}
|
2015-07-05 13:59:35 -04:00
|
|
|
|
2015-07-06 20:04:26 -04:00
|
|
|
return m, nil
|
2015-07-05 11:59:30 -04:00
|
|
|
}
|
2015-07-07 12:52:38 -04:00
|
|
|
|
|
|
|
func deleteNode(a []*JsonApiNode, i int) []*JsonApiNode {
|
|
|
|
if i < len(a)-1 {
|
|
|
|
a = append(a[:i], a[i+1:]...)
|
|
|
|
} else {
|
|
|
|
a = a[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|