/
jsonapi.go
173 lines (150 loc) · 4.46 KB
/
jsonapi.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
package util
import (
"reflect"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
// JSONAPISchema is an interface used for generating the proper JSON API response packet
type JSONAPISchema interface {
GetAPITag(lookup string) string
}
// MarshalResponse takes an object that implements the JSONAPISchema interface and marshals it to a map[string]interface{}
// Sub-structs will be placed automatically under their parent (meta/attr) so there is no need to have that tag on
// any sub-struct
func MarshalResponse(s JSONAPISchema) map[string]interface{} {
var response = make(map[string]interface{})
var data = make(map[string]interface{})
ift := reflect.TypeOf(s)
ifv := reflect.ValueOf(s)
attributes, meta, id, objectType := pullVals(ift, ifv)
data["attributes"] = attributes
data["id"] = id
data["type"] = objectType
if len(meta) > 0 {
response["meta"] = meta
}
response["data"] = data
return response
}
func pullVals(ift reflect.Type, ifv reflect.Value) (map[string]interface{}, map[string]interface{}, string, string) {
var attr = make(map[string]interface{})
var meta = make(map[string]interface{})
var recordType = ""
var id = ""
// Iterate over all the fields in the value
for i := 0; i < ift.NumField(); i++ {
var value interface{}
// Get the tags in array/slice form
split := GetTags(ift.Field(i))
if split == nil {
// It's an anonymous field, ignore it
continue
}
if ifv.Field(i).Kind() == reflect.Struct {
var subID = ""
var subMeta map[string]interface{}
value, subMeta, subID, _ = pullVals(ift.Field(i).Type, ifv.Field(i))
if len(subMeta) > 0 {
meta[split[0]] = subMeta
}
if subID != "" && id == "" {
id = subID
}
}
value = ifv.Field(i).Interface()
// HACKity hack hack but it does what we need it to and isn't specifically
// BAD code, just code that only currently applies to this specific key
// Check if the current key is createdAt
if len(split) > 1 && split[1] == "createdAt" {
// Format the datetime as human-readable RFC1123
t, err := time.Parse(time.RFC3339, value.(string))
if err != nil {
// If it's stored badly somehow record the error and move on
log.Error(err)
continue
}
value = t.Format(time.RFC1123)
}
// Anything after the first element is tags, figure out which we want
for _, tag := range split {
// Need to set the keys w/ their names here if it's a struct
switch tag {
case "attr":
// Attribute
attr[split[1]] = value
case "meta":
// Meta information about the request
meta[split[1]] = value
case "primary":
// It's the primary key/record ID & record type
id = ifv.Field(i).String()
recordType = split[1]
}
}
}
return attr, meta, id, recordType
}
// GetTags takes a reflect.StructField object and returns a slice of the associated tags
func GetTags(obj reflect.StructField) []string {
if obj.Anonymous {
// Anonymous field, don't try to access
return nil
}
// Get the jsonapi tags for this element
tags := obj.Tag.Get("jsonapi")
// Split the tags on the , character
split := strings.Split(tags, ",")
return split
}
// FieldTag looks up a single field in the provided object and returns the tag for it
func FieldTag(obj interface{}, lookup string, tag string) string {
ift := reflect.TypeOf(obj)
// Our code only works with structs
if ift.Kind() != reflect.Struct {
return ""
}
field, ok := ift.FieldByName(lookup)
if !ok {
return ""
}
if field.Type.Kind() == reflect.Struct {
}
resp, ok := field.Tag.Lookup(tag)
if !ok {
return ""
}
return resp
}
// ReturnTags takes an interface and a string to look up the tag for.
// If the first argument passed is a struct, then it starts looking for the
// tag given by the second argument & will return any it finds in map form
func ReturnTags(obj interface{}, lookup string) map[string]interface{} {
response := make(map[string]interface{})
ift := reflect.TypeOf(obj)
ifv := reflect.ValueOf(obj)
// Our code only works with structs
if ift.Kind() != reflect.Struct {
return nil
}
for i := 0; i < ift.NumField(); i++ {
// Skip the current field if it's anonymous
if ift.Field(i).Anonymous {
continue
}
fieldName := ift.Field(i).Name
switch ifv.Field(i).Kind() {
case reflect.Struct:
// Iterate over the
resp := ReturnTags(ifv.Field(i).Interface(), lookup)
response[fieldName] = resp
default:
tag, ok := ift.Field(i).Tag.Lookup(lookup)
if !ok {
response[fieldName] = nil
}
response[fieldName] = tag
}
}
return response
}