-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
35 lines (28 loc) · 873 Bytes
/
get.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
package get
import (
"errors"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch05/acme/internal/modules/data"
)
var (
// error thrown when the requested person is not in the database
errPersonNotFound = errors.New("person not found")
)
// Getter will attempt to load a person.
// It can return an error caused by the data layer or when the requested person is not found
type Getter struct {
}
// Do will perform the get
func (g *Getter) Do(ID int) (*data.Person, error) {
// load person from the data layer
person, err := loader(ID)
if err != nil {
if err == data.ErrNotFound {
// By converting the error we are hiding the implementation details from our users.
return nil, errPersonNotFound
}
return nil, err
}
return person, err
}
// this function as a variable allows us to Monkey Patch during testing
var loader = data.Load