首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并两个不同类型的结构

合并两个不同类型的结构
EN

Stack Overflow用户
提问于 2018-03-22 21:49:11
回答 1查看 178关注 0票数 0

我有两种结构。

  • EventForm是用于解析请求的POST主体的结构。
  • EventTable用于创建MYSQL表结构和查找/创建行。

我希望将EventFormEventTable合并,这样就不能通过POST主体覆盖像ID这样的字段。我无法将EventForm类型转换为EventTable,因为如果字段不100%匹配,则无法将结构转换为不同的类型。那么,我的问题是,合并这两个结构的最佳方法是什么?如果把这两个结构合并起来是不可能的,我怎么才能最好地解决这个问题呢?

代码语言:javascript
复制
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"`
}
EN

回答 1

Stack Overflow用户

发布于 2018-03-22 23:24:49

我和@mkopriva在一起,并不完全理解问题的所在。假设您正在接收来自API调用的EventForm

代码语言:javascript
复制
evtForm := GetSomeEventForm()
evtTable := &models.EventTable{ EventForm: evtForm, Created: time.Now() }
someORMProbably.Insert(evtTable)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49439023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档