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"
|
|
|
|
)
|
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
const (
|
|
|
|
unsuportedStructTagMsg = "Unsupported jsonapi tag annotation, %s"
|
|
|
|
)
|
2016-01-05 16:13:24 -05:00
|
|
|
|
|
|
|
var (
|
2016-07-22 21:34:38 -04:00
|
|
|
// ErrInvalidTime is returned when a struct has a time.Time type field, but
|
|
|
|
// the JSON value was not a unix timestamp integer.
|
|
|
|
ErrInvalidTime = errors.New("Only numbers can be parsed as dates, unix timestamps")
|
2016-09-22 16:58:07 -04:00
|
|
|
// ErrInvalidISO8601 is returned when a struct has a time.Time type field and includes
|
|
|
|
// "iso8601" in the tag spec, but the JSON value was not an ISO8601 timestamp string.
|
|
|
|
ErrInvalidISO8601 = errors.New("Only strings can be parsed as dates, ISO8601 timestamps")
|
2017-01-31 14:18:51 -05:00
|
|
|
// ErrUnknownFieldNumberType is returned when the JSON value was a float
|
|
|
|
// (numeric) but the Struct field was a non numeric type (i.e. not int, uint,
|
|
|
|
// float, etc)
|
|
|
|
ErrUnknownFieldNumberType = errors.New("The struct field was not of a known number type")
|
|
|
|
// ErrUnsupportedPtrType is returned when the Struct field was a pointer but
|
|
|
|
// the JSON value was of a different type
|
|
|
|
ErrUnsupportedPtrType = errors.New("Pointer type in struct is not supported")
|
|
|
|
// ErrInvalidType is returned when the given type is incompatible with the expected type.
|
|
|
|
ErrInvalidType = errors.New("Invalid type provided") // I wish we used punctuation.
|
2016-01-05 16:13:24 -05:00
|
|
|
)
|
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
// UnmarshalPayload converts an io into a struct instance using jsonapi tags on
|
|
|
|
// struct fields. This method supports single request payloads only, at the
|
|
|
|
// moment. Bulk creates and updates are not supported yet.
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
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
|
|
|
//
|
2017-01-20 21:06:44 -05:00
|
|
|
// w.Header().Set("Content-Type", jsonapi.MediaType)
|
2017-04-05 14:19:51 -04:00
|
|
|
// w.WriteHeader(201)
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
2017-06-28 20:30:09 -04:00
|
|
|
// if err := jsonapi.MarshalPayload(w, blog); err != nil {
|
2015-07-13 18:04:21 -04:00
|
|
|
// http.Error(w, err.Error(), 500)
|
|
|
|
// }
|
|
|
|
// }
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
|
|
|
//
|
2016-07-22 21:31:33 -04:00
|
|
|
// Visit https://github.com/google/jsonapi#create for more info.
|
2015-07-13 12:48:26 -04:00
|
|
|
//
|
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 {
|
2016-07-05 21:32:15 -04:00
|
|
|
key := fmt.Sprintf("%s,%s", included.Type, included.ID)
|
2015-07-10 14:41:54 -04:00
|
|
|
includedMap[key] = included
|
|
|
|
}
|
|
|
|
|
|
|
|
return unmarshalNode(payload.Data, reflect.ValueOf(model), &includedMap)
|
|
|
|
}
|
2016-07-22 21:34:38 -04:00
|
|
|
return unmarshalNode(payload.Data, reflect.ValueOf(model), nil)
|
2015-07-07 18:46:20 -04:00
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-01-20 19:13:04 -05:00
|
|
|
// UnmarshalManyPayload converts an io into a set of struct instances using
|
|
|
|
// jsonapi tags on the type's struct fields.
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-26 18:25:13 -05:00
|
|
|
models := []interface{}{} // will be populated from the "data"
|
|
|
|
includedMap := map[string]*Node{} // will be populate from the "included"
|
|
|
|
|
2015-10-27 12:29:56 -04:00
|
|
|
if payload.Included != nil {
|
|
|
|
for _, included := range payload.Included {
|
2016-07-05 21:32:15 -04:00
|
|
|
key := fmt.Sprintf("%s,%s", included.Type, included.ID)
|
2015-10-27 12:29:56 -04:00
|
|
|
includedMap[key] = included
|
|
|
|
}
|
2016-07-22 21:34:38 -04:00
|
|
|
}
|
2015-10-27 12:29:56 -04:00
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
for _, data := range payload.Data {
|
|
|
|
model := reflect.New(t.Elem())
|
2017-01-26 18:25:13 -05:00
|
|
|
err := unmarshalNode(data, model, &includedMap)
|
2016-07-22 21:34:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
models = append(models, model.Interface())
|
2015-10-27 12:29:56 -04:00
|
|
|
}
|
|
|
|
|
2016-07-22 21:34:38 -04:00
|
|
|
return models, nil
|
2015-10-27 12:29:56 -04:00
|
|
|
}
|
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// unmarshalNode handles embedded struct models from top to down.
|
|
|
|
// it loops through the struct fields, handles attributes/relations at that level first
|
|
|
|
// the handling the embedded structs are done last, so that you get the expected composition behavior
|
|
|
|
// data (*Node) attributes are cleared on each success.
|
|
|
|
// relations/sideloaded models use deeply copied Nodes (since those sideloaded models can be referenced in multiple relations)
|
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
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
type embedded struct {
|
|
|
|
structField, model reflect.Value
|
|
|
|
}
|
|
|
|
embeddeds := []*embedded{}
|
2015-07-06 16:41:38 -04:00
|
|
|
|
2016-01-05 16:13:24 -05:00
|
|
|
for i := 0; i < modelValue.NumField(); i++ {
|
2015-07-07 18:46:20 -04:00
|
|
|
fieldType := modelType.Field(i)
|
2017-07-27 14:16:00 -04:00
|
|
|
fieldValue := modelValue.Field(i)
|
|
|
|
tag := fieldType.Tag.Get(annotationJSONAPI)
|
|
|
|
|
|
|
|
// handle explicit ignore annotation
|
|
|
|
if shouldIgnoreField(tag) {
|
2016-01-05 16:13:24 -05:00
|
|
|
continue
|
2015-07-20 19:10:52 -04:00
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// handles embedded structs
|
|
|
|
if isEmbeddedStruct(fieldType) {
|
|
|
|
embeddeds = append(embeddeds,
|
|
|
|
&embedded{
|
|
|
|
model: reflect.ValueOf(fieldValue.Addr().Interface()),
|
|
|
|
structField: fieldValue,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// handles pointers to embedded structs
|
|
|
|
if isEmbeddedStructPtr(fieldType) {
|
|
|
|
embeddeds = append(embeddeds,
|
|
|
|
&embedded{
|
|
|
|
model: reflect.ValueOf(fieldValue.Interface()),
|
|
|
|
structField: fieldValue,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
continue
|
2015-07-08 14:49:36 -04:00
|
|
|
}
|
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// handle tagless; after handling embedded structs (which could be tagless)
|
|
|
|
if tag == "" {
|
|
|
|
continue
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
args := strings.Split(tag, annotationSeperator)
|
|
|
|
// require atleast 1
|
|
|
|
if len(args) < 1 {
|
|
|
|
return ErrBadJSONAPIStructTag
|
2015-09-10 18:55:51 -04:00
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// args[0] == annotation
|
|
|
|
switch args[0] {
|
|
|
|
case annotationClientID:
|
|
|
|
if err := handleClientIDUnmarshal(data, args, fieldValue); err != nil {
|
|
|
|
return err
|
2015-09-10 18:55:51 -04:00
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
case annotationPrimary:
|
|
|
|
if err := handlePrimaryUnmarshal(data, args, fieldType, fieldValue); err != nil {
|
|
|
|
return err
|
2015-09-10 18:55:51 -04:00
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
case annotationAttribute:
|
|
|
|
if err := handleAttributeUnmarshal(data, args, fieldType, fieldValue); err != nil {
|
|
|
|
return err
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
case annotationRelation:
|
|
|
|
if err := handleRelationUnmarshal(data, args, fieldValue, included); err != nil {
|
|
|
|
return err
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
default:
|
|
|
|
return fmt.Errorf(unsuportedStructTagMsg, args[0])
|
|
|
|
}
|
|
|
|
}
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// handle embedded last
|
|
|
|
for _, em := range embeddeds {
|
|
|
|
// if nil, need to construct and rollback accordingly
|
|
|
|
if em.model.IsNil() {
|
|
|
|
copy := deepCopyNode(data)
|
|
|
|
tmp := reflect.New(em.model.Type().Elem())
|
|
|
|
if err := unmarshalNode(copy, tmp, included); err != nil {
|
|
|
|
return err
|
2015-09-10 18:55:51 -04:00
|
|
|
}
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// had changes; assign value to struct field, replace orig node (data) w/ mutated copy
|
|
|
|
if !reflect.DeepEqual(copy, data) {
|
|
|
|
assign(em.structField, tmp)
|
|
|
|
data = copy
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// handle non-nil scenarios
|
|
|
|
return unmarshalNode(data, em.model, included)
|
|
|
|
}
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-09 15:27:03 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
func handleClientIDUnmarshal(data *Node, args []string, fieldValue reflect.Value) error {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return ErrBadJSONAPIStructTag
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if data.ClientID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// set value and clear clientID to denote it's already been processed
|
|
|
|
fieldValue.Set(reflect.ValueOf(data.ClientID))
|
|
|
|
data.ClientID = ""
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-09 15:27:03 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
func handlePrimaryUnmarshal(data *Node, args []string, fieldType reflect.StructField, fieldValue reflect.Value) error {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrBadJSONAPIStructTag
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if data.ID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// Check the JSON API Type
|
|
|
|
if data.Type != args[1] {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Trying to Unmarshal an object of type %#v, but %#v does not match",
|
|
|
|
data.Type,
|
|
|
|
args[1],
|
|
|
|
)
|
|
|
|
}
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// Deal with PTRS
|
|
|
|
var kind reflect.Kind
|
|
|
|
if fieldValue.Kind() == reflect.Ptr {
|
|
|
|
kind = fieldType.Type.Elem().Kind()
|
|
|
|
} else {
|
|
|
|
kind = fieldType.Type.Kind()
|
|
|
|
}
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
var idValue reflect.Value
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// Handle String case
|
|
|
|
if kind == reflect.String {
|
|
|
|
// ID will have to be transmitted as a string per the JSON API spec
|
|
|
|
idValue = reflect.ValueOf(data.ID)
|
|
|
|
} else {
|
|
|
|
// Value was not a string... only other supported type was a numeric,
|
|
|
|
// which would have been sent as a float value.
|
|
|
|
floatValue, err := strconv.ParseFloat(data.ID, 64)
|
|
|
|
if err != nil {
|
|
|
|
// Could not convert the value in the "id" attr to a float
|
|
|
|
return ErrBadJSONAPIID
|
|
|
|
}
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// Convert the numeric float to one of the supported ID numeric types
|
|
|
|
// (int[8,16,32,64] or uint[8,16,32,64])
|
|
|
|
switch kind {
|
|
|
|
case reflect.Int:
|
|
|
|
n := int(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int8:
|
|
|
|
n := int8(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int16:
|
|
|
|
n := int16(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int32:
|
|
|
|
n := int32(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int64:
|
|
|
|
n := int64(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint:
|
|
|
|
n := uint(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint8:
|
|
|
|
n := uint8(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint16:
|
|
|
|
n := uint16(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint32:
|
|
|
|
n := uint32(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint64:
|
|
|
|
n := uint64(floatValue)
|
|
|
|
idValue = reflect.ValueOf(&n)
|
|
|
|
default:
|
|
|
|
// We had a JSON float (numeric), but our field was not one of the
|
|
|
|
// allowed numeric types
|
|
|
|
return ErrBadJSONAPIID
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 17:35:17 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// set value and clear ID to denote it's already been processed
|
|
|
|
assign(fieldValue, idValue)
|
|
|
|
data.ID = ""
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
func handleRelationUnmarshal(data *Node, args []string, fieldValue reflect.Value, included *map[string]*Node) error {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrBadJSONAPIStructTag
|
|
|
|
}
|
2015-09-10 18:55:51 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if data.Relationships == nil || data.Relationships[args[1]] == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// to-one relationships
|
|
|
|
handler := handleToOneRelationUnmarshal
|
|
|
|
isSlice := fieldValue.Type().Kind() == reflect.Slice
|
|
|
|
if isSlice {
|
|
|
|
// to-many relationship
|
|
|
|
handler = handleToManyRelationUnmarshal
|
|
|
|
}
|
2015-10-28 13:08:50 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
v, err := handler(data.Relationships[args[1]], fieldValue.Type(), included)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set only if there is a val since val can be null (e.g. to disassociate the relationship)
|
|
|
|
if v != nil {
|
|
|
|
fieldValue.Set(*v)
|
|
|
|
}
|
|
|
|
delete(data.Relationships, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-28 13:08:50 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// to-one relationships
|
|
|
|
func handleToOneRelationUnmarshal(relationData interface{}, fieldType reflect.Type, included *map[string]*Node) (*reflect.Value, error) {
|
|
|
|
relationship := new(RelationshipOneNode)
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
json.NewEncoder(buf).Encode(relationData)
|
|
|
|
json.NewDecoder(buf).Decode(relationship)
|
|
|
|
|
|
|
|
m := reflect.New(fieldType.Elem())
|
|
|
|
/*
|
|
|
|
http://jsonapi.org/format/#document-resource-object-relationships
|
|
|
|
http://jsonapi.org/format/#document-resource-object-linkage
|
|
|
|
relationship can have a data node set to null (e.g. to disassociate the relationship)
|
|
|
|
so unmarshal and set fieldValue only if data obj is not null
|
|
|
|
*/
|
|
|
|
if relationship.Data == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-10-28 13:08:50 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if err := unmarshalNode(
|
|
|
|
fullNode(relationship.Data, included),
|
|
|
|
m,
|
|
|
|
included,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
return &m, nil
|
|
|
|
}
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// to-many relationship
|
|
|
|
func handleToManyRelationUnmarshal(relationData interface{}, fieldType reflect.Type, included *map[string]*Node) (*reflect.Value, error) {
|
|
|
|
relationship := new(RelationshipManyNode)
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
json.NewEncoder(buf).Encode(relationData)
|
|
|
|
json.NewDecoder(buf).Decode(relationship)
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
models := reflect.New(fieldType).Elem()
|
2016-09-22 16:58:07 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
rData := relationship.Data
|
|
|
|
for _, n := range rData {
|
|
|
|
m := reflect.New(fieldType.Elem().Elem())
|
2015-10-13 12:10:10 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if err := unmarshalNode(
|
|
|
|
fullNode(n, included),
|
|
|
|
m,
|
|
|
|
included,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-13 12:10:10 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
models = reflect.Append(models, m)
|
|
|
|
}
|
2015-10-13 12:10:10 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
return &models, nil
|
|
|
|
}
|
2015-10-13 12:10:10 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// TODO: break this out into smaller funcs
|
|
|
|
func handleAttributeUnmarshal(data *Node, args []string, fieldType reflect.StructField, fieldValue reflect.Value) error {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return ErrBadJSONAPIStructTag
|
|
|
|
}
|
|
|
|
attributes := data.Attributes
|
|
|
|
if attributes == nil || len(data.Attributes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-10-13 12:10:10 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
var iso8601 bool
|
2016-07-22 21:34:38 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if len(args) > 2 {
|
|
|
|
for _, arg := range args[2:] {
|
|
|
|
if arg == annotationISO8601 {
|
|
|
|
iso8601 = true
|
2016-07-22 21:34:38 -04:00
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
}
|
|
|
|
}
|
2016-07-22 21:34:38 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
val := attributes[args[1]]
|
|
|
|
|
|
|
|
// continue if the attribute was not included in the request
|
|
|
|
if val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-22 21:34:38 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
v := reflect.ValueOf(val)
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// Handle field of type time.Time
|
|
|
|
if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
|
|
|
|
if iso8601 {
|
|
|
|
var tm string
|
|
|
|
if v.Kind() == reflect.String {
|
|
|
|
tm = v.Interface().(string)
|
|
|
|
} else {
|
|
|
|
return ErrInvalidISO8601
|
2015-09-10 18:55:51 -04:00
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
t, err := time.Parse(iso8601TimeFormat, tm)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidISO8601
|
|
|
|
}
|
2015-09-17 18:55:53 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
fieldValue.Set(reflect.ValueOf(t))
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var at int64
|
2015-09-17 18:55:53 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if v.Kind() == reflect.Float64 {
|
|
|
|
at = int64(v.Interface().(float64))
|
|
|
|
} else if v.Kind() == reflect.Int {
|
|
|
|
at = v.Int()
|
|
|
|
} else {
|
|
|
|
return ErrInvalidTime
|
|
|
|
}
|
2015-09-17 18:55:53 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
t := time.Unix(at, 0)
|
2015-07-10 11:30:59 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
fieldValue.Set(reflect.ValueOf(t))
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if fieldValue.Type() == reflect.TypeOf([]string{}) {
|
|
|
|
values := make([]string, v.Len())
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
values[i] = v.Index(i).Interface().(string)
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
fieldValue.Set(reflect.ValueOf(values))
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-07 18:46:20 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
if fieldValue.Type() == reflect.TypeOf(new(time.Time)) {
|
|
|
|
if iso8601 {
|
|
|
|
var tm string
|
|
|
|
if v.Kind() == reflect.String {
|
|
|
|
tm = v.Interface().(string)
|
2015-07-07 18:46:20 -04:00
|
|
|
} else {
|
2017-07-27 14:16:00 -04:00
|
|
|
return ErrInvalidISO8601
|
|
|
|
|
|
|
|
}
|
2017-01-20 19:13:04 -05:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
v, err := time.Parse(iso8601TimeFormat, tm)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidISO8601
|
2015-07-06 16:40:43 -04:00
|
|
|
}
|
2015-09-10 18:55:51 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
t := &v
|
|
|
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(t))
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var at int64
|
|
|
|
|
|
|
|
if v.Kind() == reflect.Float64 {
|
|
|
|
at = int64(v.Interface().(float64))
|
|
|
|
} else if v.Kind() == reflect.Int {
|
|
|
|
at = v.Int()
|
|
|
|
} else {
|
|
|
|
return ErrInvalidTime
|
|
|
|
}
|
|
|
|
|
|
|
|
v := time.Unix(at, 0)
|
|
|
|
t := &v
|
|
|
|
|
|
|
|
fieldValue.Set(reflect.ValueOf(t))
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON value was a float (numeric)
|
|
|
|
if v.Kind() == reflect.Float64 {
|
|
|
|
floatValue := v.Interface().(float64)
|
|
|
|
|
|
|
|
// The field may or may not be a pointer to a numeric; the kind var
|
|
|
|
// will not contain a pointer type
|
|
|
|
var kind reflect.Kind
|
|
|
|
if fieldValue.Kind() == reflect.Ptr {
|
|
|
|
kind = fieldType.Type.Elem().Kind()
|
2015-09-10 18:55:51 -04:00
|
|
|
} else {
|
2017-07-27 14:16:00 -04:00
|
|
|
kind = fieldType.Type.Kind()
|
|
|
|
}
|
|
|
|
|
|
|
|
var numericValue reflect.Value
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case reflect.Int:
|
|
|
|
n := int(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int8:
|
|
|
|
n := int8(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int16:
|
|
|
|
n := int16(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int32:
|
|
|
|
n := int32(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Int64:
|
|
|
|
n := int64(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint:
|
|
|
|
n := uint(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint8:
|
|
|
|
n := uint8(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint16:
|
|
|
|
n := uint16(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint32:
|
|
|
|
n := uint32(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Uint64:
|
|
|
|
n := uint64(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Float32:
|
|
|
|
n := float32(floatValue)
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
case reflect.Float64:
|
|
|
|
n := floatValue
|
|
|
|
numericValue = reflect.ValueOf(&n)
|
|
|
|
default:
|
|
|
|
return ErrUnknownFieldNumberType
|
2015-07-06 16:40:43 -04:00
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
|
|
|
|
assign(fieldValue, numericValue)
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Field was a Pointer type
|
|
|
|
if fieldValue.Kind() == reflect.Ptr {
|
|
|
|
var concreteVal reflect.Value
|
|
|
|
|
|
|
|
switch cVal := val.(type) {
|
|
|
|
case string:
|
|
|
|
concreteVal = reflect.ValueOf(&cVal)
|
|
|
|
case bool:
|
|
|
|
concreteVal = reflect.ValueOf(&cVal)
|
|
|
|
case complex64:
|
|
|
|
concreteVal = reflect.ValueOf(&cVal)
|
|
|
|
case complex128:
|
|
|
|
concreteVal = reflect.ValueOf(&cVal)
|
|
|
|
case uintptr:
|
|
|
|
concreteVal = reflect.ValueOf(&cVal)
|
|
|
|
default:
|
|
|
|
return ErrUnsupportedPtrType
|
|
|
|
}
|
|
|
|
|
|
|
|
if fieldValue.Type() != concreteVal.Type() {
|
|
|
|
return ErrUnsupportedPtrType
|
|
|
|
}
|
|
|
|
|
|
|
|
fieldValue.Set(concreteVal)
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a final catch-all, ensure types line up to avoid a runtime panic.
|
|
|
|
// Ignore interfaces since interfaces are poly
|
|
|
|
if fieldValue.Kind() != reflect.Interface && fieldValue.Kind() != v.Kind() {
|
|
|
|
return ErrInvalidType
|
2016-01-05 16:13:24 -05:00
|
|
|
}
|
2015-07-06 16:40:43 -04:00
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
// set val and clear attribute key so its not processed again
|
|
|
|
fieldValue.Set(reflect.ValueOf(val))
|
|
|
|
delete(data.Attributes, args[1])
|
|
|
|
return nil
|
2015-07-06 16:40:43 -04:00
|
|
|
}
|
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 {
|
2016-07-05 21:32:15 -04:00
|
|
|
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 {
|
2017-07-27 14:16:00 -04:00
|
|
|
return deepCopyNode((*included)[includedKey])
|
2015-07-07 18:46:20 -04:00
|
|
|
}
|
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
return deepCopyNode(n)
|
2015-07-07 18:46:20 -04:00
|
|
|
}
|
Added support for string, int(8,16,32,64), uint(8,16,32,64) and each … (#51)
* Added support for string, int(8,16,32,64), uint(8,16,32,64) and each of their ptr types as acceptable to use for the ID field.
* No longer declaring a new idErr var; also eliminate the switch within a switch - if the ID field was a string or *string it will continue. Added a couple extra tests.
2016-09-22 18:02:30 -04:00
|
|
|
|
|
|
|
// assign will take the value specified and assign it to the field; if
|
|
|
|
// field is expecting a ptr assign will assign a ptr.
|
|
|
|
func assign(field, value reflect.Value) {
|
|
|
|
if field.Kind() == reflect.Ptr {
|
|
|
|
field.Set(value)
|
|
|
|
} else {
|
|
|
|
field.Set(reflect.Indirect(value))
|
|
|
|
}
|
|
|
|
}
|