-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
49 lines (39 loc) · 1.01 KB
/
item.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
package gopster
import (
"errors"
"fmt"
"image"
)
// ErrorItem is returned when a chart item is misconfigured.
var ErrorItem = errors.New("gopster: chart item is misconfigured")
type chartItem struct {
title string
creator string
coverImage image.Image
}
// AddItem adds an item to a chart.
func (c *Chart) AddItem(title string, creator string, img image.Image) error {
if c.items == nil {
c.items = make([]*chartItem, 0)
}
if len(c.items) == c.width*c.height {
return fmt.Errorf("%w: maximum number of items have been added", ErrorItem)
}
if title == "" || creator == "" || img == nil {
return fmt.Errorf("%w: missing title/creator/image", ErrorItem)
}
item := &chartItem{
title: title,
creator: creator,
coverImage: img,
}
c.items = append(c.items, item)
return nil
}
// MustAddItem is the same as AddItem, except it will panic on error.
func (c *Chart) MustAddItem(title, creator string, img image.Image) {
err := c.AddItem(title, creator, img)
if err != nil {
panic(err)
}
}