-
Notifications
You must be signed in to change notification settings - Fork 13
/
service.go
57 lines (44 loc) · 1.07 KB
/
service.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
54
55
56
57
package service
import (
"errors"
"fmt"
"github.com/owncloud/ocis/ocis-pkg/log"
)
const (
// DefaultPhrase defines the default phrase
DefaultPhrase = "Hello %s"
)
var (
ErrMissingName = errors.New("name missing")
)
type Greeter interface {
Greet(accountID, name string) (greeting string)
}
type GreetingPhraseSource interface {
GetPhrase(accountID string) (phrase string)
}
type StaticPhraseSource struct {
Phrase string
}
func (s StaticPhraseSource) GetPhrase(accountID string) string {
return s.Phrase
}
// New returns a new instance of Service
func NewGreeter(opts ...Option) (Greeter, error) {
options := newOptions(opts...)
g := BasicGreeter{
log: options.Logger,
phraseSource: options.PhraseSource,
}
return g, nil
}
// BasicGreeter implements the Greeter interface
type BasicGreeter struct {
log log.Logger
phraseSource GreetingPhraseSource
}
// Greet implements the HelloHandler interface.
func (g BasicGreeter) Greet(accountID, name string) string {
phrase := g.phraseSource.GetPhrase(accountID)
return fmt.Sprintf(phrase, name)
}