go语言设计模式之Functional Options

在极客时间上看了耗子叔发布的《Go语言编程模式实战》后,将这个模式用在了项目使用上,用于初始化数据。我的需求:结构体中分为用户的5部分信息,在insert的时候都是必填,在get的时候要验证该数据存不存在,不存在则insert一条数据,所以会出现除id必填以外,其他的都可为空。这就造成了传参数的话有很多“”,所以用了这个模式来处理数据初始化。
// entity begin
type Expert struct {
	store.BaseModel
	TenantId       string `sql:"type:varchar(100)" json:"tenant_id"`
	UserId         string `sql:"type:varchar(100)" json:"user_id"`
	Photo          string `sql:"type:varchar(100)" json:"photo"`
	CallName       string `sql:"type:varchar(100)" json:"call_name"`
	Name           string `sql:"type:varchar(100)" json:"name"`
	IdentityField  string `sql:"type:varchar(100)" json:"identity_field"`
	WorkTime       int64  `sql:"type:int(100)" json:"work_time"`
	ProvinceCode   string `sql:"type:varchar(100)" json:"province_code"`
	CityCode       string `sql:"type:varchar(100)" json:"city_code"`
	DistrictCode   string `sql:"type:varchar(100)" json:"district_code"`
	Position       string `sql:"type:varchar(100)" json:"position"`
	Advantage      string `sql:"type:varchar(100)" json:"advantage"`
	Mobile         string `sql:"type:varchar(100)" json:"mobile"`
	Email          string `sql:"type:varchar(100)" json:"email"`
	DisplayName    string `sql:"type:varchar(100)" json:"display_name"`
	ExpectSalary   string `sql:"type:varchar(100)" json:"expect_salary"`
	BankCard       string `sql:"type:varchar(100)" json:"bank_card"`
	AcctName       string `sql:"type:varchar(100)" json:"acct_name"`
	BankName       string `sql:"type:varchar(100)" json:"bank_name"`
	RcvBank        string `sql:"type:varchar(100)" json:"rcv_bank"`
	RealAuthStatus string `sql:"type:varchar(100)" json:"real_auth_status"`
	AgreementAt    int64  `sql:"type:int(100)" json:"agreement_at"`
	IdCardNo       string `sql:"type:varchar(30)" json:"id_card_no"`
	IdCardType     string `sql:"type:varchar(2)" json:"id_card_type"`
	Nationality    string `sql:"type:varchar(30)" json:"nationality"`
	Gender         string `sql:"type:varchar(6)" json:"gender"`
}

type Option func(*Expert)


func Information(photo,callName,identityField,provinceCode, cityCode, districtCode  string,workTime int64) Option {
	return func(s *Expert) {
		s.Photo = photo
		s.CallName = callName
		s.IdentityField = identityField
		s.WorkTime = workTime
		s.ProvinceCode = provinceCode
		s.CityCode = cityCode
		s.DistrictCode = districtCode
	}
}



func BriefIntroduction(position,advantage string) Option {
	return func(s *Expert) {
		s.Position = position
		s.Advantage = advantage
	}
}


func ContactInformation(mobile,email,display_name string) Option {
	return func(s *Expert) {
		s.Mobile = mobile
		s.Email = email
		s.DisplayName = display_name
	}
}


func AgreementAt(agreementAt int64) Option {
	return func(s *Expert) {
		s.AgreementAt = agreementAt
	}
}


func ChargingInformation(expectSalary,bankCard,acctName,bankName,rcvBank string) Option {
	return func(s *Expert) {
		s.ExpectSalary = expectSalary
		s.BankCard = bankCard
		s.AcctName = acctName
		s.BankName = bankName
		s.RcvBank = rcvBank
	}
}



func UserIdInformations(name,idCardNo,idCardType,nationality,gender,realAuthStatus string) Option {
	return func(s *Expert) {
		s.Name = name
		s.IdCardNo = idCardNo
		s.IdCardType = idCardType
		s.Nationality = nationality
		s.Gender = gender
		s.RealAuthStatus = realAuthStatus
	}
}

func NewExperts(tenantId,userId string, options ...func(*Expert)) (*Expert, error) {
	method := lu.Method("store.NewExperts")
	id, err := common.Gid()
	if err != nil {
		common.Log.ErrorWithFields(map[string]interface{}{"method": method, "error": err, "entity": "customer experts"}, "Failed to initialize the entity")
		return nil, errors.ConvertFrom(err)
	}
	expert := &Expert{
		BaseModel: store.BaseModel{
			Id: id,
		},
		TenantId: tenantId,
		UserId:   userId,
	}
	for _, option := range options {
		option(expert)
	}
	return expert, nil
}

//全参数调用
expert, err := store.NewExperts(tenantId, userId, store.Information(req.Photo, req.CallName, req.IdentityField, req.ProvinceCode, req.CityCode, req.DistrictCode, req.WorkTime), store.BriefIntroduction(req.Position, req.Advantage), store.ContactInformation(req.Mobile, req.Email, req.DisplayName), store.ChargingInformation(req.ExpectSalary, req.BankCard, req.AcctName, req.BankName, req.RcvBank), store.UserIdInformations(Name, IdCardNo, IdCardType, Nationality, Gender, RealAuthStatus))

//缺少参数调用
experts, err := store.NewExperts(tenantId, userId, store.UserIdInformations(Name, IdCardNo, IdCardType, Nationality, Gender, RealAuthStatus))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值