嘲笑 mgo 链式函数

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

Mocking mgo chaining functions

问题

在测试我的代码库时,我发现使用接口来模拟"gopkg.in/mgo.v2"中的对象非常有用。我遇到了一个问题,mgo.Query没有实现我的接口Query。

import mgo "gopkg.in/mgo.v2"

type Collection interface {
    FindId(interface{}) Query
    Find(interface{}) Query
    UpdateId(interface{}, interface{}) error
    Update(interface{}, interface{}) error
    UpsertId(interface{}, interface{}) (interface{}, error)
    Insert(...interface{}) error
    RemoveId(interface{}) error
}

type Query interface {
    One(interface{}) error
    All(interface{}) error
    Select(interface{}) Query
}

var _ Query = (*mgo.Query)(nil)

Query的转换引发了错误:

cannot use (*mgo.Query)(nil) (type *mgo.Query) as type Query in assignment:
    *mgo.Query does not implement Query (wrong type for Select method)
        have Select(interface {}) *mgo.Query
        want Select(interface {}) Query

这是由于链式函数无法在接口中定义导致的问题吗?我不确定如何定义一个与mgo实现匹配的Select头部。

英文:

While testing some of my codebase I found it useful to mock out objects from "gopkg.in/mgo.v2" with interfaces. I'm running into a problem where mgo.Query does not implement my interface Query.

import mgo "gopkg.in/mgo.v2"

type Collection interface {
	FindId(interface{}) Query
	Find(interface{}) Query
	UpdateId(interface{}, interface{}) error
	Update(interface{}, interface{}) error
	UpsertId(interface{}, interface{}) (interface{}, error)
	Insert(...interface{}) error
	RemoveId(interface{}) error
}

type Query interface {
	One(interface{}) error
	All(interface{}) error
	Select(interface{}) Query
}

var _ Query = (*mgo.Query)(nil)

The Query cast kicks up the error:

cannot use (*mgo.Query)(nil) (type *mgo.Query) as type Query in assignment:
    *mgo.Query does not implement Query (wrong type for Select method)
        have Select(interface {}) *mgo.Query
        want Select(interface {}) Query

Is this a problem with chain functions not being able to be defined in an interface? I'm not sure how to make a Select header that will match the mgo implementation.

答案1

得分: 1

函数签名不同,这就是为什么你遇到了编译错误。你的接口版本的Select函数返回你的Query类型,而mgo的Select函数返回*mgo.Query,这是一个不同的类型。即使该类型实现了你的接口,函数签名仍然不同。你需要在这里添加另一层,以便能够包装mgo包。

type MgoQuery struct {
    *mgo.Query
}

func (q *MgoQuery) Select(selector interface{}) Query {
    return q.Query.Select(selector)
}

...
英文:

The function signatures are different, which is why you're running into a compilation error. Your interface version of Select returns your Query type, while mgo's Select returns *mgo.Query, which is a different type. Even if that type does implement your interface, the function signatures are still different. You will need to add another layer to this where you are able to wrap the mgo package.

type MgoQuery struct {
    *mgo.Query
}

func (q *MgoQuery) Select(selector interface{}) Query {
    return q.Query.Select(selector)
}

...

huangapple
  • 本文由 发表于 2017年9月16日 00:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/46244007.html
匿名

发表评论

匿名网友

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

确定