-
Notifications
You must be signed in to change notification settings - Fork 939
/
include.go
58 lines (45 loc) · 1002 Bytes
/
include.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package patreonapi
import (
"encoding/json"
"errors"
"reflect"
)
var TypeMap = map[string]interface{}{
"user": UserAttributes{},
}
type Include struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes json.RawMessage `json:"attributes"`
Decoded interface{} `json:"-"`
}
func DecodeIncludes(includes []*Include) error {
for _, v := range includes {
dec, err := DecodeInclude(v)
if err != nil {
return err
}
v.Decoded = dec
}
return nil
}
func DecodeInclude(include *Include) (interface{}, error) {
t, ok := TypeMap[include.Type]
if !ok {
return nil, errors.New("Unknown include: " + include.Type)
}
typ := reflect.TypeOf(t)
dst := reflect.New(typ).Interface()
err := json.Unmarshal(include.Attributes, dst)
return dst, err
}
type Relationships struct {
User RelationShip `json:"user"`
}
type RelationShip struct {
Data *RelationshipData `json"data"`
}
type RelationshipData struct {
Type string `json:"type"`
ID string `json:"id"`
}