-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstorage.go
40 lines (32 loc) · 867 Bytes
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package babyapi
import (
"context"
"errors"
"net/url"
)
var ErrNotFound = errors.New("resource not found")
// FilterFunc is used for GetAll to filter resources that are read from storage
type FilterFunc[T any] func(T) bool
func (f FilterFunc[T]) Filter(in []T) []T {
if f == nil {
return in
}
out := []T{}
for _, item := range in {
if f(item) {
out = append(out, item)
}
}
return out
}
// Storage defines how the API will interact with a storage backend
type Storage[T Resource] interface {
// Get a single resource by ID
Get(context.Context, string) (T, error)
// GetAll will return all resources that match the provided query filters
GetAll(context.Context, url.Values) ([]T, error)
// Set will save the provided resource
Set(context.Context, T) error
// Delete will delete a resource by ID
Delete(context.Context, string) error
}