dprxj1995 2019-04-26 00:32
浏览 5

如何在golang中模拟GCP的存储?

I'm really new to mocking third-party library in go, I'm mocking cloud.google.com/go/storage right now

I'm using mockery. This is my current interface:

//Client storage client
type Client interface {
    Bucket(name string) BucketHandle
    Buckets(ctx context.Context, projectID string) BucketIterator
}

//BucketHandle storage's BucketHandle
type BucketHandle interface {
    Attrs(context.Context) (*storage.BucketAttrs, error)
    Objects(context.Context, *storage.Query) ObjectIterator
}

//ObjectIterator storage's ObjectIterator
type ObjectIterator interface {
    Next() (*storage.ObjectAttrs, error)
}

//BucketIterator storage's BucketIterator
type BucketIterator interface {
    Next() (*storage.BucketAttrs, error)
}

and this is how I use it in my function

//Runner runner for this module
type Runner struct {
    StorageClient stiface.Client
}

.... function
   //get storage client
    client, err := storage.NewClient(ctx)
    if err != nil {
        return err
    }

    runner := Runner{
        StorageClient: client,
    }
.... rest of functions

However, I got this error:

cannot use client (type *"cloud.google.com/go/storage".Client) as type stiface.Client in field value: *"cloud.google.com/go/storage".Client does not implement stiface.Client (wrong type for Bucket method) have Bucket(string) *"cloud.google.com/go/storage".BucketHandle want Bucket(string) stiface.BucketHandle

What have I done wrong here? Thanks!

Edit

here's one example of the code that I want to mock. I'd like to mock on bucketIterator.Next():

//GetBuckets get list of buckets
func GetBuckets(ctx context.Context, client *storage.Client, projectName string) []checker.Resource {
    //Get bucket iterator based on a project
    bucketIterator := client.Buckets(ctx, projectName)
    //iterate over the buckets and store bucket details
    buckets := make([]checker.Resource, 0)
    for bucket, done := bucketIterator.Next(); done == nil; bucket, done = bucketIterator.Next() {
        buckets = append(buckets, checker.Resource{
            Name: bucket.Name,
            Type: "Bucket",
        })
    }
    return buckets
}
  • 写回答

1条回答 默认 最新

  • duanrui3480 2019-04-26 04:33
    关注

    The error message is basically saying your stiface.Client defines an interface that *storage.Client does not implement. On first glance your code looks valid however the problem lies in your interface method signatures and because they have outputs as interfaces.

    Go makes a difference between the statements:

    • This function returns a BucketHandle
    • and this function returns a *storage.BucketHandle that is a BucketHandle

    Try changing your interface to return the *storage.BucketHandle. You can see a more complex example of similar behaviour in the mockery S3API example where the functions return the s3 types, not their own interfaces.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题