forked from mfcochauxlaberge/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.go
More file actions
185 lines (147 loc) · 3.56 KB
/
registry.go
File metadata and controls
185 lines (147 loc) · 3.56 KB
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
185
package jsonapi
import (
"database/sql"
"fmt"
"reflect"
"strings"
"sync"
)
// Registry ...
type Registry struct {
sync.Mutex
Types map[string]Type
}
// NewRegistry ...
func NewRegistry() *Registry {
return &Registry{
Types: map[string]Type{},
}
}
// RegisterType checks and registers the provided value as a type.
func (r *Registry) RegisterType(v interface{}) {
r.Lock()
defer r.Unlock()
value := reflect.ValueOf(v)
if value.Kind() == reflect.Struct {
value = reflect.New(value.Type())
}
if value.Kind() != reflect.Ptr {
panic("jsonapi: RegisterType requires a struct or a pointer to a struct")
}
if value.Elem().Kind() != reflect.Struct {
panic("jsonapi: RegisterType requires a struct or a pointer to a struct")
}
err := CheckType(value.Elem().Interface())
if err != nil {
panic(err)
}
res := Wrap(value.Interface())
value = value.Elem()
// Get ID field
idField, _ := value.Type().FieldByName("ID")
// Get name
resType := idField.Tag.Get("api")
fields := []string{}
// Get attributes
attrs := map[string]Attr{}
for i := 0; i < value.NumField(); i++ {
sf := value.Type().Field(i)
if sf.Tag.Get("api") == "attr" {
var n string
if n = sf.Tag.Get("json"); n == "" {
n = sf.Name
}
def := sql.NullString{}
def.String = ""
attrs[n] = Attr{
Name: n,
Type: sf.Type.String(),
Null: strings.HasPrefix(sf.Type.String(), "*"),
Default: def,
}
fields = append(fields, n)
}
}
// Get relationships
rels := map[string]Rel{}
for i := 0; i < value.NumField(); i++ {
sf := value.Type().Field(i)
if strings.Contains(sf.Tag.Get("api"), "rel,") {
var t, i string
if s := strings.Split(sf.Tag.Get("api"), ","); len(s) >= 2 {
t = s[1]
if len(s) == 3 {
i = s[2]
}
}
var toOne bool
if sf.Type.String() == "string" {
toOne = true
} else if sf.Type.String() == "[]string" {
toOne = false
}
var n string
if n = sf.Tag.Get("json"); n == "" {
n = sf.Name
}
rels[n] = Rel{
Name: n,
Type: t,
ToOne: toOne,
InverseName: i,
InverseType: resType,
InverseToOne: false, // should be set in Check()
}
fields = append(fields, n)
}
}
if _, ok := r.Types[resType]; ok {
panic("jsonapi: type with same name already exists")
}
r.Types[resType] = Type{
Name: resType,
Fields: fields,
Attrs: attrs,
Rels: rels,
Default: res,
}
}
// Check ...
func (r *Registry) Check() []error {
errs := []error{}
// Check and set the inverse relationships
for t, typ := range r.Types {
for re, rel := range typ.Rels {
if _, ok := r.Types[rel.Type]; !ok {
errs = append(errs, fmt.Errorf("jsonapi: the target type of relationship %s of type %s does not exist", rel.Name, typ.Name))
}
if rel.InverseName != "" {
if invRel, ok := r.Types[rel.Type].Rels[rel.InverseName]; !ok {
errs = append(errs, fmt.Errorf("jsonapi: the inverse of relationship %s of type %s does not exist", rel.Name, typ.Name))
} else {
rel.InverseToOne = invRel.ToOne
// rel.InverseToMany = invRel.ToMany
}
typ.Rels[re] = rel
}
}
r.Types[t] = typ
}
return errs
}
// Resource ...
func (r *Registry) Resource(name string) Resource {
if t, ok := r.Types[name]; ok {
return t.Default.New()
}
panic(fmt.Sprintf(`jsonapi: type "%s" not found`, name))
}
// Collection ...
func (r *Registry) Collection(name string) Collection {
if t, ok := r.Types[name]; ok {
r := t.Default.New()
col := WrapCollection(r)
return col
}
panic(fmt.Sprintf(`jsonapi: type "%s" not found`, name))
}