Skip to content

Commit

Permalink
Migrate from resource.Lookup to query.Query (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
rs authored Aug 1, 2017
1 parent 2407f38 commit 25a468b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
23 changes: 14 additions & 9 deletions mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/schema/query"
)

// MemoryHandler is an example handler storing data in memory
Expand Down Expand Up @@ -153,7 +154,7 @@ func (m *MemoryHandler) Delete(ctx context.Context, item *resource.Item) (err er
}

// Clear clears all items from the memory store matching the lookup
func (m *MemoryHandler) Clear(ctx context.Context, lookup *resource.Lookup) (total int, err error) {
func (m *MemoryHandler) Clear(ctx context.Context, q *query.Query) (total int, err error) {
m.Lock()
defer m.Unlock()
err = handleWithLatency(m.Latency, ctx, func() error {
Expand All @@ -164,7 +165,7 @@ func (m *MemoryHandler) Clear(ctx context.Context, lookup *resource.Lookup) (tot
if err != nil {
return err
}
if !lookup.Filter().Match(item.Payload) {
if !q.Predicate.Match(item.Payload) {
continue
}
m.delete(item.ID)
Expand All @@ -176,7 +177,7 @@ func (m *MemoryHandler) Clear(ctx context.Context, lookup *resource.Lookup) (tot
}

// Find items from memory matching the provided lookup
func (m *MemoryHandler) Find(ctx context.Context, lookup *resource.Lookup, offset, limit int) (list *resource.ItemList, err error) {
func (m *MemoryHandler) Find(ctx context.Context, q *query.Query) (list *resource.ItemList, err error) {
m.RLock()
defer m.RUnlock()
err = handleWithLatency(m.Latency, ctx, func() error {
Expand All @@ -187,23 +188,27 @@ func (m *MemoryHandler) Find(ctx context.Context, lookup *resource.Lookup, offse
if err != nil {
return err
}
if !lookup.Filter().Match(item.Payload) {
if !q.Predicate.Match(item.Payload) {
continue
}
items = append(items, item)
}
// Apply sort
if len(lookup.Sort()) > 0 {
s := sortableItems{lookup.Sort(), items}
if len(q.Sort) > 0 {
s := sortableItems{q.Sort, items}
sort.Sort(s)
}
// Apply pagination
total := len(items)
if q.Window == nil {
list = &resource.ItemList{Total: total, Items: items}
return nil
}
end := total
start := offset
start := q.Window.Offset

if limit > 0 {
end = start + limit
if q.Window.Limit >= 0 {
end = start + q.Window.Limit
if end > total-1 {
end = total
}
Expand Down
15 changes: 8 additions & 7 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"time"

"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/schema/query"
)

// sortableItems is an item slice implementing sort.Interface
type sortableItems struct {
sort []string
sort query.Sort
items []*resource.Item
}

Expand All @@ -21,15 +22,15 @@ func (s sortableItems) Swap(i, j int) {
}

func (s sortableItems) Less(i, j int) bool {
for _, exp := range s.sort {
for _, field := range s.sort {
var field1 interface{}
var field2 interface{}
if exp[0] == '-' {
field1 = s.items[j].GetField(exp[1:])
field2 = s.items[i].GetField(exp[1:])
if field.Reversed {
field1 = s.items[j].GetField(field.Name)
field2 = s.items[i].GetField(field.Name)
} else {
field1 = s.items[i].GetField(exp)
field2 = s.items[j].GetField(exp)
field1 = s.items[i].GetField(field.Name)
field2 = s.items[j].GetField(field.Name)
}
if field1 == field2 {
continue
Expand Down

0 comments on commit 25a468b

Please sign in to comment.