-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathschema.go
256 lines (220 loc) · 6.13 KB
/
schema.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package jsonapi
import (
"errors"
"fmt"
"sort"
)
// A Schema contains a list of types. It makes sure that all types are valid and
// their relationships are consistent.
//
// Check can be used to validate the relationships between the types.
type Schema struct {
Types []Type
// Rels stores the relationships found in the schema's types. For
// two-way relationships, only one is chosen to be part of this
// map. The chosen one is the one that comes first when sorting
// both relationships in alphabetical order using the type name
// first and then the relationship name.
//
// For example, a type called Directory has a Parent relationship
// and a Children relationship. Both relationships have the same
// type (Directory), so now the name is used for sorting. Children
// comes before Parent, so the relationship Children from type
// Directory is stored here. The other one is not stored to avoid
// duplication (the information is already accessible through the
// inverse relationship).
rels map[string]Rel
}
// AddType adds a type to the schema.
func (s *Schema) AddType(typ Type) error {
// Validation
if typ.Name == "" {
return errors.New("jsonapi: type name is empty")
}
// Make sure the name isn't already used
for i := range s.Types {
if s.Types[i].Name == typ.Name {
return fmt.Errorf("jsonapi: type name %q is already used", typ.Name)
}
}
s.Types = append(s.Types, typ)
return nil
}
// RemoveType removes a type from the schema.
func (s *Schema) RemoveType(typ string) {
for i := range s.Types {
if s.Types[i].Name == typ {
s.Types = append(s.Types[0:i], s.Types[i+1:]...)
}
}
}
// AddAttr adds an attribute to the specified type.
func (s *Schema) AddAttr(typ string, attr Attr) error {
for i := range s.Types {
if s.Types[i].Name == typ {
return s.Types[i].AddAttr(attr)
}
}
return fmt.Errorf("jsonapi: type %q does not exist", typ)
}
// RemoveAttr removes an attribute from the specified type.
func (s *Schema) RemoveAttr(typ string, attr string) {
for i := range s.Types {
if s.Types[i].Name == typ {
s.Types[i].RemoveAttr(attr)
}
}
}
// AddRel adds a relationship to the specified type.
func (s *Schema) AddRel(typ string, rel Rel) error {
for i := range s.Types {
if s.Types[i].Name == typ {
return s.Types[i].AddRel(rel)
}
}
return fmt.Errorf("jsonapi: type %q does not exist", typ)
}
// RemoveRel removes a relationship from the specified type.
func (s *Schema) RemoveRel(typ string, rel string) {
for i := range s.Types {
if s.Types[i].Name == typ {
s.Types[i].RemoveRel(rel)
}
}
}
// AddTwoWayRel adds a two-way relationship to both types involved.
//
// The types must already exist in the schema.
func (s *Schema) AddTwoWayRel(rel Rel) error {
rel1 := rel.Normalize()
rel2 := rel.Invert()
found1 := false
found2 := false
for i := range s.Types {
if s.Types[i].Name == rel1.FromType {
found1 = true
err := s.Types[i].AddRel(rel1)
if err != nil {
return err
}
} else if s.Types[i].Name == rel2.FromType {
found2 = true
err := s.Types[i].AddRel(rel2)
if err != nil {
return err
}
}
}
if found1 && found2 {
return nil
}
return fmt.Errorf(
"jsonapi: types %q and %q must exist",
rel1.FromType, rel2.FromType,
)
}
// Rels returns all the relationships from the schema's types. For two-way
// relationships (two types where each has a relationship pointing to the other
// type), only one of the two relationships will appear in the list.
func (s *Schema) Rels() []Rel {
s.buildRels()
rels := make([]Rel, 0, len(s.rels))
for _, rel := range s.rels {
rels = append(rels, rel)
}
sort.Slice(rels, func(i, j int) bool {
name1 := rels[i].FromType + rels[i].FromName
name2 := rels[j].FromType + rels[j].FromName
return name1 < name2
})
return rels
}
// HasType returns a boolean indicating whether a type has the specified name or
// not.
func (s *Schema) HasType(name string) bool {
for i := range s.Types {
if s.Types[i].Name == name {
return true
}
}
return false
}
// GetType returns the type associated with the speficied name.
//
// If no type with the given name is found, an zero instance of Type is
// returned. Therefore, checking whether the Name field is empty or not is a
// good way to dertermine whether the type was found or not.
func (s *Schema) GetType(name string) Type {
for _, typ := range s.Types {
if typ.Name == name {
return typ
}
}
return Type{}
}
// Check checks the integrity of all the relationships between the types and
// returns all the errors that were found.
func (s *Schema) Check() []error {
var (
errs = []error{}
)
// Check the inverse relationships
for _, typ := range s.Types {
// Relationships
for _, rel := range typ.Rels {
var targetType Type
// Does the relationship point to a type that exists?
if targetType = s.GetType(rel.ToType); targetType.Name == "" {
errs = append(errs, fmt.Errorf(
"jsonapi: field ToType of relationship %q of type %q does not exist",
rel.FromName,
typ.Name,
))
}
// Skip to next relationship here if there's no inverse
if rel.ToName == "" {
continue
}
// Is the inverse relationship type the same as its
// type name?
if rel.FromType != typ.Name {
errs = append(errs, fmt.Errorf(
"jsonapi: "+
"field FromType of relationship %q must be its type's name (%q, not %q)",
rel.FromName,
typ.Name,
rel.FromType,
))
} else {
// Do both relationships (current and inverse) point
// to each other?
var found bool
for _, invRel := range targetType.Rels {
if rel.FromName == invRel.ToName && rel.ToName == invRel.FromName {
found = true
}
}
if !found {
errs = append(errs, fmt.Errorf(
"jsonapi: "+
"relationship %q of type %q and its inverse do not point each other",
rel.FromName,
typ.Name,
))
}
}
}
}
return errs
}
// buildRels builds the set of normalized relationships that is returned by
// Schema.Rels.
func (s *Schema) buildRels() {
s.rels = map[string]Rel{}
for _, typ := range s.Types {
for _, rel := range typ.Rels {
relName := rel.String()
s.rels[relName] = rel.Normalize()
}
}
}