douqingzhi0980 2016-02-07 22:47
浏览 8
已采纳

如何在Go中使该对象映射更加干燥和可重用?

I have created an object mapping in Go that is not relational, it is very simple.

I have several structs that looks like this:

type Message struct {
    Id       int64
    Message  string
    ReplyTo  sql.NullInt64 `db:"reply_to"`
    FromId   int64         `db:"from_id"`
    ToId     int64         `db:"to_id"`
    IsActive bool          `db:"is_active"`
    SentTime int64         `db:"sent_time"`
    IsViewed bool          `db:"is_viewed"`

    Method   string `db:"-"`
    AppendTo int64  `db:"-"`
}

To create a new message I just run this function:

func New() *Message {
    return &Message{
        IsActive: true,
        SentTime: time.Now().Unix(),
        Method:   "new",
    }
}

And then I have a message_crud.go file for this struct that looks like this:

To find a message by a unique column (for example by id) I run this function:

func ByUnique(column string, value interface{}) (*Message, error) {

    query := fmt.Sprintf(`
        SELECT *
        FROM message
        WHERE %s = ?
        LIMIT 1;
    `, column)

    message := &Message{}
    err := sql.DB.QueryRowx(query, value).StructScan(message)
    if err != nil {
        return nil, err
    }
    return message, nil
}

And to save a message (insert or update in the database) I run this method:

func (this *Message) save() error {
    s := ""
    if this.Id == 0 {
        s = "INSERT INTO message SET %s;"
    } else {
        s = "UPDATE message SET %s WHERE id=:id;"
    }
    query := fmt.Sprintf(s, sql.PlaceholderPairs(this))

    nstmt, err := sql.DB.PrepareNamed(query)
    if err != nil {
        return err
    }

    res, err := nstmt.Exec(*this)
    if err != nil {
        return err
    }

    if this.Id == 0 {
        lastId, err := res.LastInsertId()
        if err != nil {
            return err
        }
        this.Id = lastId
    }

    return nil
}

The sql.PlaceholderPairs() function looks like this:

func PlaceholderPairs(i interface{}) string {

    s := ""
    val := reflect.ValueOf(i).Elem()
    count := val.NumField()

    for i := 0; i < count; i++ {
        typeField := val.Type().Field(i)
        tag := typeField.Tag

        fname := strings.ToLower(typeField.Name)

        if fname == "id" {
            continue
        }

        if t := tag.Get("db"); t == "-" {
            continue
        } else if t != "" {
            s += t + "=:" + t
        } else {
            s += fname + "=:" + fname
        }
        s += ", "
    }
    s = s[:len(s)-2]
    return s
}

But every time I create a new struct, for example a User struct I have to copy paste the "crud section" above and create a user_crud.go file and replace the words "Message" with "User", and the words "message" with "user". I repeat alot of code and it is not very dry. Is there something I could do to not repeat this code for things I would reuse? I always have a save() method, and always have a function ByUnique() where I can return a struct and search by a unique column.

In PHP this was easy because PHP is not statically typed.

Is this possible to do in Go?

  • 写回答

3条回答 默认 最新

  • donglv9813 2016-02-08 01:10
    关注

    Your ByUnique is almost generic already. Just pull out the piece that varies (the table and destination):

    func ByUnique(table string, column string, value interface{}, dest interface{}) error {
        query := fmt.Sprintf(`
                SELECT *
                FROM %s
                WHERE %s = ?
                LIMIT 1;
            `, table, column)
    
        return sql.DB.QueryRowx(query, value).StructScan(dest)
    }
    
    func ByUniqueMessage(column string, value interface{}) (*Message, error) {
        message := &Message{}
        if err := ByUnique("message", column, value, &message); err != nil {
            return nil, err
        }
        return message, error
    }
    

    Your save is very similar. You just need to make a generic save function along the lines of:

    func Save(table string, identifier int64, source interface{}) { ... }
    

    Then inside of (*Message)save, you'd just call the general Save() function. Looks pretty straightforward.

    Side notes: do not use this as the name of the object inside a method. See the link from @OneOfOne for more on that. And do not get obsessed about DRY. It is not a goal in itself. Go focuses on code being simple, clear, and reliable. Do not create something complicated and fragile just to avoid typing a simple line of error handling. This doesn't mean that you shouldn't extract duplicated code. It just means that in Go it is usually better to repeat simple code a little bit rather than create complicated code to avoid it.


    EDIT: If you want to implement Save using an interface, that's no problem. Just create an Identifier interface.

    type Ider interface {
        Id() int64
        SetId(newId int64)
    }
    
    func (msg *Message) Id() int64 {
        return msg.Id
    }
    
    func (msg *Message) SetId(newId int64) {
        msg.Id = newId
    }
    
    func Save(table string, source Ider) error {
        s := ""
        if source.Id() == 0 {
            s = fmt.Sprintf("INSERT INTO %s SET %%s;", table)
        } else {
            s = fmt.Sprintf("UPDATE %s SET %%s WHERE id=:id;", table)
        }
        query := fmt.Sprintf(s, sql.PlaceholderPairs(source))
    
        nstmt, err := sql.DB.PrepareNamed(query)
        if err != nil {
            return err
        }
    
        res, err := nstmt.Exec(source)
        if err != nil {
            return err
        }
    
        if source.Id() == 0 {
            lastId, err := res.LastInsertId()
            if err != nil {
                return err
            }
            source.SetId(lastId)
        }
    
        return nil
    }
    
    func (msg *Message) save() error {
        return Save("message", msg)
    }
    

    The one piece that might blow up with this is the call to Exec. I don't know what package you're using, and it's possible that Exec won't work correctly if you pass it an interface rather than the actual struct, but it probably will work. That said, I'd probably just pass the identifier rather than adding this overhead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路