Skip to content

Commit

Permalink
Added slice criteria
Browse files Browse the repository at this point in the history
```Go
val := struct {
    Set []string
}{
    Set: []string{"1", "2", "3"},
}
bh.Where("Set").Contains("1") // true
bh.Where("Set").ContainsAll("1", "3") // true
bh.Where("Set").ContainsAll("1", "3", "4") // false
bh.Where("Set").ContainsAny("1", "7", "4") // true
```

Add `bolthold.Slice(val)` helper for turning slices of arbitrary types
to `[]interface{}`.
  • Loading branch information
timshannon committed Jul 14, 2019
1 parent aa134f7 commit ff57bec
Show file tree
Hide file tree
Showing 4 changed files with 766 additions and 552 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Fields must be exported, and thus always need to start with an upper-case letter
* Reverse - `Where("field").Eq(value).SortBy("field").Reverse()`
* Index - `Where("field").Eq(value).Index("indexName")`
* Not - `Where("field").Not().In(val1, val2, val3)`
* Contains - `Where("field").Contains(val1)`
* ContainsAll - `Where("field").Contains(val1, val2, val3)`
* ContainsAny - `Where("field").Contains(val1, val2, val3)`


If you want to run a query's criteria against the Key value, you can use the `bolthold.Key` constant:
Expand Down Expand Up @@ -185,6 +188,52 @@ err := store.Insert(bolthold.NextSequence(), &data)
```


### Slices in Structs and Queries
When querying slice fields in structs you can use the `Contains`, `ContainsAll` and `ContainsAny` criterion.

```Go
val := struct {
Set []string
}{
Set: []string{"1", "2", "3"},
}
bh.Where("Set").Contains("1") // true
bh.Where("Set").ContainsAll("1", "3") // true
bh.Where("Set").ContainsAll("1", "3", "4") // false
bh.Where("Set").ContainsAny("1", "7", "4") // true
```

The `In`, `ContainsAll` and `ContainsAny` critierion accept a slice of `interface{}` values. This means you can build
your queries by passing in your values as arguments:
```
where := bolthold.Where("Id").In("1", "2", "3")
```

However if you have an existing slice of values to test against, you can't pass in that slice because it is not of type
`[]interface{}`.

```Go
t := []string{"1", "2", "3", "4"}
where := bolthold.Where("Id").In(t...) // compile error
```

Instead you need to copy your slice into another slice of empty interfaces:
```Go
t := []string{"1", "2", "3", "4"}
s := make([]interface{}, len(t))
for i, v := range t {
s[i] = v
}
where := bolthold.Where("Id").In(s...)
```

You can use the helper function `bh.Slice` which does exactly that.
```Go
t := []string{"1", "2", "3", "4"}
where := bolthold.Where("Id").In(bh.Slice(t)...)

```

### Aggregate Queries

Aggregate queries are queries that group results by a field. For example, lets say you had a collection of employees:
Expand Down

0 comments on commit ff57bec

Please sign in to comment.