github.com/scaleway/scaleway-cli@v1.11.1/pkg/pricing/basket.go (about)

     1  package pricing
     2  
     3  import (
     4  	"math/big"
     5  	"time"
     6  )
     7  
     8  // Basket represents a billing basket
     9  type Basket []Usage
    10  
    11  // NewBasket return a new instance of Basket
    12  func NewBasket() *Basket {
    13  	return &Basket{}
    14  }
    15  
    16  // Add adds an Usage to a Basket
    17  func (b *Basket) Add(usage Usage) error {
    18  	*b = append(*b, usage)
    19  	return nil
    20  }
    21  
    22  // Length returns the amount of Usages in a Basket
    23  func (b *Basket) Length() int {
    24  	return len(*b)
    25  }
    26  
    27  // SetDuration sets the duration for each Usages of a Basket
    28  func (b *Basket) SetDuration(duration time.Duration) error {
    29  	var err error
    30  	for i, usage := range *b {
    31  		err = usage.SetDuration(duration)
    32  		if err != nil {
    33  			return err
    34  		}
    35  		(*b)[i] = usage
    36  	}
    37  	return nil
    38  }
    39  
    40  // Total computes the Usage.Total() of all the Usages of a Basket
    41  func (b *Basket) Total() *big.Rat {
    42  	total := new(big.Rat)
    43  	for _, usage := range *b {
    44  		total = total.Add(total, usage.Total())
    45  	}
    46  	return total
    47  }