iris
iris copied to clipboard
[Feature request] For reading the body of a request, is there a tag that supports both "url" and "form"?
E.g:
type HospPay struct {
ID int `gorm:"column:ID;primaryKey" json:"ID" form:"id" url:"id"`
PayWebType int `gorm:"column:Pay_WebType;default:0" json:"Pay_WebType" form:"oper_type" url:"oper_type"` // 1.整形 2.眼科
PayType int `gorm:"column:Pay_Type;default:0" json:"Pay_Type" form:"fee_type" url:"fee_type"` // 0.正常 1.公单
PayHzDate string `gorm:"column:Pay_HzDate;type:varchar(7)" json:"Pay_HzDate" form:"hz_date" url:"hz_date"`
PayProv string `gorm:"column:Pay_Prov;type:varchar(20)" json:"Pay_Prov" form:"prov" url:"prov"`
PayCity string `gorm:"column:Pay_City;type:varchar(20)" json:"Pay_City" form:"city" url:"city"`
PayHospID int `gorm:"column:Pay_HospID;default:0" json:"Pay_HospID" form:"hosp_id" url:"hosp_id"`
PayHospName string `gorm:"column:Pay_HospName;type:varchar(80)" json:"Pay_HospName" form:"hosp_name" url:"hosp_name"`
PayOperCount int `gorm:"column:Pay_OperCount;default:0" json:"Pay_OperCount" form:"oper_count" url:"oper_count"`
PayOperMoney float32 `gorm:"column:Pay_OperMoney;default:0" json:"Pay_OperMoney" form:"money" url:"money"`
PayInvoice int `gorm:"column:Pay_Invoice;default:0" json:"Pay_Invoice" form:"invoice" url:"invoice"` // 0.无发票 1.有发票
PayState int `gorm:"column:Pay_State;default:0" json:"Pay_State" form:"pay_state" url:"pay_state"` // 0.未付 1.已付
PayDesc string `gorm:"column:Pay_Desc;type:varchar(250)" json:"Pay_Desc" form:"desc" url:"desc"`
PayRlPerson string `gorm:"column:Pay_RlPerson;type:varchar(20)" json:"Pay_RlPerson" form:"rl_person" url:"rl_person"`
PayScPerson string `gorm:"column:Pay_ScPerson;type:varchar(20)" json:"Pay_ScPerson" form:"sc_person" url:"sc_person"`
PayJsPerson string `gorm:"column:Pay_JsPerson;type:varchar(20)" json:"Pay_JsPerson" form:"js_person" url:"js_person"`
PayRlDate types.Time `gorm:"column:Pay_RlDate;type:datetime" json:"Pay_RlDate"`
PayDkDate types.Time `gorm:"column:Pay_DkDate;type:datetime" json:"Pay_DkDate"`
PayIsDel int `gorm:"column:Pay_isDel;default:0" json:"Pay_isDel"`
PayAccID int `gorm:"column:Pay_AccID;default:0" json:"Pay_AccID"`
}
The interface needs to read the parameters passed from get or post. So I can only write the same tag twice: url: "xx", form: "xx", wasting time and code。 Is it possible that only one tag can be defined to support both types at the same time。
My assumption is, suppose I only define one param:"hosp_name"
, param:"hosp_id"
:
**( Is equivalent to defining at the same time:url:"hosp_name", form:"hosp_name"
, param:"hosp_id"
、param:"hosp_id"
) ** 。
Then I have two interfaces for query and update:
// If this is a get method, check the value of the url parameter and assign it to the corresponding struct。
func (c *ApiController) GetFinanceHosps(h HospPay) {
// get request ->
// form: hosp_name = 'post test name',
// url: hosp_id:100,
// url: hosp_name: 'get test name'
// Because this is a get request, HospPay cannot receive the from parameter:
// resutl: HospPay(PayHospID: 100, PayHospName: "get test name")
}
// If this is a post method, check the value of the form parameter and assign it to the corresponding struct.
func (c *ApiController) PostFinanceHospUpd(h HospPay) {
// post request ->
// form: hosp_name = 'test post name',
// url: hosp_id:100,
// url: hosp_name: 'get test name'
// Because this is a post request, HospPay cannot receive the url parameter:
// resutl: HospPay(PayHospID: 0, PayHospName: "post test name")
}
Because I have encountered this scenario many times in the structure, I hope that only one label is enough。 I am here to provide an idea to iris. If iris already supports this writing method, please let me know, thank you! I can't find a related issue。
@hjzCy, we wait for go1.16
release, which will contain support for multi struct field tag keys per value :) Check the conversation here: https://github.com/golang/go/issues/40281#issuecomment-666500138. When go 1.16 will be released you will be able to do something like that:
ID int `gorm:"column:ID;primaryKey" json form url:"id"`
Doesn't worth to provide an extra Iris configuration option for that as it will be resolved by the Go Team :)
go1.16
has reverted this feature(https://github.com/golang/tools/commit/21398c4d1a19761d59d39decd0e7e8ed1984bc83), really sucks.
That really sucks... I will take a look for a solution soon (probably this weekend).