-
Notifications
You must be signed in to change notification settings - Fork 8
/
wrapper_collection.go
51 lines (44 loc) · 1.26 KB
/
wrapper_collection.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
package jsonapi
// WrapCollection returns a *WrapperCollection which implements the Collection
// interface and holds resources of the type defined in r.
func WrapCollection(r Resource) *WrapperCollection {
return &WrapperCollection{
typ: r.GetType(),
col: []*Wrapper{},
sample: r,
}
}
// WrapperCollection is a Collection of resources of a certain type defined
// using the WrapCollection constructor.
//
// Only resources of that type can be added to the collection and the type may
// not be modified.
type WrapperCollection struct {
typ Type
col []*Wrapper
sample Resource
}
// GetType returns the type of the resources in the collection.
func (wc *WrapperCollection) GetType() Type {
return wc.typ
}
// Len returns the number of elements in the collection.
func (wc *WrapperCollection) Len() int {
return len(wc.col)
}
// At returns the resource at the given index.
//
// It returns nil if the index is greater than the number of resources in the
// collection.
func (wc *WrapperCollection) At(i int) Resource {
if len(wc.col) > i {
return wc.col[i]
}
return nil
}
// Add appends the given resource at the end of the collection.
func (wc *WrapperCollection) Add(r Resource) {
if wr, ok := r.(*Wrapper); ok {
wc.col = append(wc.col, wr)
}
}