-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
47 lines (38 loc) · 1017 Bytes
/
list.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
package list
import (
"errors"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch04/acme/internal/modules/data"
)
var (
// error thrown when there are no people in the database
errPeopleNotFound = errors.New("no people found")
)
// Lister will attempt to load all people in the database.
// It can return an error caused by the data layer
type Lister struct {
}
// Do will load the people from the data layer
func (l *Lister) Do() ([]*data.Person, error) {
// load all people
people, err := l.load()
if err != nil {
return nil, err
}
if len(people) == 0 {
// special processing for 0 people returned
return nil, errPeopleNotFound
}
return people, nil
}
// load all people
func (l *Lister) load() ([]*data.Person, error) {
people, err := data.LoadAll()
if err != nil {
if err == data.ErrNotFound {
// By converting the error we are encapsulating the implementation details from our users.
return nil, errPeopleNotFound
}
return nil, err
}
return people, nil
}