如何在Go中使这个对象映射更加干净和可重用?

huangapple go评论78阅读模式
英文:

How can I make this object mapping more dry and reusable in Go?

问题

我在Go语言中创建了一个非关系型的对象映射,非常简单。

我有几个类似于以下结构的结构体:

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:"-"`
}

要创建一个新的消息,我只需运行以下函数:

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

然后,我有一个名为message_crud.go的文件,用于处理这个结构体,内容如下:

要通过唯一列(例如通过id)查找消息,我运行以下函数:

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
}

要保存消息(在数据库中插入或更新),我运行以下方法:

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
}

sql.PlaceholderPairs()函数如下:

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
}

但是,每次我创建一个新的结构体,例如一个User结构体,我都必须复制粘贴上面的"crud部分",创建一个user_crud.go文件,并将单词"Message"替换为"User",将单词"message"替换为"user"。我重复了很多代码,这样不够DRY(Don't Repeat Yourself)。有没有办法可以避免为我要重用的内容重复编写这段代码?我总是有一个save()方法,并且总是有一个ByUnique()函数,可以返回一个结构体并按唯一列搜索。

在PHP中,这很容易,因为PHP不是静态类型的。

在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:&quot;reply_to&quot;`
	FromId   int64         `db:&quot;from_id&quot;`
	ToId     int64         `db:&quot;to_id&quot;`
	IsActive bool          `db:&quot;is_active&quot;`
	SentTime int64         `db:&quot;sent_time&quot;`
	IsViewed bool          `db:&quot;is_viewed&quot;`

	Method   string `db:&quot;-&quot;`
	AppendTo int64  `db:&quot;-&quot;`
}

To create a new message I just run this function:

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

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 := &amp;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 := &quot;&quot;
	if this.Id == 0 {
		s = &quot;INSERT INTO message SET %s;&quot;
	} else {
		s = &quot;UPDATE message SET %s WHERE id=:id;&quot;
	}
	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 := &quot;&quot;
	val := reflect.ValueOf(i).Elem()
	count := val.NumField()

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

		fname := strings.ToLower(typeField.Name)

		if fname == &quot;id&quot; {
			continue
		}

		if t := tag.Get(&quot;db&quot;); t == &quot;-&quot; {
			continue
		} else if t != &quot;&quot; {
			s += t + &quot;=:&quot; + t
		} else {
			s += fname + &quot;=:&quot; + fname
		}
		s += &quot;, &quot;
	}
	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?

答案1

得分: 0

你可能想使用一个ORM(对象关系映射)工具。它们可以消除你所描述的很多样板代码。

关于"什么是ORM",你可以参考这个问题

以下是一些适用于Go语言的ORM工具:https://github.com/avelino/awesome-go#orm

我个人没有使用过ORM工具,所以无法给出具体推荐。主要原因是ORM会从开发者手中夺走一定的控制权,并且会引入一定的性能开销。你需要自己判断它们是否适合你的使用场景,以及你是否对这些库中的"魔法"感到舒适。

英文:

You probably want to use an ORM.
They eliminate a lot of the boilerplate code you're describing.

See this question for "What is an ORM?"

Here is a list of ORMs for go: https://github.com/avelino/awesome-go#orm

I have never used one myself, so I can't recommend any. The main reason is that an ORM takes a lot of control from the developer and introduces a non-negligible performance overhead. You need to see for yourself if they fit your use-case and/or if you are comfortable with the "magic" that's going on in those libraries.

答案2

得分: 0

我不建议这样做,我个人更喜欢在扫描到结构体和创建查询时明确一些。但是如果你真的想坚持使用反射,你可以这样做:

func ByUnique(obj interface{}, column string, value interface{}) error {
    // ...
    return sql.DB.QueryRowx(query, value).StructScan(obj)
}

// 调用方式
message := &Message{}
ByUnique(message, ...)

至于保存部分:

type Identifiable interface {
    Id() int64
}

// 为 message 等类型实现 Identifiable 接口

func Save(obj Identifiable) error {
    // ...
}

// 调用方式
Save(message)

我使用并推荐的方法是:

type Redirect struct {
    ID        string
    URL       string
    CreatedAt time.Time
}

func FindByID(db *sql.DB, id string) (*Redirect, error) {
    var redirect Redirect

    err := db.QueryRow(
        `SELECT "id", "url", "created_at" FROM "redirect" WHERE "id" = $1`, id).
        Scan(&redirect.ID, &redirect.URL, &redirect.CreatedAt)

    switch {
    case err == sql.ErrNoRows:
        return nil, nil
    case err != nil:
        return nil, err
    }

    return &redirect, nil
}

func Save(db *sql.DB, redirect *Redirect) error {
    redirect.CreatedAt = time.Now()

    _, err := db.Exec(
        `INSERT INTO "redirect" ("id", "url", "created_at") VALUES ($1, $2, $3)`,
        redirect.ID, redirect.URL, redirect.CreatedAt)

    return err
}

这种方法的优点是利用了类型系统,只映射了实际需要映射的内容。

英文:

I don't recommend doing this, i personally would prefer being explicit about scanning into structs and creating queries.

But if you really want to stick to reflection you could do:

func ByUnique(obj interface{}, column string, value interface{}) error {
    // ...
    return sql.DB.QueryRowx(query, value).StructScan(obj)
}

// Call with
message := &amp;Message{}
ByUnique(message, ...)

And for your save:

type Identifiable interface {
	Id() int64
}

// Implement Identifiable for message, etc.

func Save(obj Identifiable) error {
    // ...
}

// Call with
Save(message)

The approach i use and would recommend to you:

type Redirect struct {
	ID        string
	URL       string
	CreatedAt time.Time
}

func FindByID(db *sql.DB, id string) (*Redirect, error) {
	var redirect Redirect

	err := db.QueryRow(
		`SELECT &quot;id&quot;, &quot;url&quot;, &quot;created_at&quot; FROM &quot;redirect&quot; WHERE &quot;id&quot; = $1`, id).
		Scan(&amp;redirect.ID, &amp;redirect.URL, &amp;redirect.CreatedAt)

	switch {
	case err == sql.ErrNoRows:
		return nil, nil
	case err != nil:
		return nil, err
	}

	return &amp;redirect, nil
}

func Save(db *sql.DB, redirect *Redirect) error {
	redirect.CreatedAt = time.Now()

	_, err := db.Exec(
		`INSERT INTO &quot;redirect&quot; (&quot;id&quot;, &quot;url&quot;, &quot;created_at&quot;) VALUES ($1, $2, $3)`,
		redirect.ID, redirect.URL, redirect.CreatedAt)

	return err
}

This has the advantage of using the type system and mapping only things it should actually map.

答案3

得分: 0

你的ByUnique函数已经几乎是通用的了。只需要提取出变化的部分(表名和目标):

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
}

你的save函数非常相似。你只需要创建一个类似下面的通用保存函数:

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

然后在(*Message).save方法中调用通用的Save()函数。看起来很简单明了。

附注:不要将对象的名称命名为this,在方法内部使用。参考@OneOfOne的链接了解更多信息。不要过分追求DRY原则,它不是一个目标。Go注重代码的简洁、清晰和可靠。不要为了避免输入一行简单的错误处理代码而创建复杂而脆弱的代码。这并不意味着你不应该提取重复的代码,只是在Go中,重复一点简单的代码通常比创建复杂的代码更好。


编辑:如果你想使用接口来实现Save函数,也没有问题。只需要创建一个Identifier接口。

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)
}

这段代码中可能会出问题的地方是对Exec的调用。我不知道你使用的是哪个包,如果你传递给Exec的是接口而不是实际的结构体,可能会导致Exec无法正常工作,但它可能会正常工作。话虽如此,我可能只会传递标识符,而不是增加这个开销。

英文:

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 := &amp;Message{}
	if err := ByUnique(&quot;message&quot;, column, value, &amp;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 := &quot;&quot;
	if source.Id() == 0 {
		s = fmt.Sprintf(&quot;INSERT INTO %s SET %%s;&quot;, table)
	} else {
		s = fmt.Sprintf(&quot;UPDATE %s SET %%s WHERE id=:id;&quot;, 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(&quot;message&quot;, 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.

huangapple
  • 本文由 发表于 2016年2月8日 06:47:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/35260122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定