func FromRegoExpression()

in sg/internal/result/result.go [45:79]


func FromRegoExpression(
	query string,
	expression *rego.ExpressionValue,
) ([]Result, error) {
	// Rego rules that are intended for evaluation should return a slice of values.
	// For example, deny[msg] or violation[{"msg": msg}].
	//
	// When an expression does not have a slice of values, the expression did not
	// evaluate to true, and no message was returned.
	var expressionValues []interface{}
	if v, ok := expression.Value.([]interface{}); ok {
		expressionValues = v
	}
	if len(expressionValues) == 0 {
		return []Result{empty(query)}, nil
	}

	var rv []Result
	for _, v := range expressionValues {
		switch val := v.(type) {
		// Policies that only return a single string (e.g. deny[msg])
		case string:
			rv = append(rv, fromString(query, val))
			// Policies that return metadata (e.g. deny[{"msg": msg}])
		case map[string]interface{}:
			v, err := fromMetadata(query, val)
			if err != nil {
				return nil, fmt.Errorf("failed to load from metadata: %w", err)
			}
			rv = append(rv, v)
		}
	}

	return rv, nil
}