De-nests the KV output structure (removes DirEnt member).

This commit is contained in:
James Phillips 2016-05-11 13:48:03 -07:00
parent 04a13ec3d7
commit 3d35acaa90
6 changed files with 114 additions and 152 deletions

View file

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

View file

@ -126,12 +126,11 @@ 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{
KV: &structs.DirEntry{
Key: "key",
Value: nil,
Flags: 23,
@ -143,10 +142,8 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "key",
Value: []byte("hello world"),
Flags: 23,
@ -159,7 +156,6 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
},
},
},
},
}
if !reflect.DeepEqual(txnResp, expected) {
t.Fatalf("bad: %v", txnResp)
@ -208,12 +204,11 @@ 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{
KV: &structs.DirEntry{
Key: "key",
Value: nil,
Session: id,
@ -223,10 +218,8 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "key",
Value: []byte("goodbye world"),
Session: id,
@ -237,7 +230,6 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
},
},
},
},
}
if !reflect.DeepEqual(txnResp, expected) {
t.Fatalf("bad: %v", txnResp)

View file

@ -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

View file

@ -177,8 +177,7 @@ 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{
KV: &structs.DirEntry{
Key: "foo/new",
RaftIndex: structs.RaftIndex{
CreateIndex: 8,
@ -186,13 +185,11 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{}, // delete
&structs.TxnResult{}, // delete tree
&structs.TxnResult{}, // delete CAS
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/update",
Value: []byte("stale"),
RaftIndex: structs.RaftIndex{
@ -201,10 +198,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/update",
RaftIndex: structs.RaftIndex{
@ -213,10 +208,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/update",
RaftIndex: structs.RaftIndex{
CreateIndex: 5,
@ -224,10 +217,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/update",
Value: []byte("new"),
RaftIndex: structs.RaftIndex{
@ -236,11 +227,9 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{}, // get on not/there
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/update",
RaftIndex: structs.RaftIndex{
CreateIndex: 5,
@ -248,11 +237,9 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{}, // get on foo/lock before it's created
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/lock",
Session: session,
LockIndex: 1,
@ -262,10 +249,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/lock",
Session: session,
LockIndex: 1,
@ -275,10 +260,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/lock",
LockIndex: 1,
RaftIndex: structs.RaftIndex{
@ -287,10 +270,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "foo/lock",
LockIndex: 1,
RaftIndex: structs.RaftIndex{
@ -299,7 +280,6 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
},
}
if len(results) != len(expected) {
t.Fatalf("bad: %v", results)

View file

@ -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.

View file

@ -71,8 +71,7 @@ func TestTxn_Apply(t *testing.T) {
expected := structs.TxnResponse{
Results: structs.TxnResults{
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "test",
Flags: 42,
Value: nil,
@ -82,10 +81,8 @@ func TestTxn_Apply(t *testing.T) {
},
},
},
},
&structs.TxnResult{
KV: &structs.TxnKVResult{
DirEnt: &structs.DirEntry{
KV: &structs.DirEntry{
Key: "test",
Flags: 42,
Value: []byte("test"),
@ -96,7 +93,6 @@ func TestTxn_Apply(t *testing.T) {
},
},
},
},
}
if !reflect.DeepEqual(out, expected) {
t.Fatalf("bad %v", out)
@ -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)
}
}