13 | 13 |
|
14 | 14 |
// StringService provides operations on strings.
|
15 | 15 |
type StringService interface {
|
16 | |
Uppercase(context.Context, string) (string, error)
|
17 | |
Count(context.Context, string) int
|
|
16 |
Uppercase(string) (string, error)
|
|
17 |
Count(string) int
|
18 | 18 |
}
|
19 | 19 |
|
20 | 20 |
// stringService is a concrete implementation of StringService
|
21 | 21 |
type stringService struct{}
|
22 | 22 |
|
23 | |
func (stringService) Uppercase(_ context.Context, s string) (string, error) {
|
|
23 |
func (stringService) Uppercase(s string) (string, error) {
|
24 | 24 |
if s == "" {
|
25 | 25 |
return "", ErrEmpty
|
26 | 26 |
}
|
27 | 27 |
return strings.ToUpper(s), nil
|
28 | 28 |
}
|
29 | 29 |
|
30 | |
func (stringService) Count(_ context.Context, s string) int {
|
|
30 |
func (stringService) Count(s string) int {
|
31 | 31 |
return len(s)
|
32 | 32 |
}
|
33 | 33 |
|
|
54 | 54 |
|
55 | 55 |
// Endpoints are a primary abstraction in go-kit. An endpoint represents a single RPC (method in our service interface)
|
56 | 56 |
func makeUppercaseEndpoint(svc StringService) endpoint.Endpoint {
|
57 | |
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
57 |
return func(_ context.Context, request interface{}) (interface{}, error) {
|
58 | 58 |
req := request.(uppercaseRequest)
|
59 | |
v, err := svc.Uppercase(ctx, req.S)
|
|
59 |
v, err := svc.Uppercase(req.S)
|
60 | 60 |
if err != nil {
|
61 | 61 |
return uppercaseResponse{v, err.Error()}, nil
|
62 | 62 |
}
|
|
65 | 65 |
}
|
66 | 66 |
|
67 | 67 |
func makeCountEndpoint(svc StringService) endpoint.Endpoint {
|
68 | |
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
68 |
return func(_ context.Context, request interface{}) (interface{}, error) {
|
69 | 69 |
req := request.(countRequest)
|
70 | |
v := svc.Count(ctx, req.S)
|
|
70 |
v := svc.Count(req.S)
|
71 | 71 |
return countResponse{v}, nil
|
72 | 72 |
}
|
73 | 73 |
}
|