2015-07-06 16:40:43 -04:00
|
|
|
package jsonapi
|
|
|
|
|
|
|
|
import (
|
2015-09-17 18:55:53 -04:00
|
|
|
"bytes"
|
2015-07-08 12:34:37 -04:00
|
|
|
"encoding/json"
|
2015-07-06 16:40:43 -04:00
|
|
|
"errors"
|
2015-07-07 18:46:20 -04:00
|
|
|
"fmt"
|
2015-07-08 12:34:37 -04:00
|
|
|
"io"
|
2015-07-06 16:40:43 -04:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-07-13 12:48:26 -04:00
|
|
|
// Convert an io into a struct instance using jsonapi tags on struct fields.
|
|
|
|
// Method supports single request payloads only, at the moment. Bulk creates and updates
|
|
|
|
// are not supported yet.
|
|
|
|
//
|
2015-07-13 14:23:03 -04:00
|
|
|
// Will Unmarshal embedded and sideloaded payloads. The latter is only possible if the
|
|
|
|
// object graph is complete. That is, in the "relationships" data there are type and id,
|
|
|
|
// keys that correspond to records in the "included" array.
|
|
|
|
//
|
2015-07-13 12:48:26 -04:00
|
|
|
// For example you could pass it, in, req.Body and, model, a BlogPost
|
|
|
|
// struct instance to populate in an http handler,
|
|
|
|
//
|
2015-07-13 18:04:21 -04:00
|
|
|
// func CreateBlog(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// blog := new(Blog)
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
2015-07-13 18:04:21 -04:00
|
|
|
// if err := jsonapi.UnmarshalPayload(r.Body, blog); err != nil {
|
|
|
|
// http.Error(w, err.Error(), 500)
|
|
|
|
// return
|
|
|
|
// }
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
2015-07-13 18:04:21 -04:00
|
|
|
// // ...do stuff with your blog...
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
2015-07-13 18:04:21 -04:00
|
|
|
// w.WriteHeader(201)
|
|
|
|
// w.Header().Set("Content-Type", "application/vnd.api+json")
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
2015-07-13 18:04:21 -04:00
|
|
|
// if err := jsonapi.MarshalOnePayload(w, blog); err != nil {
|
|
|
|
// http.Error(w, err.Error(), 500)
|
|
|
|
// }
|
|
|
|
// }
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Visit https://github.com/shwoodard/jsonapi#create for more info.
|
|
|
|
//
|
2015-07-13 14:23:03 -04:00
|
|
|
// model interface{} should be a pointer to a struct.
|
2015-07-10 12:07:12 -04:00
|
|
|
func UnmarshalPayload(in io.Reader, model interface{}) error {
|
|
|
|
payload := new(OnePayload)
|
2015-07-08 12:34:37 -04:00
|
|
|
|
|
|
|
if err := json.NewDecoder(in).Decode(payload); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-10 14:41:54 -04:00
|
|
|
if payload.Included != nil {
|
|
|
|
includedMap := make(map[string]*Node)
|
|
|
|
for _, included := range payload.Included {
|
|
|
|
key := fmt.Sprintf("%s,%s", included.Type, included.Id)
|
|
|
|
includedMap[key] = included
|
|
|
|
}
|
|
|
|
|
|
|
|
return unmarshalNode(payload.Data, reflect.ValueOf(model), &includedMap)
|
|
|
|
} else {
|
|
|
|
return unmarshalNode(payload.Data, reflect.ValueOf(model), nil)
|
|
|
|
}
|
|
|
|
|
2015-07-07 18:46:20 -04:00
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-10-27 12:29:56 -04:00
|
|
|
func UnmarshalManyPayload(in io.Reader, t reflect.Type) ([]interface{}, error) {
|
|
|
|
payload := new(ManyPayload)
|
|
|
|
|
|
|
|
if err := json.NewDecoder(in).Decode(payload); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Included != nil {
|
|
|
|
includedMap := make(map[string]*Node)
|
|
|
|
for _, included := range payload.Included {
|
|
|
|
key := fmt.Sprintf("%s,%s", included.Type, included.Id)
|
|
|
|
includedMap[key] = included
|
|
|
|
}
|
|
|
|
|
|
|
|
var models []interface{}
|
|
|
|
for _, data := range payload.Data {
|
2015-10-28 13:08:50 -04:00
|
|
|
model := reflect.New(t.Elem())
|
|
|
|
err := unmarshalNode(data, model, &includedMap)
|
2015-10-27 12:29:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
models = append(models, model.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
return models, nil
|
|
|
|
} else {
|
|
|
|
var models []interface{}
|
|
|
|
|
|
|
|
for _, data := range payload.Data {
|
|
|
|
model := reflect.New(t.Elem())
|
|
|
|
err := unmarshalNode(data, model, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
models = append(models, model.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
return models, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-29 13:29:58 -04:00
|
|
|
func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("data is not a jsonapi representation of '%v'", model.Type())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-07-07 18:46:20 -04:00
|
|
|
modelValue := model.Elem()
|
|
|
|
modelType := model.Type().Elem()
|
2015-07-06 16:40:43 -04:00
|
|
|
|
|
|
|
var er error
|
2015-07-06 16:41:38 -04:00
|
|
|
|
|
|
|
var i = 0
|
2015-07-06 16:40:43 -04:00
|
|
|
modelType.FieldByNameFunc(func(name string) bool {
|
2015-07-07 18:46:20 -04:00
|
|
|
if er != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldType := modelType.Field(i)
|
2015-07-20 19:10:52 -04:00
|
|
|
tag := fieldType.Tag.Get("jsonapi")
|
|
|
|
if tag == "" {
|
|
|
|
i += 1
|
|
|
|
return false
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-07-20 19:10:52 -04:00
|
|
|
fieldValue := modelValue.Field(i)
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-07-20 19:10:52 -04:00
|
|
|
i += 1
|
2015-07-06 16:40:43 -04:00
|
|
|
|
|
|
|
args := strings.Split(tag, ",")
|
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if len(args) < 1 {
|
|
|
|
er = BadJSONAPIStructTag{fieldType.Name}
|
2015-07-08 14:49:36 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
annotation := args[0]
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if (annotation == "client-id" && len(args) != 1) || (annotation != "client-id" && len(args) != 2) {
|
|
|
|
er = BadJSONAPIStructTag{fieldType.Name}
|
|
|
|
return false
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if annotation == "primary" {
|
|
|
|
if data.Id == "" {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if data.Type != args[1] {
|
|
|
|
er = errors.New("Trying to Unmarshal a type that does not match")
|
|
|
|
return false
|
|
|
|
}
|
2015-07-09 15:27:03 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if fieldValue.Kind() == reflect.String {
|
|
|
|
fieldValue.Set(reflect.ValueOf(data.Id))
|
|
|
|
} else if fieldValue.Kind() == reflect.Int {
|
|
|
|
id, err := strconv.Atoi(data.Id)
|
|
|
|
if err != nil {
|
|
|
|
er = err
|
2015-07-10 11:20:49 -04:00
|
|
|
return false
|
|
|
|
}
|
2015-09-10 18:55:51 -04:00
|
|
|
fieldValue.SetInt(int64(id))
|
|
|
|
} else {
|
|
|
|
er = errors.New("Unsuppored data type for primary key, not int or string")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if annotation == "client-id" {
|
|
|
|
if data.ClientId == "" {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-09 15:27:03 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
fieldValue.Set(reflect.ValueOf(data.ClientId))
|
|
|
|
} else if annotation == "attr" {
|
|
|
|
attributes := data.Attributes
|
|
|
|
if attributes == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
val := attributes[args[1]]
|
2015-07-09 15:27:03 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
// next if the attribute was not included in the request
|
|
|
|
if val == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
v := reflect.ValueOf(val)
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
|
|
|
|
var at int64
|
2015-07-06 17:35:17 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if v.Kind() == reflect.Float64 {
|
|
|
|
at = int64(v.Interface().(float64))
|
|
|
|
} else if v.Kind() == reflect.Int {
|
|
|
|
at = v.Int()
|
|
|
|
} else {
|
|
|
|
er = errors.New("Only numbers can be parsed as dates, unix timestamps")
|
2015-07-10 11:20:49 -04:00
|
|
|
return false
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
t := time.Unix(at, 0)
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
fieldValue.Set(reflect.ValueOf(t))
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-10-28 13:08:50 -04:00
|
|
|
if fieldValue.Type() == reflect.TypeOf([]string(nil)) {
|
|
|
|
values := make([]string, v.Len())
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
values[i] = v.Index(i).Interface().(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(values))
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-13 12:10:10 -04:00
|
|
|
if fieldValue.Type() == reflect.TypeOf(new(time.Time)) {
|
|
|
|
var at int64
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Float64 {
|
|
|
|
at = int64(v.Interface().(float64))
|
|
|
|
} else if v.Kind() == reflect.Int {
|
|
|
|
at = v.Int()
|
|
|
|
} else {
|
|
|
|
er = errors.New("Only numbers can be parsed as dates, unix timestamps")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
v := time.Unix(at, 0)
|
|
|
|
t := &v
|
|
|
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(t))
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if fieldValue.Kind() == reflect.Int && v.Kind() == reflect.Float64 {
|
|
|
|
fieldValue.Set(reflect.ValueOf(int(v.Interface().(float64))))
|
|
|
|
} else {
|
|
|
|
fieldValue.Set(reflect.ValueOf(val))
|
|
|
|
}
|
|
|
|
} else if annotation == "relation" {
|
|
|
|
isSlice := fieldValue.Type().Kind() == reflect.Slice
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if data.Relationships == nil || data.Relationships[args[1]] == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
if isSlice {
|
2015-09-17 18:55:53 -04:00
|
|
|
relationship := new(RelationshipManyNode)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
json.NewEncoder(buf).Encode(data.Relationships[args[1]])
|
|
|
|
json.NewDecoder(buf).Decode(relationship)
|
|
|
|
|
|
|
|
data := relationship.Data
|
2015-09-10 18:55:51 -04:00
|
|
|
models := reflect.New(fieldValue.Type()).Elem()
|
2015-07-10 11:30:59 -04:00
|
|
|
|
2015-09-17 18:55:53 -04:00
|
|
|
for _, n := range data {
|
2015-09-10 18:55:51 -04:00
|
|
|
m := reflect.New(fieldValue.Type().Elem().Elem())
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-09-17 18:55:53 -04:00
|
|
|
if err := unmarshalNode(fullNode(n, included), m, included); err != nil {
|
2015-07-07 18:46:20 -04:00
|
|
|
er = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
models = reflect.Append(models, m)
|
2015-07-07 18:46:20 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
fieldValue.Set(models)
|
2015-07-07 18:46:20 -04:00
|
|
|
} else {
|
2015-09-17 18:55:53 -04:00
|
|
|
relationship := new(RelationshipOneNode)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
json.NewEncoder(buf).Encode(data.Relationships[args[1]])
|
|
|
|
json.NewDecoder(buf).Decode(relationship)
|
|
|
|
|
2015-09-10 18:55:51 -04:00
|
|
|
m := reflect.New(fieldValue.Type().Elem())
|
2015-09-17 18:55:53 -04:00
|
|
|
|
|
|
|
if err := unmarshalNode(fullNode(relationship.Data, included), m, included); err != nil {
|
2015-09-10 18:55:51 -04:00
|
|
|
er = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldValue.Set(m)
|
2015-07-06 16:40:43 -04:00
|
|
|
}
|
2015-09-10 18:55:51 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
er = errors.New(fmt.Sprintf("Unsupported jsonapi tag annotation, %s", annotation))
|
2015-07-06 16:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
if er != nil {
|
|
|
|
return er
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-09-17 18:55:53 -04:00
|
|
|
func fullNode(n *Node, included *map[string]*Node) *Node {
|
|
|
|
includedKey := fmt.Sprintf("%s,%s", n.Type, n.Id)
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2015-09-17 18:55:53 -04:00
|
|
|
if included != nil && (*included)[includedKey] != nil {
|
|
|
|
return (*included)[includedKey]
|
2015-07-07 18:46:20 -04:00
|
|
|
}
|
|
|
|
|
2015-09-17 18:55:53 -04:00
|
|
|
return n
|
2015-07-07 18:46:20 -04:00
|
|
|
}
|