Skip to content

Commit 526ec43

Browse files
authored
Merge pull request #46 from j-fuentes/fix-unmarshalling-from-rego
Change how results are unmarshaled from rego
2 parents 7171fdd + 3e463b0 commit 526ec43

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

pkg/results/results.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,28 @@ func NewResultCollectionFromRegoResultSet(rs *rego.ResultSet) (*ResultCollection
129129
for _, k := range keys {
130130
var violations []string
131131
boolValid, boolOk := values[k].(bool)
132-
strings, stringsOk := values[k].([]string)
132+
sliceValid, sliceOk := values[k].([]interface{})
133133
if boolOk {
134134
log.Printf("Using a boolean for `Value` is deprecated, found for key: (%s)", k)
135135
if boolValid {
136136
violations = []string{}
137137
} else {
138138
violations = []string{"missing violation context in rule definition"}
139139
}
140-
} else if stringsOk {
141-
violations = strings
140+
} else if sliceOk {
141+
violations = make([]string, len(sliceValid))
142+
for idx, e := range sliceValid {
143+
violation, ok := e.(string)
144+
if !ok {
145+
// worst case scenario, use string representation of the variable
146+
violation = fmt.Sprintf("%+v", e)
147+
}
148+
violations[idx] = violation
149+
}
142150
} else {
143151
return nil, fmt.Errorf("format error, cannot unmarshall value '%+v' to bool or []string", values[k])
144152
}
153+
145154
rc = append(rc, &Result{
146155
ID: k,
147156
Value: violations,

pkg/results/results_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ func TestNewResultCollectionFromRegoResultSet(t *testing.T) {
136136
regoResultSet := &rego.ResultSet{
137137
rego.Result{Expressions: []*rego.ExpressionValue{&rego.ExpressionValue{
138138
Value: map[string]interface{}{
139-
"_1_4_3": []string{},
140-
"_1_4_4": []string{"violation"},
141-
"_1_4_5": []string{},
142-
"_1_4_6": []string{},
143-
"node_pools_with_legacy_endpoints_enabled": []string{},
144-
"node_pools_without_cloud_platform_scope": []string{"violation"},
139+
"_1_4_3": []interface{}{},
140+
"_1_4_4": []interface{}{"violation"},
141+
"_1_4_5": []interface{}{},
142+
"_1_4_6": []interface{}{},
143+
"node_pools_with_legacy_endpoints_enabled": []interface{}{},
144+
"node_pools_without_cloud_platform_scope": []interface{}{"violation"},
145+
"something_returning_a_map": []interface{}{map[string]string{"bar": "foo"}},
145146
},
146147
Text: "data.package.name",
147148
}}},
@@ -154,6 +155,7 @@ func TestNewResultCollectionFromRegoResultSet(t *testing.T) {
154155
&Result{ID: "_1_4_6", Value: []string{}, Violations: []string{}, Package: "package.name"},
155156
&Result{ID: "node_pools_with_legacy_endpoints_enabled", Value: []string{}, Violations: []string{}, Package: "package.name"},
156157
&Result{ID: "node_pools_without_cloud_platform_scope", Value: []string{"violation"}, Violations: []string{"violation"}, Package: "package.name"},
158+
&Result{ID: "something_returning_a_map", Value: []string{fmt.Sprintf("%+v", map[string]string{"bar": "foo"})}, Violations: []string{fmt.Sprintf("%+v", map[string]string{"bar": "foo"})}, Package: "package.name"},
157159
}
158160

159161
rc, err := NewResultCollectionFromRegoResultSet(regoResultSet)

0 commit comments

Comments
 (0)