Skip to content

Commit

Permalink
updated comment, check for existence, updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
pflueder authored and 256dpi committed Apr 5, 2024
1 parent da934d8 commit 5d6e1ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
20 changes: 16 additions & 4 deletions roast/matrix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package roast

import "reflect"
import (
"reflect"

"github.com/256dpi/fire/stick"
)

// Query represents an item query.
type Query func(Item) bool
Expand Down Expand Up @@ -88,7 +92,7 @@ func (m *Matrix) Generate(name string, values []any, fn func(value any, item Ite
base := m.items[m.names[len(m.names)-1]]

// prepare new items
newItems := make([]Item, 0, len(base)+len(values))
newItems := make([]Item, 0, len(base)*len(values))

// generate items
for _, item := range base {
Expand All @@ -110,17 +114,25 @@ func (m *Matrix) Generate(name string, values []any, fn func(value any, item Ite
}

// Items will return a dimension's items that match at least on of the specified
// queries. If no queries are specified the full list is returned. Queries
// are parsed using the Common Expression Language (CEL).
// queries. If no queries are specified the full list is returned.
func (m *Matrix) Items(name string, queries ...Query) []Item {
// check existence
if !stick.Contains(m.names, name) {
panic("roast: unknown dimension")
}

// check queries
if len(queries) == 0 {
return m.items[name]
}

// collect items
var items []Item
for _, item := range m.items[name] {
if item.Match(queries...) {
items = append(items, item)
}
}

return items
}
4 changes: 4 additions & 0 deletions roast/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ func TestMatrix(t *testing.T) {
assert.True(t, item.Is("bar", 2))
assert.Equal(t, 2, item["bar"])
}

assert.Panics(t, func() {
matrix.Items("qux")
})
}

0 comments on commit 5d6e1ae

Please sign in to comment.