PsychicPuppy
PsychicPuppy

Reputation: 156

OPA/REGO Using AND condition to combine two arrays of Boolean values

I whish to combine two arrays of Boolean value using AND.

For example: a1 := [true, true, false], a2 := [false, true, false].

the resulting AND operation:

a3 = a1 AND a2 would be [false, true, false]

Upvotes: 1

Views: 75

Answers (1)

SignalRichard
SignalRichard

Reputation: 1697

To get to the desired output, one would need to basically implement a Boolean "AND" function implementing each set of possible inputs as functions and return the result and build the resulting array using a comprehension:

policy.rego

package example

import rego.v1

a1 := [true, true, false]
a2 := [false, true, false]
a3 := [b | b := boolean_and(a1[i], a2[i])]

boolean_and(x, y) := z if {
    is_boolean(x)
    is_boolean(y)
    x
    y
    z := true
}

boolean_and(x, y) := z if {
    is_boolean(x)
    not x
    z := false
}

boolean_and(x, y) := z if {
    is_boolean(y)
    not y
    z := false
}

The resulting output would look like:

opa eval -d policy.rego "data.example"

{
  "result": [
    {
      "expressions": [
        {
          "value": {
            "a1": [
              true,
              true,
              false
            ],
            "a2": [
              false,
              true,
              false
            ],
            "a3": [
              false,
              true,
              false
            ]
          },
          "text": "data.example",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}

Upvotes: 0

Related Questions