Handle nil Amount in resource.Quantity.{Add,Sub}

Treat `nil` Amount as 0 in `resource.Quantity.Add` and
`resource.Quantity.Sub`. Also, allow adding/subtracting resources with
different Formats (since Format has no effect on the underlying value).
This commit is contained in:
Tim St. Clair
2015-12-10 19:24:23 -08:00
parent 1b70a403bb
commit b4116eb718
2 changed files with 61 additions and 4 deletions

View File

@@ -328,15 +328,28 @@ func (q *Quantity) Cmp(y Quantity) int {
}
func (q *Quantity) Add(y Quantity) error {
q.Amount.Add(q.Amount, y.Amount)
switch {
case y.Amount == nil:
// Adding 0: do nothing.
case q.Amount == nil:
q.Amount = &inf.Dec{}
return q.Add(y)
default:
q.Amount.Add(q.Amount, y.Amount)
}
return nil
}
func (q *Quantity) Sub(y Quantity) error {
if q.Format != y.Format {
return fmt.Errorf("format mismatch: %v vs. %v", q.Format, y.Format)
switch {
case y.Amount == nil:
// Subtracting 0: do nothing.
case q.Amount == nil:
q.Amount = &inf.Dec{}
return q.Sub(y)
default:
q.Amount.Sub(q.Amount, y.Amount)
}
q.Amount.Sub(q.Amount, y.Amount)
return nil
}