Compare-and-swap in etcd v3?

2.3k Views Asked by At

I'm trying to do a compare-and-store operation on a given key using etcd's Go client for the v3 API. Seeing that --swap-with-value seems to be gone from etcdctl put, I suspect there's no corresponding method or argument in the client library either. Thus, I'm attempting my own implementation:

res, err := etcdv3.KV.Txn(context.Background()).If(
    etcdv3.Compare(etcdv3.ModRevision(c.path), "=", c.modRevision),
).Then(
    etcdv3.OpPut(c.path, string(data)),
    etcdv3.OpGet(c.path),
).Commit()

if err != nil {
    return err
}
if !res.Succeeded {
    return fmt.Errorf("A newer revision exists.")
}

// Return the current `ModRevision` on success.
return res.Responses[1].GetResponseRange().Kvs[0].ModRevision

However, Go refuses to compile with this message:

main.go:55: not enough arguments in call to method expression clientv3.KV.Txn
    have ("context".Context)
    want (clientv3.KV, "github.com/miguel/myproject/vendor/golang.org/x/net/context".Context)

What's wrong with this approach? Why does Go refuse to compile this snippet?

Edit: Thanks to JimB I found out that etcdv3 is importing the old golang/x/net/context packages. I switched the import statements, but I still get something similar:

main.go:55: not enough arguments in call to method expression clientv3.KV.Txn
    have ("github.com/miguel/myproject/vendor/golang.org/x/net/context".Context)
    want (clientv3.KV, "github.com/miguel/myproject/vendor/golang.org/x/net/context".Context)
0

There are 0 best solutions below