Skip to content

Commit

Permalink
Add support for list values for Eq() predicate.
Browse files Browse the repository at this point in the history
  • Loading branch information
jortel committed Oct 7, 2021
1 parent 858e8b9 commit dc63f1f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/inventory/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,17 @@ func TestList(t *testing.T) {
g.Expect(err).To(gomega.BeNil())
g.Expect(len(list)).To(gomega.Equal(1))
g.Expect(list[0].ID).To(gomega.Equal(0))
// List = (multiple).
list = []TestObject{}
err = DB.List(
&list,
ListOptions{
Predicate: Eq("ID", []int{2, 4}),
})
g.Expect(err).To(gomega.BeNil())
g.Expect(len(list)).To(gomega.Equal(2))
g.Expect(list[0].ID).To(gomega.Equal(2))
g.Expect(list[1].ID).To(gomega.Equal(4))
// List != AND
list = []TestObject{}
err = DB.List(
Expand Down
31 changes: 30 additions & 1 deletion pkg/inventory/model/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,36 @@ type EqPredicate struct {
//
// Build.
func (p *EqPredicate) Build(options *ListOptions) error {
return p.build("=", options)
f, found := p.match(options.fields)
if !found {
return liberr.Wrap(PredicateRefErr)
}
pv := reflect.ValueOf(p.Value)
switch pv.Kind() {
case reflect.Slice:
params := []string{}
for i := 0; i < pv.Len(); i++ {
v, err := f.AsValue(pv.Index(i).Interface())
if err != nil {
return err
}
params = append(
params,
options.Param(f.Name, v))
}
p.expr = strings.Join(
[]string{
f.Name,
"IN",
"(",
strings.Join(params, ","),
")"},
" ")
default:
return p.build("=", options)
}

return nil
}

//
Expand Down

0 comments on commit dc63f1f

Please sign in to comment.