Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias committed Sep 18, 2024
1 parent be38dd9 commit d4d7312
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 67 deletions.
8 changes: 5 additions & 3 deletions internal/engine/eval/jq/jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func NewJQEvaluator(assertions []*pb.RuleType_Definition_Eval_JQComparison) (*Ev

for idx := range assertions {
a := assertions[idx]
if a.Profile == nil && a.Constant == nil {
if a.Profile != nil && a.Constant != nil {
return nil, fmt.Errorf("profile and constant accessors are mutually exclusive")
} else if a.Profile == nil && a.Constant == nil {
return nil, fmt.Errorf("missing profile or constant accessor")
}

Expand Down Expand Up @@ -99,7 +101,7 @@ func (jqe *Evaluator) Eval(ctx context.Context, pol map[string]any, res *engif.R
}

// Deep compare
if !reflect.DeepEqual(numberToFloat64(expectedVal), numberToFloat64(dataVal)) {
if !reflect.DeepEqual(standardizeNumbers(expectedVal), standardizeNumbers(dataVal)) {
msg := fmt.Sprintf("data does not match profile: for assertion %d, got %v, want %v",
idx, dataVal, expectedVal)

Expand All @@ -116,7 +118,7 @@ func (jqe *Evaluator) Eval(ctx context.Context, pol map[string]any, res *engif.R
}

// Convert numeric types to float64
func numberToFloat64(v any) any {
func standardizeNumbers(v any) any {
switch v := v.(type) {
case int:
return float64(v)
Expand Down
80 changes: 16 additions & 64 deletions internal/engine/eval/jq/jq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ func TestNewJQEvaluatorValid(t *testing.T) {
},
},
{
Constant: &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: "b",
},
},
Constant: structpb.NewStringValue("b"),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".b",
},
Expand Down Expand Up @@ -237,11 +233,7 @@ func TestValidJQEvals(t *testing.T) {
name: "valid single rule evaluates constant string",
assertions: []*pb.RuleType_Definition_Eval_JQComparison{
{
Constant: &structpb.Value{
Kind: &structpb.Value_StringValue{
StringValue: "simple",
},
},
Constant: structpb.NewStringValue("simple"),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".simple",
},
Expand Down Expand Up @@ -279,11 +271,7 @@ func TestValidJQEvals(t *testing.T) {
name: "valid single rule evaluates constant int",
assertions: []*pb.RuleType_Definition_Eval_JQComparison{
{
Constant: &structpb.Value{
Kind: &structpb.Value_NumberValue{
NumberValue: 1,
},
},
Constant: structpb.NewNumberValue(1),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".simple",
},
Expand Down Expand Up @@ -321,11 +309,7 @@ func TestValidJQEvals(t *testing.T) {
name: "valid single rule evaluates constant bool",
assertions: []*pb.RuleType_Definition_Eval_JQComparison{
{
Constant: &structpb.Value{
Kind: &structpb.Value_BoolValue{
BoolValue: false,
},
},
Constant: structpb.NewBoolValue(false),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".simple",
},
Expand Down Expand Up @@ -363,29 +347,13 @@ func TestValidJQEvals(t *testing.T) {
name: "valid single rule evaluates constant array",
assertions: []*pb.RuleType_Definition_Eval_JQComparison{
{
Constant: &structpb.Value{
Kind: &structpb.Value_ListValue{
ListValue: &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "a",
},
},
{
Kind: &structpb.Value_StringValue{
StringValue: "b",
},
},
{
Kind: &structpb.Value_StringValue{
StringValue: "c",
},
},
},
},
Constant: structpb.NewListValue(&structpb.ListValue{
Values: []*structpb.Value{
structpb.NewStringValue("a"),
structpb.NewStringValue("b"),
structpb.NewStringValue("c"),
},
},
}),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".simple",
},
Expand Down Expand Up @@ -557,11 +525,7 @@ func TestValidJQEvalsFailed(t *testing.T) {
name: "constant value doesn't match",
assertions: []*pb.RuleType_Definition_Eval_JQComparison{
{
Constant: &structpb.Value{
Kind: &structpb.Value_BoolValue{
BoolValue: true,
},
},
Constant: structpb.NewBoolValue(true),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".simple",
},
Expand All @@ -578,11 +542,7 @@ func TestValidJQEvalsFailed(t *testing.T) {
name: "constant type doesn't match",
assertions: []*pb.RuleType_Definition_Eval_JQComparison{
{
Constant: &structpb.Value{
Kind: &structpb.Value_BoolValue{
BoolValue: false,
},
},
Constant: structpb.NewBoolValue(false),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".simple",
},
Expand All @@ -599,19 +559,11 @@ func TestValidJQEvalsFailed(t *testing.T) {
name: "constant array doesn't match",
assertions: []*pb.RuleType_Definition_Eval_JQComparison{
{
Constant: &structpb.Value{
Kind: &structpb.Value_ListValue{
ListValue: &structpb.ListValue{
Values: []*structpb.Value{
{
Kind: &structpb.Value_StringValue{
StringValue: "a",
},
},
},
},
Constant: structpb.NewListValue(&structpb.ListValue{
Values: []*structpb.Value{
structpb.NewStringValue("a"),
},
},
}),
Ingested: &pb.RuleType_Definition_Eval_JQComparison_Operator{
Def: ".simple",
},
Expand Down

0 comments on commit d4d7312

Please sign in to comment.