Go boltDB query using bolthold 3 conditions

Issue

So I had this question before and I had an answer below the question which worked, but I just realized that the query I came out with does not work as planned.

Basically now if it works like this

(if the roleskey contains any of the roles in slice) and (if the tenantID is an empty string) or (if tenantIDKey is equal to tenantID)

But what I need is

(if the roleskey contains any of the roles in slice) AND (if the tenantID is an empty string OR if tenantIDKey is equal to tenantID)

Here is my current query:

query := bolthold.Where(roleskey).ContainsAny(bolthold.Slice(roles)...).And(tenantIDKey).Eq("").Or(bolthold.Where(tenantIDKey).Eq(tenantID))

Does anyone know how to solve this?

Solution

You just need to move the emptiness check inside the OR condition.

query := bolthold.
  Where(roleskey).
  ContainsAny(bolthold.Slice(roles)...).
  And(
    bolthold.
      Where(tenantIDKey).
      Eq(tenantID).
      Or(tenantIDKey).
      Eq("")
  )

Answered By – Vüsal

Answer Checked By – Mary Flores (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.