-
Notifications
You must be signed in to change notification settings - Fork 12
/
collection.go
98 lines (84 loc) · 2.55 KB
/
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
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
package types
import (
"fmt"
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Collection is a set of items categorized by denom
type Collection struct {
Denom string `json:"denom,omitempty" yaml:"denom"`
Items Items `json:"items" yaml:"items"`
}
// NewCollection is a constructor function for Collection
func NewCollection(denom string, items Items) Collection {
return Collection{
Denom: strings.TrimSpace(denom),
Items: items,
}
}
// EmptyCollection returns an empty collection
func EmptyCollection() Collection {
return NewCollection("", NewItems())
}
// GetItem gets a Item from the collection
func (collection Collection) GetItem(id string) (item Item, err error) {
item, found := collection.Items.Find(id)
if found {
return item, nil
}
return Item{}, sdkerrors.Wrap(ErrUnknownItem,
fmt.Sprintf("Item #%s doesn't exist in collection %s", id, collection.Denom),
)
}
// ContainsItem returns whether or not a Collection contains an Item
func (collection Collection) ContainsItem(id string) bool {
_, err := collection.GetItem(id)
return err == nil
}
// AddItem adds an Item to the collection
func (collection Collection) AddItem(item Item) (Collection, error) {
id := item.GetID()
exists := collection.ContainsItem(id)
if exists {
return collection, sdkerrors.Wrap(ErrItemAlreadyExists,
fmt.Sprintf("Item #%s already exists in collection %s", id, collection.Denom),
)
}
collection.Items = collection.Items.Append(item)
return collection, nil
}
// UpdateItem updates an Item from a collection
func (collection Collection) UpdateItem(item Item) (Collection, error) {
items, ok := collection.Items.Update(item.GetID(), item)
if !ok {
return collection, sdkerrors.Wrap(ErrUnknownItem,
fmt.Sprintf("Item #%s doesn't exist on collection %s", item.GetID(), collection.Denom),
)
}
collection.Items = items
return collection, nil
}
// DeleteItem deletes an Item from a collection
func (collection Collection) DeleteItem(item Item) (Collection, error) {
items, ok := collection.Items.Remove(item.GetID())
if !ok {
return collection, sdkerrors.Wrap(ErrUnknownItem,
fmt.Sprintf("Item #%s doesn't exist on collection %s", item.GetID(), collection.Denom),
)
}
collection.Items = items
return collection, nil
}
// Supply gets the total supply of Items of a collection
func (collection Collection) Supply() int {
return len(collection.Items)
}
// String follows stringer interface
func (collection Collection) String() string {
return fmt.Sprintf(`Denom: %s
Items:
%s`,
collection.Denom,
collection.Items.String(),
)
}