我有两种结构。
EventForm是用于解析请求的POST主体的结构。EventTable用于创建MYSQL表结构和查找/创建行。我希望将EventForm与EventTable合并,这样就不能通过POST主体覆盖像ID这样的字段。我无法将EventForm类型转换为EventTable,因为如果字段不100%匹配,则无法将结构转换为不同的类型。那么,我的问题是,合并这两个结构的最佳方法是什么?如果把这两个结构合并起来是不可能的,我怎么才能最好地解决这个问题呢?
package models
import "time"
// EventTable table structure of "events"
type EventTable struct {
EventForm `xorm:"extends"`
ID int `xorm:"autoincr pk 'id'" json:"id"`
Created time.Time `xorm:"not null created" json:"created"`
Updated time.Time `xorm:"not null updated" json:"updated"`
}
// TableName table name of EventTable
func (u *EventTable) TableName() string {
return "events"
}
// EventForm the structure that is received via an API call
type EventForm struct {
Title string `xorm:"not null" json:"title" required:"true"`
Description string `xorm:"not null" json:"description" required:"true"`
Owner string `xorm:"not null" json:"owner" required:"true"`
Lat string `xorm:"not null" json:"lat" required:"true"`
Lng string `xorm:"not null" json:"lng" required:"true"`
}发布于 2018-03-22 23:24:49
我和@mkopriva在一起,并不完全理解问题的所在。假设您正在接收来自API调用的EventForm
evtForm := GetSomeEventForm()
evtTable := &models.EventTable{ EventForm: evtForm, Created: time.Now() }
someORMProbably.Insert(evtTable)https://stackoverflow.com/questions/49439023
复制相似问题