From 589224e6401cc9d072336469d621396eb84f300b Mon Sep 17 00:00:00 2001 From: Tareq Al-Maamari Date: Wed, 27 Nov 2019 21:32:45 +0100 Subject: [PATCH] Getting values from an empty yamls should not fail When getting a value from an empty yaml, GetType tries to get the kind of nil which makes it fail, this commit covers the nil case in GetType. --- assets/examples/empty.yml | 1 + pkg/v1/ytbx/common.go | 3 ++- pkg/v1/ytbx/getting_test.go | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 assets/examples/empty.yml diff --git a/assets/examples/empty.yml b/assets/examples/empty.yml new file mode 100644 index 0000000..73b314f --- /dev/null +++ b/assets/examples/empty.yml @@ -0,0 +1 @@ +--- \ No newline at end of file diff --git a/pkg/v1/ytbx/common.go b/pkg/v1/ytbx/common.go index e07d478..c1b6bc3 100644 --- a/pkg/v1/ytbx/common.go +++ b/pkg/v1/ytbx/common.go @@ -37,7 +37,8 @@ const ( // GetType returns the type of the input value with a YAML specific view func GetType(value interface{}) string { switch tobj := value.(type) { - case yaml.MapSlice: + + case yaml.MapSlice, nil: return typeMap case []interface{}: diff --git a/pkg/v1/ytbx/getting_test.go b/pkg/v1/ytbx/getting_test.go index 3911f59..c3acc72 100644 --- a/pkg/v1/ytbx/getting_test.go +++ b/pkg/v1/ytbx/getting_test.go @@ -59,4 +59,10 @@ var _ = Describe("getting stuff test cases", func() { Expect(grabError(example, "/yaml/named-entry-list-using-id/id=0")).To(BeEquivalentTo("there is no entry id=0 in the list")) }) }) + Context("Trying to get values by path in an empty file", func() { + It("should return a not found key error", func() { + emptyFile := yml("../../../assets/examples/empty.yml") + Expect(grabError(emptyFile, "does-not-exist")).To(BeEquivalentTo("no key 'does-not-exist' found in map, available keys: ")) + }) + }) })