-
Notifications
You must be signed in to change notification settings - Fork 0
/
presenter.go
47 lines (37 loc) · 1.47 KB
/
presenter.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 presentation
import (
"time"
"github.com/prodadidb/go-email-validator/pkg/ev"
"github.com/prodadidb/go-email-validator/pkg/ev/evmail"
"github.com/prodadidb/go-email-validator/pkg/presentation/converter"
)
// GetEmail converts string to evmail.Address
type GetEmail func(email string) evmail.Address
// Interface is a decorator to represent result in a form of some service
type Interface interface {
Validate(email string, opts map[ev.ValidatorName]interface{}) (interface{}, error)
ValidateFromAddress(email evmail.Address, opts map[ev.ValidatorName]interface{}) (interface{}, error)
}
// NewPresenter creates presenter decorator
func NewPresenter(getEmail GetEmail, validator ev.Validator, converter converter.Interface) Interface {
return presenter{
getEmail: getEmail,
validator: validator,
converter: converter,
}
}
type presenter struct {
getEmail func(email string) evmail.Address
validator ev.Validator
converter converter.Interface
}
func (p presenter) Validate(email string, opts map[ev.ValidatorName]interface{}) (interface{}, error) {
address := p.getEmail(email)
return p.ValidateFromAddress(address, opts)
}
func (p presenter) ValidateFromAddress(address evmail.Address, opts map[ev.ValidatorName]interface{}) (interface{}, error) {
start := time.Now()
validationResult := p.validator.Validate(ev.NewInputFromMap(address, opts))
elapsed := time.Since(start)
return p.converter.Convert(address, validationResult, converter.NewOptions(elapsed)), nil
}