mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 23:17:50 +00:00
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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user