/
saver.go
53 lines (42 loc) · 1.11 KB
/
saver.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
41
42
43
44
45
46
47
48
49
50
51
52
53
package multistorage
import (
multierror "github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/thomaso-mirodin/go-shorten/storage"
)
// Saver are expected to process a slice of storages and return a result of SaveName(short, url string)
type Saver func(short string, url string, stores []storage.NamedStorage) error
func saveAllFunc(short string, url string, stores []storage.NamedStorage) error {
if len(stores) == 0 {
return ErrEmpty
}
errs := new(multierror.Error)
for _, store := range stores {
err := store.SaveName(short, url)
if err != nil {
multierror.Append(
errs,
errors.Wrapf(err, "failed to save %q to %q", short, store),
)
}
}
return errs.ErrorOrNil()
}
// saveOnlyOnceFunc
func saveOnlyOnceFunc(short string, url string, stores []storage.NamedStorage) error {
if len(stores) == 0 {
return ErrEmpty
}
errs := new(multierror.Error)
for _, store := range stores {
err := store.SaveName(short, url)
if err == nil {
return nil
}
multierror.Append(errs, err)
}
return errors.Wrap(
errs.ErrorOrNil(),
"failed to save to only one store",
)
}