diff --git a/internal/factsengine/mapper.go b/internal/factsengine/mapper.go index fb8935d9..ac0fec6b 100644 --- a/internal/factsengine/mapper.go +++ b/internal/factsengine/mapper.go @@ -2,6 +2,8 @@ package factsengine import ( + "strconv" + "github.com/google/uuid" "github.com/pkg/errors" "github.com/trento-project/agent/internal/factsengine/entities" @@ -58,13 +60,7 @@ func factGatheredItemToEvent(fact entities.Fact) *events.Fact { } else { switch value := fact.Value.(type) { case string: - eventFact = &events.Fact{ - CheckId: fact.CheckID, - Name: fact.Name, - Value: &events.Fact_TextValue{ - TextValue: value, - }, - } + eventFact = parseString(value, fact) case int: eventFact = &events.Fact{ CheckId: fact.CheckID, @@ -102,6 +98,26 @@ func factGatheredItemToEvent(fact entities.Fact) *events.Fact { return eventFact } +func parseString(value string, fact entities.Fact) *events.Fact { + if floatValue, err := strconv.ParseFloat(value, 32); err == nil { + return &events.Fact{ + CheckId: fact.CheckID, + Name: fact.Name, + Value: &events.Fact_NumericValue{ + NumericValue: float32(floatValue), + }, + } + } + + return &events.Fact{ + CheckId: fact.CheckID, + Name: fact.Name, + Value: &events.Fact_TextValue{ + TextValue: value, + }, + } +} + func FactsGatheredToEvent(gatheredFacts entities.FactsGathered) ([]byte, error) { facts := []*events.Fact{} for _, fact := range gatheredFacts.FactsGathered { diff --git a/internal/factsengine/mapper_test.go b/internal/factsengine/mapper_test.go index 12dd10a3..df60326a 100644 --- a/internal/factsengine/mapper_test.go +++ b/internal/factsengine/mapper_test.go @@ -34,7 +34,12 @@ func (suite *MapperTestSuite) TestFactsGatheredToEvent() { }, { Name: "dummy2", - Value: "2", + Value: "result", + CheckID: "check1", + }, + { + Name: "dummy3", + Value: 2, CheckID: "check1", }, }, @@ -53,15 +58,22 @@ func (suite *MapperTestSuite) TestFactsGatheredToEvent() { FactsGathered: []*events.Fact{ { Name: "dummy1", - Value: &events.Fact_TextValue{ - TextValue: "1", + Value: &events.Fact_NumericValue{ + NumericValue: float32(1), }, CheckId: "check1", }, { Name: "dummy2", Value: &events.Fact_TextValue{ - TextValue: "2", + TextValue: "result", + }, + CheckId: "check1", + }, + { + Name: "dummy3", + Value: &events.Fact_NumericValue{ + NumericValue: float32(2), }, CheckId: "check1", }, @@ -92,7 +104,7 @@ func (suite *MapperTestSuite) TestFactsGatheredWithErrorToEvent() { }, { Name: "dummy2", - Value: "2", + Value: "result", CheckID: "check1", }, }, @@ -122,7 +134,7 @@ func (suite *MapperTestSuite) TestFactsGatheredWithErrorToEvent() { { Name: "dummy2", Value: &events.Fact_TextValue{ - TextValue: "2", + TextValue: "result", }, CheckId: "check1", }, diff --git a/internal/factsengine/policy_test.go b/internal/factsengine/policy_test.go index 1454c1ea..e0be6b4b 100644 --- a/internal/factsengine/policy_test.go +++ b/internal/factsengine/policy_test.go @@ -109,14 +109,14 @@ func (suite *PolicyTestSuite) TestPolicyPublishFacts() { { Name: "dummy1", Value: &events.Fact_TextValue{ - TextValue: "1", + TextValue: "result1", }, CheckId: "check1", }, { Name: "dummy2", Value: &events.Fact_TextValue{ - TextValue: "2", + TextValue: "result2", }, CheckId: "check1", }, @@ -136,12 +136,12 @@ func (suite *PolicyTestSuite) TestPolicyPublishFacts() { FactsGathered: []entities.Fact{ { Name: "dummy1", - Value: "1", + Value: "result1", CheckID: "check1", }, { Name: "dummy2", - Value: "2", + Value: "result2", CheckID: "check1", }, },