Skip to content

Commit

Permalink
Getting values from an empty yamls should not fail
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tareqmamari authored and HeavyWombat committed Dec 6, 2019
1 parent d04d1bb commit 589224e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions assets/examples/empty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
3 changes: 2 additions & 1 deletion pkg/v1/ytbx/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}:
Expand Down
6 changes: 6 additions & 0 deletions pkg/v1/ytbx/getting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "))
})
})
})

0 comments on commit 589224e

Please sign in to comment.