| // Copyright 2026 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package entx |
| |
| import ( |
| "entgo.io/ent/dialect/sql" |
| |
| "go.chromium.org/infra/fleetconsole/internal/utils" |
| ) |
| |
| // RealmFilter returns a generic predicate that works on ANY Ent query. |
| // It assumes the underlying table has a column named "realm". |
| func RealmFilter(col string, realms []string) func(*sql.Selector) { |
| return func(s *sql.Selector) { |
| // If nil, apply no filter |
| if realms == nil { |
| return |
| } |
| |
| // s.C resolves to "table_name"."col_name" to avoid ambiguity |
| col := s.C(col) |
| |
| nilCheck := sql.IsNull(col) |
| if len(realms) == 0 { |
| s.Where(nilCheck) |
| return |
| } |
| |
| // Convert []string to []any for the sql builder |
| args := utils.Map(realms, func(realm string) any { return realm }) |
| |
| // (realm IS NULL) OR (realm IN (...)) |
| s.Where(sql.Or( |
| nilCheck, |
| sql.In(col, args...), |
| )) |
| } |
| } |