Merge pull request #19 from shwoodard/unmarshal-many

Add UnmarshalManyPayload
This commit is contained in:
Sam Woodard 2015-10-27 09:33:41 -07:00
commit 00f3f5774e
1 changed files with 42 additions and 0 deletions

View File

@ -66,6 +66,48 @@ func UnmarshalPayload(in io.Reader, model interface{}) error {
}
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 {
model := reflect.New(t)
err := unmarshalNode(data, reflect.ValueOf(model), &includedMap)
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
}
}
func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) (err error) {
defer func() {
if r := recover(); r != nil {