diff --git a/api/kv.go b/api/kv.go index fb35758365..dd06b91017 100644 --- a/api/kv.go +++ b/api/kv.go @@ -286,7 +286,7 @@ type TxnOps []*TxnOp // TxnResult is the internal format we receive from Consul. type TxnResult struct { - KV *struct{ DirEnt *KVPair } + KV *KVPair } // TxnResults is a list of TxnResult objects. @@ -373,11 +373,7 @@ func (k *KV) Txn(txn KVTxnOps, q *WriteOptions) (bool, *KVTxnResponse, *WriteMet Errors: txnResp.Errors, } for _, result := range txnResp.Results { - var entry *KVPair - if result.KV != nil { - entry = result.KV.DirEnt - } - kvResp.Results = append(kvResp.Results, entry) + kvResp.Results = append(kvResp.Results, result.KV) } return resp.StatusCode == http.StatusOK, &kvResp, wm, nil } diff --git a/command/agent/txn_endpoint_test.go b/command/agent/txn_endpoint_test.go index d891bbca1d..4d40b5f0a9 100644 --- a/command/agent/txn_endpoint_test.go +++ b/command/agent/txn_endpoint_test.go @@ -126,36 +126,32 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) { if len(txnResp.Results) != 2 { t.Fatalf("bad: %v", txnResp) } - index = txnResp.Results[0].KV.DirEnt.ModifyIndex + index = txnResp.Results[0].KV.ModifyIndex expected := structs.TxnResponse{ Results: structs.TxnResults{ &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "key", - Value: nil, - Flags: 23, - Session: id, - LockIndex: 1, - RaftIndex: structs.RaftIndex{ - CreateIndex: index, - ModifyIndex: index, - }, + KV: &structs.DirEntry{ + Key: "key", + Value: nil, + Flags: 23, + Session: id, + LockIndex: 1, + RaftIndex: structs.RaftIndex{ + CreateIndex: index, + ModifyIndex: index, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "key", - Value: []byte("hello world"), - Flags: 23, - Session: id, - LockIndex: 1, - RaftIndex: structs.RaftIndex{ - CreateIndex: index, - ModifyIndex: index, - }, + KV: &structs.DirEntry{ + Key: "key", + Value: []byte("hello world"), + Flags: 23, + Session: id, + LockIndex: 1, + RaftIndex: structs.RaftIndex{ + CreateIndex: index, + ModifyIndex: index, }, }, }, @@ -208,32 +204,28 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) { if len(txnResp.Results) != 2 { t.Fatalf("bad: %v", txnResp) } - modIndex := txnResp.Results[0].KV.DirEnt.ModifyIndex + modIndex := txnResp.Results[0].KV.ModifyIndex expected := structs.TxnResponse{ Results: structs.TxnResults{ &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "key", - Value: nil, - Session: id, - RaftIndex: structs.RaftIndex{ - CreateIndex: index, - ModifyIndex: modIndex, - }, + KV: &structs.DirEntry{ + Key: "key", + Value: nil, + Session: id, + RaftIndex: structs.RaftIndex{ + CreateIndex: index, + ModifyIndex: modIndex, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "key", - Value: []byte("goodbye world"), - Session: id, - RaftIndex: structs.RaftIndex{ - CreateIndex: index, - ModifyIndex: modIndex, - }, + KV: &structs.DirEntry{ + Key: "key", + Value: []byte("goodbye world"), + Session: id, + RaftIndex: structs.RaftIndex{ + CreateIndex: index, + ModifyIndex: modIndex, }, }, }, diff --git a/consul/state/txn.go b/consul/state/txn.go index 525b0d4b2f..f324831cd0 100644 --- a/consul/state/txn.go +++ b/consul/state/txn.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/go-memdb" ) -func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (*structs.TxnKVResult, error) { +func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (structs.TxnKVResult, error) { var entry *structs.DirEntry var err error @@ -74,12 +74,12 @@ func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (*st // the state store). if entry != nil { if op.Verb == structs.KVSGet { - return &structs.TxnKVResult{entry}, nil + return entry, nil } clone := entry.Clone() clone.Value = nil - return &structs.TxnKVResult{clone}, nil + return clone, nil } return nil, nil diff --git a/consul/state/txn_test.go b/consul/state/txn_test.go index acf2b5d8ec..dd459bc523 100644 --- a/consul/state/txn_test.go +++ b/consul/state/txn_test.go @@ -177,13 +177,11 @@ func TestStateStore_Txn_KVS(t *testing.T) { // Make sure the response looks as expected. expected := structs.TxnResults{ &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/new", - RaftIndex: structs.RaftIndex{ - CreateIndex: 8, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/new", + RaftIndex: structs.RaftIndex{ + CreateIndex: 8, + ModifyIndex: 8, }, }, }, @@ -191,112 +189,94 @@ func TestStateStore_Txn_KVS(t *testing.T) { &structs.TxnResult{}, // delete tree &structs.TxnResult{}, // delete CAS &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/update", - Value: []byte("stale"), - RaftIndex: structs.RaftIndex{ - CreateIndex: 5, - ModifyIndex: 5, - }, + KV: &structs.DirEntry{ + Key: "foo/update", + Value: []byte("stale"), + RaftIndex: structs.RaftIndex{ + CreateIndex: 5, + ModifyIndex: 5, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ + KV: &structs.DirEntry{ - Key: "foo/update", - RaftIndex: structs.RaftIndex{ - CreateIndex: 5, - ModifyIndex: 5, - }, + Key: "foo/update", + RaftIndex: structs.RaftIndex{ + CreateIndex: 5, + ModifyIndex: 5, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/update", - RaftIndex: structs.RaftIndex{ - CreateIndex: 5, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/update", + RaftIndex: structs.RaftIndex{ + CreateIndex: 5, + ModifyIndex: 8, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/update", - Value: []byte("new"), - RaftIndex: structs.RaftIndex{ - CreateIndex: 5, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/update", + Value: []byte("new"), + RaftIndex: structs.RaftIndex{ + CreateIndex: 5, + ModifyIndex: 8, }, }, }, &structs.TxnResult{}, // get on not/there &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/update", - RaftIndex: structs.RaftIndex{ - CreateIndex: 5, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/update", + RaftIndex: structs.RaftIndex{ + CreateIndex: 5, + ModifyIndex: 8, }, }, }, &structs.TxnResult{}, // get on foo/lock before it's created &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/lock", - Session: session, - LockIndex: 1, - RaftIndex: structs.RaftIndex{ - CreateIndex: 8, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/lock", + Session: session, + LockIndex: 1, + RaftIndex: structs.RaftIndex{ + CreateIndex: 8, + ModifyIndex: 8, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/lock", - Session: session, - LockIndex: 1, - RaftIndex: structs.RaftIndex{ - CreateIndex: 8, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/lock", + Session: session, + LockIndex: 1, + RaftIndex: structs.RaftIndex{ + CreateIndex: 8, + ModifyIndex: 8, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/lock", - LockIndex: 1, - RaftIndex: structs.RaftIndex{ - CreateIndex: 8, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/lock", + LockIndex: 1, + RaftIndex: structs.RaftIndex{ + CreateIndex: 8, + ModifyIndex: 8, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "foo/lock", - LockIndex: 1, - RaftIndex: structs.RaftIndex{ - CreateIndex: 8, - ModifyIndex: 8, - }, + KV: &structs.DirEntry{ + Key: "foo/lock", + LockIndex: 1, + RaftIndex: structs.RaftIndex{ + CreateIndex: 8, + ModifyIndex: 8, }, }, }, diff --git a/consul/structs/txn.go b/consul/structs/txn.go index 7ea4a72aab..f815f5b7e5 100644 --- a/consul/structs/txn.go +++ b/consul/structs/txn.go @@ -13,9 +13,7 @@ type TxnKVOp struct { // TxnKVResult is used to define the result of a single operation on the KVS // inside a transaction. -type TxnKVResult struct { - DirEnt *DirEntry -} +type TxnKVResult *DirEntry // TxnOp is used to define a single operation inside a transaction. Only one // of the types should be filled out per entry. @@ -56,7 +54,7 @@ type TxnErrors []*TxnError // TxnResult is used to define the result of a given operation inside a // transaction. Only one of the types should be filled out per entry. type TxnResult struct { - KV *TxnKVResult + KV TxnKVResult } // TxnResults is a list of TxnResult entries. diff --git a/consul/txn_endpoint_test.go b/consul/txn_endpoint_test.go index 0dd175e118..97098f558b 100644 --- a/consul/txn_endpoint_test.go +++ b/consul/txn_endpoint_test.go @@ -71,28 +71,24 @@ func TestTxn_Apply(t *testing.T) { expected := structs.TxnResponse{ Results: structs.TxnResults{ &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "test", - Flags: 42, - Value: nil, - RaftIndex: structs.RaftIndex{ - CreateIndex: d.CreateIndex, - ModifyIndex: d.ModifyIndex, - }, + KV: &structs.DirEntry{ + Key: "test", + Flags: 42, + Value: nil, + RaftIndex: structs.RaftIndex{ + CreateIndex: d.CreateIndex, + ModifyIndex: d.ModifyIndex, }, }, }, &structs.TxnResult{ - KV: &structs.TxnKVResult{ - DirEnt: &structs.DirEntry{ - Key: "test", - Flags: 42, - Value: []byte("test"), - RaftIndex: structs.RaftIndex{ - CreateIndex: d.CreateIndex, - ModifyIndex: d.ModifyIndex, - }, + KV: &structs.DirEntry{ + Key: "test", + Flags: 42, + Value: []byte("test"), + RaftIndex: structs.RaftIndex{ + CreateIndex: d.CreateIndex, + ModifyIndex: d.ModifyIndex, }, }, }, @@ -315,7 +311,7 @@ func TestTxn_Apply_LockDelay(t *testing.T) { } if len(out.Results) != 1 || len(out.Errors) != 0 || - out.Results[0].KV.DirEnt.LockIndex != 2 { + out.Results[0].KV.LockIndex != 2 { t.Fatalf("bad: %v", out) } }