Files
mailx/interfaces/interfaces.go
T

86 lines
1.8 KiB
Go
Raw Normal View History

2024-11-20 19:42:07 +08:00
package interfaces
import (
"context"
"errors"
)
2025-08-10 21:17:10 +08:00
type EmailInterface interface {
2025-11-21 15:54:49 +08:00
SetOption(ctx context.Context, opt ...Option) error // 初始化
2025-08-10 21:17:10 +08:00
GetEmailType() EmailType
2024-11-20 19:42:07 +08:00
// Send 发送邮件
Send(ctx context.Context, params Message) error
}
2025-08-10 21:17:10 +08:00
type EmailFactoryInterface interface {
Register(EmailInterface) // 注册一个接口
SetOption(opt ...Option) // 针对已注册的进行初始化
GetEmail(EmailType) (EmailInterface, error)
UnRegister(EmailType)
}
type DefaultEmail struct {
2025-11-21 14:01:43 +08:00
Options Options // 配置信息
2025-08-10 21:17:10 +08:00
EmailType EmailType
2025-11-21 15:54:49 +08:00
IsSet bool
2025-08-10 21:17:10 +08:00
}
func NewDefaultEmail() *DefaultEmail {
return &DefaultEmail{
Options: DefaultOptions(),
EmailType: "DefaultEmail",
}
}
2025-11-21 15:54:49 +08:00
func (l *DefaultEmail) SetOption(ctx context.Context, opt ...Option) error {
2025-08-10 21:17:10 +08:00
// 深复制l并且返回新的
for _, o := range opt {
2025-11-21 15:54:49 +08:00
o(&l.Options)
2025-08-10 21:17:10 +08:00
}
2025-11-21 15:54:49 +08:00
l.IsSet = true
return nil
2025-08-10 21:17:10 +08:00
}
2024-11-20 19:42:07 +08:00
2025-08-10 21:17:10 +08:00
func (l *DefaultEmail) GetEmailType() EmailType {
return l.EmailType
2024-11-20 19:42:07 +08:00
}
func (l *DefaultEmail) Send(ctx context.Context, params Message) error {
return errors.New("not implemented")
}
2025-07-27 22:13:01 +08:00
2025-08-10 21:17:10 +08:00
type DefaultEmailFactory struct {
Emails map[EmailType]EmailInterface
}
2025-07-27 22:13:01 +08:00
2025-08-10 21:17:10 +08:00
func NewDefaultEmailFactory() *DefaultEmailFactory {
return &DefaultEmailFactory{
Emails: make(map[EmailType]EmailInterface),
}
}
2025-07-27 22:13:01 +08:00
2025-08-10 21:17:10 +08:00
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)
}