From e4405f9f17054651967a21d7ebd964927bcc60c8 Mon Sep 17 00:00:00 2001 From: Earncef Sequeira Date: Thu, 31 May 2018 15:57:16 +0200 Subject: [PATCH] Nil value returns empty string. --- value.go | 2 ++ value_test.go | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/value.go b/value.go index 8598f60..4e5f9b7 100644 --- a/value.go +++ b/value.go @@ -20,6 +20,8 @@ func (v *Value) Data() interface{} { // String returns the value always as a string func (v *Value) String() string { switch { + case v.IsNil(): + return "" case v.IsStr(): return v.Str() case v.IsBool(): diff --git a/value_test.go b/value_test.go index ab25e84..898a59a 100644 --- a/value_test.go +++ b/value_test.go @@ -67,10 +67,12 @@ func TestStringTypeFloat(t *testing.T) { func TestStringTypeOther(t *testing.T) { m := objx.Map{ - "other": []string{"foo", "bar"}, + "other": []string{"foo", "bar"}, + "nilValue": nil, } assert.Equal(t, "[]string{\"foo\", \"bar\"}", m.Get("other").String()) + assert.Equal(t, "", m.Get("nilValue").String()) } func TestStringSliceTypeString(t *testing.T) {