Search code examples
policyopen-policy-agentrego

Rego - assign array to existing array


I'm getting a weird behavior in Rego and I wonder why does it happen.

Link to Rego Playground

When I create an empty array, and than assign to it new array, the count of the first array is still zero:

package play

x[{"msg": msg}]{
    c := []
    a := [1,2]
    b := [3,4]
    c = array.concat(a,b)
    count(c) > 0
    msg := "Length of c is greater than zero"
}

And the output is:

    {
    "x": []
}

So, I have 2 questions:

  1. Why do I get false in the line count(c)> 0?

  2. How can I assign array to existing one? ( I need it because I have function that returns array and I'm trying to return the concatenation of 2 arrays. e.g.:

    func[{"msg": msg}] = c{ a := [1,2] b := [3,4] c = array.concat(a,b) }

Thanks!


Solution

  • Rego values and variables are immutable, so there's no way to assign a new value to an already existing variable. Your example compiles due to using the unification operator (=) rather than the assignment operator (:=).

    In the example you provided, simply remove the first assignment:

    package play
    
    x[{"msg": msg}]{
        a := [1,2]
        b := [3,4]
        c := array.concat(a,b)
        count(c) > 0
        msg := "Length of c is greater than zero"
    }