From 7a4b08b9d0005a467dd45414123fc90ad5789d3a Mon Sep 17 00:00:00 2001 From: Xabier Arbulu Insausti Date: Mon, 24 Apr 2023 17:29:11 +0200 Subject: [PATCH] Fix getValue function when map is empty (#218) --- pkg/factsengine/entities/fact_value.go | 2 +- pkg/factsengine/entities/fact_value_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/factsengine/entities/fact_value.go b/pkg/factsengine/entities/fact_value.go index a346ebfc..4d429aab 100644 --- a/pkg/factsengine/entities/fact_value.go +++ b/pkg/factsengine/entities/fact_value.go @@ -193,7 +193,7 @@ func getValue(fact FactValue, values []string) (FactValue, error) { } return getValue(value.Value[listIndex], values[1:]) default: - return value, nil + return nil, fmt.Errorf("requested field value not found") } } diff --git a/pkg/factsengine/entities/fact_value_test.go b/pkg/factsengine/entities/fact_value_test.go index 65bfaa9c..a936f46d 100644 --- a/pkg/factsengine/entities/fact_value_test.go +++ b/pkg/factsengine/entities/fact_value_test.go @@ -182,6 +182,7 @@ func (suite *FactValueTestSuite) TestFactValueMapGetValue() { "value": &entities.FactValueString{Value: "other_value"}, }, }, + "empty_entry": &entities.FactValueString{}, }, } @@ -252,6 +253,15 @@ func (suite *FactValueTestSuite) TestFactValueMapGetValue() { Message: "error getting value: requested field value not found: map_value.other_value", }, }, + { + description: "Should return ValueNotFoundError when value does not exist because the entry is empty", + key: "empty_entry.some_value", + expected: nil, + err: &entities.FactGatheringError{ + Type: "value-not-found", + Message: "error getting value: requested field value not found: empty_entry.some_value", + }, + }, { description: "Should return ValueNotFoundError when given list index does not exist", key: "list_value.3",