我有两种结构。
EventForm
是用于解析请求的POST
正文的结构。EventTable
用于创建MYSQL
表格结构以及查找/创建行。我想将EventForm
与EventTable
合并,以便无法通过ID
机构覆盖POST
等字段。我无法将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"`
}
答案 0 :(得分:0)
I am with @mkopriva and don't fully understand what the problem is. Assuming you are receiving EventForm from some API call
evtForm := GetSomeEventForm()
evtTable := &models.EventTable{ EventForm: evtForm, Created: time.Now() }
someORMProbably.Insert(evtTable)