From c19d30414cb68e1469e7c54e8517f7ce71f1167f Mon Sep 17 00:00:00 2001 From: Inteon <42113979+inteon@users.noreply.github.com> Date: Sat, 11 Sep 2021 16:51:10 +0200 Subject: [PATCH] fix bug in numberTyping Signed-off-by: Inteon <42113979+inteon@users.noreply.github.com> --- yaml.go | 4 ++-- yaml_test.go | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/yaml.go b/yaml.go index 801dbe9..90d049d 100644 --- a/yaml.go +++ b/yaml.go @@ -342,7 +342,7 @@ cast_type: case uint64: s = "xd" + strconv.FormatUint(typedVal, 10) case string: - if typedVal[0] == 'x' { + if len(typedVal) > 0 && typedVal[0] == 'x' { s = "x" + typedVal } } @@ -367,7 +367,7 @@ func convertNestedJSONNumbers(jsonObj interface{}) (interface{}, error) { typedJSONObj[i], err = convertNestedJSONNumbers(value) } case string: - if typedJSONObj[0] == 'x' { + if len(typedJSONObj) > 0 && typedJSONObj[0] == 'x' { switch typedJSONObj[1] { case 'a': var val int64 diff --git a/yaml_test.go b/yaml_test.go index 756dc72..238d9cd 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -242,6 +242,14 @@ func TestUnmarshal(t *testing.T) { "b": float64(333), }, }, + { + encoded: []byte("a: \"\"\nb: \n"), + decodeInto: new(interface{}), + decoded: map[string]interface{}{ + "a": "", + "b": nil, + }, + }, } for _, test := range tests {