-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
184 lines (161 loc) · 4.62 KB
/
encoder.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package jsonapi
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"reflect"
"strconv"
)
type Encoder interface {
Encode(interface{}) error
}
type coreEncoder struct {
encoder *json.Encoder
}
func NewEncoder(writer io.Writer) Encoder {
return &coreEncoder{
encoder: json.NewEncoder(writer),
}
}
func NewIndentEncoder(writer io.Writer, prefix, indent string) Encoder {
encoder := json.NewEncoder(writer)
encoder.SetIndent(prefix, indent)
return &coreEncoder{
encoder: encoder,
}
}
func (enc *coreEncoder) Encode(model interface{}) error {
cache := NewCache()
// TODO: fix duplication in buildNode
val := reflect.ValueOf(model)
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
var result Document
if val.Kind() == reflect.Slice {
result = newSliceDocument()
for i := 0; i < val.Len(); i++ {
resource, err := buildResource(cache, val.Index(i), result, false)
if err != nil {
return errors.Wrap(err, "unable to serialize resource")
}
result.AppendData(resource)
}
} else {
result = newSingleDocument()
resource, err := buildResource(cache, val, result, false)
if err != nil {
return errors.Wrap(err, "unable to serialize resource")
}
result.AppendData(resource)
}
return enc.encoder.Encode(result)
}
func buildResource(cache Cache, val reflect.Value, includer Includer, addIncluded bool) (ResourceObject, error) {
resource := newResourceObject()
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val)
}
typ := val.Type()
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
value := val.Field(i)
tag, err := ParseTag(field)
if err != nil {
return nil, errors.Wrap(err, "invalid jsonapi struct tag")
}
if !tag.IsPrimary() {
continue
}
resourceID, err := formatID(value)
if err != nil {
return nil, errors.Wrap(err, "unable to serialize resource id")
}
resource.SetID(resourceID)
resourceType, err := tag.GetTypeIdentifier()
if err != nil {
return nil, errors.Wrap(err, "unable to serialize resource type")
}
resource.SetType(resourceType)
}
if ok, existing := cache.Add(resource); !ok {
return existing, nil
}
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
value := val.Field(i)
tag, err := ParseTag(field)
if err != nil {
return nil, errors.Wrap(err, "invalid jsonapi struct tag")
}
if tag.IsPrimary() {
continue
} else if tag.IsAttribute() {
attrName, err := tag.GetAttributeName()
if err != nil {
return nil, errors.Wrap(err, "unable to serialize attribute key")
}
// Using value.Interface() so the json encoder can handle the
// serialization of attributes
resource.GetAttributes().Append(attrName, value.Interface())
} else if tag.IsRelation() {
relationName, err := tag.GetRelationName()
if err != nil {
return nil, errors.Wrap(err, "unable to serialize relation key")
}
if value.Kind() == reflect.Ptr {
value = reflect.Indirect(value)
}
var relManager Relationship
if value.Kind() == reflect.Slice {
relManager = newSliceRelationship()
for x := 0; x < value.Len(); x++ {
relationResource, err := buildResource(cache, value.Index(x), includer, true)
if err != nil {
return nil, errors.Wrap(err, "unable to serialize relation resource")
}
relManager.Append(relationResource)
}
} else {
relManager = newSingleRelationship()
relationResource, err := buildResource(cache, value, includer, true)
if err != nil {
return nil, errors.Wrap(err, "unable to serialize relation resource")
}
relManager.Append(relationResource)
}
resource.GetRelationships().Append(relationName, relManager)
}
}
if addIncluded {
includer.AppendIncluded(resource)
}
return resource, nil
}
// JSONAPI expects IDs to be a string so we need to convert
func formatID(value reflect.Value) (string, error) {
if value.Kind() == reflect.Interface {
value = value.Elem()
}
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(value.Int(), 10), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(value.Uint(), 10), nil
case reflect.Float32:
v, _ := value.Interface().(float32)
return fmt.Sprintf("%G", v), nil
case reflect.Float64:
return fmt.Sprintf("%G", value.Float()), nil
case reflect.Complex64:
v, _ := value.Interface().(complex64)
return fmt.Sprintf("%G", v), nil
case reflect.Complex128:
return fmt.Sprintf("%G", value.Complex()), nil
case reflect.String:
return value.String(), nil
default:
return "", fmt.Errorf("Unable to convert ID to string")
}
}