调整接口
This commit is contained in:
@@ -5,23 +5,81 @@ import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Email interface {
|
||||
InitEmail(ctx context.Context, opt ...Option) (Email, error)
|
||||
type EmailType string
|
||||
|
||||
type EmailInterface interface {
|
||||
SetOption(ctx context.Context, opt ...Option) (EmailInterface, error) // 初始化
|
||||
GetEmailType() EmailType
|
||||
// Send 发送邮件
|
||||
Send(ctx context.Context, params Message) error
|
||||
}
|
||||
|
||||
type DefaultEmail struct{}
|
||||
type EmailFactoryInterface interface {
|
||||
Register(EmailInterface) // 注册一个接口
|
||||
SetOption(opt ...Option) // 针对已注册的进行初始化
|
||||
GetEmail(EmailType) (EmailInterface, error)
|
||||
UnRegister(EmailType)
|
||||
}
|
||||
|
||||
func (l *DefaultEmail) InitEmail(ctx context.Context, opt ...Option) (Email, error) {
|
||||
return &DefaultEmail{}, errors.New("not implemented")
|
||||
type DefaultEmail struct {
|
||||
Options emailOption
|
||||
EmailType EmailType
|
||||
}
|
||||
|
||||
func NewDefaultEmail() *DefaultEmail {
|
||||
return &DefaultEmail{
|
||||
Options: DefaultOptions(),
|
||||
EmailType: "DefaultEmail",
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DefaultEmail) SetOption(ctx context.Context, opt ...Option) (EmailInterface, error) {
|
||||
// 深复制l并且返回新的
|
||||
newL := *l
|
||||
|
||||
for _, o := range opt {
|
||||
o(&newL.Options)
|
||||
}
|
||||
|
||||
return &newL, nil
|
||||
}
|
||||
|
||||
func (l *DefaultEmail) GetEmailType() EmailType {
|
||||
return l.EmailType
|
||||
}
|
||||
|
||||
func (l *DefaultEmail) Send(ctx context.Context, params Message) error {
|
||||
return errors.New("not implemented")
|
||||
}
|
||||
|
||||
// 初始化一个接口
|
||||
type DefaultEmailFactory struct {
|
||||
Emails map[EmailType]EmailInterface
|
||||
}
|
||||
|
||||
// 实际执行一个接口
|
||||
func NewDefaultEmailFactory() *DefaultEmailFactory {
|
||||
return &DefaultEmailFactory{
|
||||
Emails: make(map[EmailType]EmailInterface),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DefaultEmailFactory) Register(email EmailInterface) {
|
||||
l.Emails[email.GetEmailType()] = email
|
||||
}
|
||||
|
||||
func (l *DefaultEmailFactory) SetOption(opt ...Option) {
|
||||
for _, email := range l.Emails {
|
||||
email.SetOption(context.Background(), opt...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DefaultEmailFactory) GetEmail(emailType EmailType) (EmailInterface, error) {
|
||||
email, ok := l.Emails[emailType]
|
||||
if !ok {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
return email, nil
|
||||
}
|
||||
|
||||
func (l *DefaultEmailFactory) UnRegister(emailType EmailType) {
|
||||
delete(l.Emails, emailType)
|
||||
}
|
||||
|
||||
+24
-5
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
type emailOption struct {
|
||||
logger loggerx.LoggerInterface
|
||||
Logger loggerx.LoggerInterface
|
||||
|
||||
Smtp *EmailConfigDataSmtp `json:"smtp,omitempty"` // smtp
|
||||
Aws *EmailConfigDataAws `json:"aws,omitempty"` // 亚马逊
|
||||
@@ -15,10 +15,10 @@ type emailOption struct {
|
||||
Mailgun *EmialConfigDataMailgun `json:"mailgun,omitempty"` // mailgun
|
||||
}
|
||||
|
||||
func defaultOptions() emailOption {
|
||||
func DefaultOptions() emailOption {
|
||||
ctx := context.Background()
|
||||
return emailOption{
|
||||
logger: loggerx.NewLogger(ctx),
|
||||
Logger: loggerx.NewLogger(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,30 @@ type Option func(*emailOption)
|
||||
// 设置日志
|
||||
func SetLogger(logger loggerx.LoggerInterface) Option {
|
||||
return func(o *emailOption) {
|
||||
o.logger = logger
|
||||
o.Logger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func SetSmtp(smtp *EmailConfigDataSmtp) Option {
|
||||
return func(o *emailOption) {
|
||||
o.Smtp = smtp
|
||||
}
|
||||
}
|
||||
|
||||
func SetAws(aws *EmailConfigDataAws) Option {
|
||||
return func(o *emailOption) {
|
||||
o.Aws = aws
|
||||
}
|
||||
}
|
||||
|
||||
func SetAliyun(aliyun *EmialConfigDataAliyun) Option {
|
||||
return func(o *emailOption) {
|
||||
o.Aliyun = aliyun
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func SetMailgun(mailgun *EmialConfigDataMailgun) Option {
|
||||
return func(o *emailOption) {
|
||||
o.Mailgun = mailgun
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user