mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #101590 from smarterclayton/quantity
quantity: Allow a new quantity to be created directly from inf.Dec
This commit is contained in:
commit
72b24d128b
@ -696,6 +696,15 @@ func (q *Quantity) UnmarshalJSON(value []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewDecimalQuantity returns a new Quantity representing the given
|
||||
// value in the given format.
|
||||
func NewDecimalQuantity(b inf.Dec, format Format) *Quantity {
|
||||
return &Quantity{
|
||||
d: infDecAmount{&b},
|
||||
Format: format,
|
||||
}
|
||||
}
|
||||
|
||||
// NewQuantity returns a new Quantity representing the given
|
||||
// value in the given format.
|
||||
func NewQuantity(value int64, format Format) *Quantity {
|
||||
|
@ -392,6 +392,16 @@ func TestQuantityParse(t *testing.T) {
|
||||
got.AsDec()
|
||||
}
|
||||
|
||||
for _, format := range []Format{DecimalSI, BinarySI, DecimalExponent} {
|
||||
// ensure we are not simply checking pointer equality by creating a new inf.Dec
|
||||
var copied inf.Dec
|
||||
copied.Add(inf.NewDec(0, inf.Scale(0)), got.AsDec())
|
||||
q := NewDecimalQuantity(copied, format)
|
||||
if c := q.Cmp(got); c != 0 {
|
||||
t.Errorf("%v: round trip from decimal back to quantity is not comparable: %d: %#v vs %#v", item.input, c, got, q)
|
||||
}
|
||||
}
|
||||
|
||||
// verify that we can decompose the input and get the same result by building up from the base.
|
||||
positive, _, num, denom, suffix, err := parseQuantityString(item.input)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user