mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-13 21:25:09 +00:00
Tolerate YAML and JSON numbers
Both YAML and JSON can contain numbers
This commit is contained in:
@@ -626,10 +626,7 @@ func (q *Quantity) UnmarshalJSON(value []byte) error {
|
|||||||
q.i = int64Amount{}
|
q.i = int64Amount{}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if l < 2 {
|
if l >= 2 && value[0] == '"' && value[l-1] == '"' {
|
||||||
return ErrFormatWrong
|
|
||||||
}
|
|
||||||
if value[0] == '"' && value[l-1] == '"' {
|
|
||||||
value = value[1 : l-1]
|
value = value[1 : l-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -826,11 +826,24 @@ func TestJSON(t *testing.T) {
|
|||||||
|
|
||||||
func TestJSONWhitespace(t *testing.T) {
|
func TestJSONWhitespace(t *testing.T) {
|
||||||
q := Quantity{}
|
q := Quantity{}
|
||||||
for _, s := range []string{" 1", "1 "} {
|
testCases := []struct {
|
||||||
if err := json.Unmarshal([]byte(`"`+s+`"`), &q); err != nil {
|
in string
|
||||||
t.Errorf("%q: %v", s, err)
|
expect string
|
||||||
|
}{
|
||||||
|
{`" 1"`, "1"},
|
||||||
|
{`"1 "`, "1"},
|
||||||
|
{`1`, "1"},
|
||||||
|
{` 1`, "1"},
|
||||||
|
{`1 `, "1"},
|
||||||
|
{`10`, "10"},
|
||||||
|
{`-1`, "-1"},
|
||||||
|
{` -1`, "-1"},
|
||||||
|
}
|
||||||
|
for _, test := range testCases {
|
||||||
|
if err := json.Unmarshal([]byte(test.in), &q); err != nil {
|
||||||
|
t.Errorf("%q: %v", test.in, err)
|
||||||
}
|
}
|
||||||
if q.String() != "1" {
|
if q.String() != test.expect {
|
||||||
t.Errorf("unexpected string: %q", q.String())
|
t.Errorf("unexpected string: %q", q.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user