Skip to content

Commit bdd15aa

Browse files
committed
change to go modules
update README.md whole batch functions can error renaming stuff
1 parent 1232fe3 commit bdd15aa

File tree

15 files changed

+56
-153
lines changed

15 files changed

+56
-153
lines changed

.travis.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

Godeps/Godeps.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

Godeps/Readme

Lines changed: 0 additions & 5 deletions
This file was deleted.

Gopkg.lock

Lines changed: 0 additions & 33 deletions
This file was deleted.

Gopkg.toml

Lines changed: 0 additions & 34 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
This is an implementation of [Facebook's DataLoader](https://github.com/facebook/dataloader) in Golang.
66

77
## Install
8-
`go get -u github.com/graph-gophers/dataloader`
8+
`go get -u go.dribs.io/dataloader`
99

1010
## Usage
1111
```go
1212
// setup batch function
13-
batchFn := func(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
13+
batchFn := func(ctx context.Context, keys dataloader.Keys) ([]*dataloader.Result, error) {
1414
var results []*dataloader.Result
1515
// do some aync work to get data for specified keys
1616
// append to this list resolved values
17-
return results
17+
return results, nil
1818
}
1919

2020
// create Loader with an in-memory cache
@@ -36,9 +36,6 @@ if err != nil {
3636
log.Printf("value: %#v", result)
3737
```
3838

39-
### Don't need/want to use context?
40-
You're welcome to install the v1 version of this library.
41-
4239
## Cache
4340
This implementation contains a very basic cache that is intended only to be used for short lived DataLoaders (i.e. DataLoaders that ony exsist for the life of an http request). You may use your own implementation if you want.
4441

File renamed without changes.
File renamed without changes.

codecov.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

dataloader.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ type Interface interface {
2727
Prime(ctx context.Context, key Key, value interface{}) Interface
2828
}
2929

30-
// BatchFunc is a function, which when given a slice of keys (string), returns a slice of `results`.
30+
// BatchFunc is a function, which when given a slice of keys (string), returns an slice of `results`.
3131
// It's important that the length of the input keys matches the length of the output results.
3232
//
3333
// The keys passed to this function are guaranteed to be unique
34-
type BatchFunc func(context.Context, Keys) []*Result
34+
// Returning error saying the whole batch errored.
35+
type BatchFunc func(context.Context, Keys) ([]*Result, error)
3536

3637
// Result is the data structure that a BatchFunc returns.
3738
// It contains the resolved data, and any errors that may have occurred while fetching the data.
@@ -405,6 +406,7 @@ func (b *batcher) batch(originalContext context.Context) {
405406
keys = make(Keys, 0)
406407
reqs = make([]*batchRequest, 0)
407408
items = make([]*Result, 0)
409+
err error // indicating error for whole batch
408410
panicErr interface{}
409411
)
410412

@@ -429,7 +431,7 @@ func (b *batcher) batch(originalContext context.Context) {
429431
log.Printf("Dataloader: Panic received in batch function:: %v\n%s", panicErr, buf)
430432
}
431433
}()
432-
items = b.batchFn(ctx, keys)
434+
items, err = b.batchFn(ctx, keys)
433435
}()
434436

435437
if panicErr != nil {
@@ -440,6 +442,15 @@ func (b *batcher) batch(originalContext context.Context) {
440442
return
441443
}
442444

445+
if err != nil {
446+
err := &Result{Error: err}
447+
for _, req := range reqs {
448+
req.channel <- err
449+
close(req.channel)
450+
}
451+
return
452+
}
453+
443454
if len(items) != len(keys) {
444455
err := &Result{Error: fmt.Errorf(`
445456
The batch function supplied did not return an array of responses

example/lru-cache/golang-lru.go renamed to examples/lru-cache/using-lru-cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88

99
lru "github.com/hashicorp/golang-lru"
10-
"github.com/nicksrandall/dataloader"
10+
"go.dribs.io/dataloader"
1111
)
1212

1313
// Cache implements the dataloader.Cache interface
@@ -58,11 +58,11 @@ func main() {
5858
fmt.Printf("identity: %s\n", result)
5959
}
6060

61-
func batchFunc(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
61+
func batchFunc(_ context.Context, keys dataloader.Keys) ([]*dataloader.Result, error) {
6262
var results []*dataloader.Result
6363
// do some pretend work to resolve keys
6464
for _, key := range keys {
6565
results = append(results, &dataloader.Result{key.String(), nil})
6666
}
67-
return results
67+
return results, nil
6868
}

example/no-cache/no-cache.go renamed to examples/no-cache/using-no-cache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/nicksrandall/dataloader"
7+
"go.dribs.io/dataloader"
88
)
99

1010
func main() {
@@ -20,11 +20,11 @@ func main() {
2020
fmt.Printf("identity: %s\n", result)
2121
}
2222

23-
func batchFunc(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
23+
func batchFunc(_ context.Context, keys dataloader.Keys) ([]*dataloader.Result, error) {
2424
var results []*dataloader.Result
2525
// do some pretend work to resolve keys
2626
for _, key := range keys {
2727
results = append(results, &dataloader.Result{key.String(), nil})
2828
}
29-
return results
29+
return results, nil
3030
}

example/ttl-cache/go-cache.go renamed to examples/ttl-cache/using-ttl-cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"fmt"
88
"time"
99

10-
"github.com/nicksrandall/dataloader"
11-
cache "github.com/patrickmn/go-cache"
10+
"github.com/patrickmn/go-cache"
11+
"go.dribs.io/dataloader"
1212
)
1313

1414
// Cache implements the dataloader.Cache interface
@@ -59,11 +59,11 @@ func main() {
5959
fmt.Printf("identity: %s\n", result)
6060
}
6161

62-
func batchFunc(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
62+
func batchFunc(_ context.Context, keys dataloader.Keys) ([]*dataloader.Result, error) {
6363
var results []*dataloader.Result
6464
// do some pretend work to resolve keys
6565
for _, key := range keys {
6666
results = append(results, &dataloader.Result{key.String(), nil})
6767
}
68-
return results
68+
return results, nil
6969
}

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module go.dribs.io/dataloader
2+
3+
go 1.12
4+
5+
require (
6+
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad
7+
github.com/nicksrandall/dataloader v5.0.0+incompatible
8+
github.com/opentracing/opentracing-go v1.0.2
9+
github.com/patrickmn/go-cache v2.1.0+incompatible
10+
github.com/stretchr/testify v1.3.0 // indirect
11+
golang.org/x/net v0.0.0-20171129192339-a8b929477797 // indirect
12+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0PvIGS9TTtxg4R+JxuPGav82J8rA+GFnY7po=
4+
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
5+
github.com/nicksrandall/dataloader v5.0.0+incompatible h1:uX/GNCB35GQET9x6scOxL5mgoCL2FWU1uCX0YHwFz4k=
6+
github.com/nicksrandall/dataloader v5.0.0+incompatible/go.mod h1:ffw+n6o2gnZpT4lkSLF6W1+aDf6WIHZwWcwLF3/jJSo=
7+
github.com/opentracing/opentracing-go v1.0.2 h1:3jA2P6O1F9UOrWVpwrIo17pu01KWvNWg4X946/Y5Zwg=
8+
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
9+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
10+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
14+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
15+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
16+
golang.org/x/net v0.0.0-20171129192339-a8b929477797 h1:LwuzaILeZdnfjwbkFDc5ex0Us4o0k6PlbZuThgT8a68=
17+
golang.org/x/net v0.0.0-20171129192339-a8b929477797/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

0 commit comments

Comments
 (0)