forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basket.go
47 lines (40 loc) · 904 Bytes
/
basket.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
package pricing
import (
"math/big"
"time"
)
// Basket represents a billing basket
type Basket []Usage
// NewBasket return a new instance of Basket
func NewBasket() *Basket {
return &Basket{}
}
// Add adds an Usage to a Basket
func (b *Basket) Add(usage Usage) error {
*b = append(*b, usage)
return nil
}
// Length returns the amount of Usages in a Basket
func (b *Basket) Length() int {
return len(*b)
}
// SetDuration sets the duration for each Usages of a Basket
func (b *Basket) SetDuration(duration time.Duration) error {
var err error
for i, usage := range *b {
err = usage.SetDuration(duration)
if err != nil {
return err
}
(*b)[i] = usage
}
return nil
}
// Total computes the Usage.Total() of all the Usages of a Basket
func (b *Basket) Total() *big.Rat {
total := new(big.Rat)
for _, usage := range *b {
total = total.Add(total, usage.Total())
}
return total
}