0

I have two structures.

  • EventForm is the structure that is used to parse the POST body of a request.
  • EventTable is used for creating the MYSQL table structure and finding/creating rows.

I want to merge EventForm with EventTable so that fields like ID cannot be overridden via the POST body. I am not able to convert the type of EventForm to EventTable since you cannot convert a struct to a different type if the fields do not 100% match. So my question is what is the best way to merge these two structs? If it is not plausible to merge these two structs how could I best solve this problem?

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"`
}
1
  • What do you mean by "merge"? Do you want only one type instead of two, e.g. Event? Is embedding, which is what you already use, not enough? If so, how? What do you need that embedding doesn't provide? Commented Mar 22, 2018 at 22:33

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.