Tolerate YAML and JSON numbers

Both YAML and JSON can contain numbers
This commit is contained in:
Clayton Coleman 2016-06-06 17:14:35 -04:00
parent 2ed3246631
commit 653ddbb01e
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
2 changed files with 18 additions and 8 deletions

View File

@ -626,10 +626,7 @@ func (q *Quantity) UnmarshalJSON(value []byte) error {
q.i = int64Amount{}
return nil
}
if l < 2 {
return ErrFormatWrong
}
if value[0] == '"' && value[l-1] == '"' {
if l >= 2 && value[0] == '"' && value[l-1] == '"' {
value = value[1 : l-1]
}

View File

@ -826,11 +826,24 @@ func TestJSON(t *testing.T) {
func TestJSONWhitespace(t *testing.T) {
q := Quantity{}
for _, s := range []string{" 1", "1 "} {
if err := json.Unmarshal([]byte(`"`+s+`"`), &q); err != nil {
t.Errorf("%q: %v", s, err)
testCases := []struct {
in string
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())
}
}