You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
1.6 KiB

package sms
import (
"errors"
"yunyan/lego/utils/mapstructure"
)
type Option func(*Options)
type Options struct {
AppId string
SecretId string
SecretKey string
SignName string
Template1 string
Template2 string
}
func SetAppId(v string) Option {
return func(o *Options) {
o.AppId = v
}
}
func SetSecretId(v string) Option {
return func(o *Options) {
o.SecretId = v
}
}
func KeySecretKey(v string) Option {
return func(o *Options) {
o.SecretKey = v
}
}
func SetSignName(v string) Option {
return func(o *Options) {
o.SignName = v
}
}
func SetTemplate1(v string) Option {
return func(o *Options) {
o.Template1 = v
}
}
func SetTemplate2(v string) Option {
return func(o *Options) {
o.Template2 = v
}
}
func newOptionsByOption(opts ...Option) (Options, error) {
options := Options{}
for _, o := range opts {
o(&options)
}
return options, options.validate()
}
// validate 校验发短信必需字段。
func (o Options) validate() error {
if o.SecretId == "" {
return errors.New("sms 配置缺失: SecretId 为空")
}
if o.SecretKey == "" {
return errors.New("sms 配置缺失: SecretKey 为空")
}
return nil
}
// newOptionsChecked 解析 map 配置并校验,不合法时返回 error 而非 panic。
// 启动(OnInit)与运行时热重载(Reload)共用:配置缺失只应让本子系统降级/本次重载失败,不该打挂进程。
func newOptionsChecked(config map[string]interface{}, opts ...Option) (Options, error) {
options := Options{}
if config != nil {
mapstructure.Decode(config, &options)
}
for _, o := range opts {
o(&options)
}
return options, options.validate()
}