-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.go
51 lines (42 loc) · 1.21 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package get
import (
"context"
"errors"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch08/acme/internal/logging"
"github.com/PacktPublishing/Hands-On-Dependency-Injection-in-Go/ch08/acme/internal/modules/data"
)
var (
// error thrown when the requested person is not in the database
errPersonNotFound = errors.New("person not found")
)
// NewGetter creates and initializes a Getter
func NewGetter(cfg Config) *Getter {
return &Getter{
cfg: cfg,
}
}
// Config is the configuration for Getter
type Config interface {
Logger() logging.Logger
DataDSN() string
}
// 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 {
cfg Config
}
// Do will perform the get
func (g *Getter) Do(ID int) (*data.Person, error) {
// load person from the data layer
person, err := loader(context.TODO(), g.cfg, 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