mirror of https://github.com/status-im/consul.git
De-nests the KV output structure (removes DirEnt member).
This commit is contained in:
parent
960b9d6fb6
commit
4882a9fe43
|
@ -286,7 +286,7 @@ type TxnOps []*TxnOp
|
||||||
|
|
||||||
// TxnResult is the internal format we receive from Consul.
|
// TxnResult is the internal format we receive from Consul.
|
||||||
type TxnResult struct {
|
type TxnResult struct {
|
||||||
KV *struct{ DirEnt *KVPair }
|
KV *KVPair
|
||||||
}
|
}
|
||||||
|
|
||||||
// TxnResults is a list of TxnResult objects.
|
// 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,
|
Errors: txnResp.Errors,
|
||||||
}
|
}
|
||||||
for _, result := range txnResp.Results {
|
for _, result := range txnResp.Results {
|
||||||
var entry *KVPair
|
kvResp.Results = append(kvResp.Results, result.KV)
|
||||||
if result.KV != nil {
|
|
||||||
entry = result.KV.DirEnt
|
|
||||||
}
|
|
||||||
kvResp.Results = append(kvResp.Results, entry)
|
|
||||||
}
|
}
|
||||||
return resp.StatusCode == http.StatusOK, &kvResp, wm, nil
|
return resp.StatusCode == http.StatusOK, &kvResp, wm, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,12 +126,11 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
||||||
if len(txnResp.Results) != 2 {
|
if len(txnResp.Results) != 2 {
|
||||||
t.Fatalf("bad: %v", txnResp)
|
t.Fatalf("bad: %v", txnResp)
|
||||||
}
|
}
|
||||||
index = txnResp.Results[0].KV.DirEnt.ModifyIndex
|
index = txnResp.Results[0].KV.ModifyIndex
|
||||||
expected := structs.TxnResponse{
|
expected := structs.TxnResponse{
|
||||||
Results: structs.TxnResults{
|
Results: structs.TxnResults{
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "key",
|
Key: "key",
|
||||||
Value: nil,
|
Value: nil,
|
||||||
Flags: 23,
|
Flags: 23,
|
||||||
|
@ -143,10 +142,8 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "key",
|
Key: "key",
|
||||||
Value: []byte("hello world"),
|
Value: []byte("hello world"),
|
||||||
Flags: 23,
|
Flags: 23,
|
||||||
|
@ -159,7 +156,6 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(txnResp, expected) {
|
if !reflect.DeepEqual(txnResp, expected) {
|
||||||
t.Fatalf("bad: %v", txnResp)
|
t.Fatalf("bad: %v", txnResp)
|
||||||
|
@ -208,12 +204,11 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
||||||
if len(txnResp.Results) != 2 {
|
if len(txnResp.Results) != 2 {
|
||||||
t.Fatalf("bad: %v", txnResp)
|
t.Fatalf("bad: %v", txnResp)
|
||||||
}
|
}
|
||||||
modIndex := txnResp.Results[0].KV.DirEnt.ModifyIndex
|
modIndex := txnResp.Results[0].KV.ModifyIndex
|
||||||
expected := structs.TxnResponse{
|
expected := structs.TxnResponse{
|
||||||
Results: structs.TxnResults{
|
Results: structs.TxnResults{
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "key",
|
Key: "key",
|
||||||
Value: nil,
|
Value: nil,
|
||||||
Session: id,
|
Session: id,
|
||||||
|
@ -223,10 +218,8 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "key",
|
Key: "key",
|
||||||
Value: []byte("goodbye world"),
|
Value: []byte("goodbye world"),
|
||||||
Session: id,
|
Session: id,
|
||||||
|
@ -237,7 +230,6 @@ func TestTxnEndpoint_KV_Actions(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(txnResp, expected) {
|
if !reflect.DeepEqual(txnResp, expected) {
|
||||||
t.Fatalf("bad: %v", txnResp)
|
t.Fatalf("bad: %v", txnResp)
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"github.com/hashicorp/go-memdb"
|
"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 entry *structs.DirEntry
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -74,12 +74,12 @@ func (s *StateStore) txnKVS(tx *memdb.Txn, idx uint64, op *structs.TxnKVOp) (*st
|
||||||
// the state store).
|
// the state store).
|
||||||
if entry != nil {
|
if entry != nil {
|
||||||
if op.Verb == structs.KVSGet {
|
if op.Verb == structs.KVSGet {
|
||||||
return &structs.TxnKVResult{entry}, nil
|
return entry, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
clone := entry.Clone()
|
clone := entry.Clone()
|
||||||
clone.Value = nil
|
clone.Value = nil
|
||||||
return &structs.TxnKVResult{clone}, nil
|
return clone, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
|
@ -177,8 +177,7 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
// Make sure the response looks as expected.
|
// Make sure the response looks as expected.
|
||||||
expected := structs.TxnResults{
|
expected := structs.TxnResults{
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/new",
|
Key: "foo/new",
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
CreateIndex: 8,
|
CreateIndex: 8,
|
||||||
|
@ -186,13 +185,11 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{}, // delete
|
&structs.TxnResult{}, // delete
|
||||||
&structs.TxnResult{}, // delete tree
|
&structs.TxnResult{}, // delete tree
|
||||||
&structs.TxnResult{}, // delete CAS
|
&structs.TxnResult{}, // delete CAS
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/update",
|
Key: "foo/update",
|
||||||
Value: []byte("stale"),
|
Value: []byte("stale"),
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
|
@ -201,10 +198,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
|
|
||||||
Key: "foo/update",
|
Key: "foo/update",
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
|
@ -213,10 +208,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/update",
|
Key: "foo/update",
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
CreateIndex: 5,
|
CreateIndex: 5,
|
||||||
|
@ -224,10 +217,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/update",
|
Key: "foo/update",
|
||||||
Value: []byte("new"),
|
Value: []byte("new"),
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
|
@ -236,11 +227,9 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{}, // get on not/there
|
&structs.TxnResult{}, // get on not/there
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/update",
|
Key: "foo/update",
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
CreateIndex: 5,
|
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{}, // get on foo/lock before it's created
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/lock",
|
Key: "foo/lock",
|
||||||
Session: session,
|
Session: session,
|
||||||
LockIndex: 1,
|
LockIndex: 1,
|
||||||
|
@ -262,10 +249,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/lock",
|
Key: "foo/lock",
|
||||||
Session: session,
|
Session: session,
|
||||||
LockIndex: 1,
|
LockIndex: 1,
|
||||||
|
@ -275,10 +260,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/lock",
|
Key: "foo/lock",
|
||||||
LockIndex: 1,
|
LockIndex: 1,
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
|
@ -287,10 +270,8 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "foo/lock",
|
Key: "foo/lock",
|
||||||
LockIndex: 1,
|
LockIndex: 1,
|
||||||
RaftIndex: structs.RaftIndex{
|
RaftIndex: structs.RaftIndex{
|
||||||
|
@ -299,7 +280,6 @@ func TestStateStore_Txn_KVS(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if len(results) != len(expected) {
|
if len(results) != len(expected) {
|
||||||
t.Fatalf("bad: %v", results)
|
t.Fatalf("bad: %v", results)
|
||||||
|
|
|
@ -13,9 +13,7 @@ type TxnKVOp struct {
|
||||||
|
|
||||||
// TxnKVResult is used to define the result of a single operation on the KVS
|
// TxnKVResult is used to define the result of a single operation on the KVS
|
||||||
// inside a transaction.
|
// inside a transaction.
|
||||||
type TxnKVResult struct {
|
type TxnKVResult *DirEntry
|
||||||
DirEnt *DirEntry
|
|
||||||
}
|
|
||||||
|
|
||||||
// TxnOp is used to define a single operation inside a transaction. Only one
|
// TxnOp is used to define a single operation inside a transaction. Only one
|
||||||
// of the types should be filled out per entry.
|
// 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
|
// 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.
|
// transaction. Only one of the types should be filled out per entry.
|
||||||
type TxnResult struct {
|
type TxnResult struct {
|
||||||
KV *TxnKVResult
|
KV TxnKVResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// TxnResults is a list of TxnResult entries.
|
// TxnResults is a list of TxnResult entries.
|
||||||
|
|
|
@ -71,8 +71,7 @@ func TestTxn_Apply(t *testing.T) {
|
||||||
expected := structs.TxnResponse{
|
expected := structs.TxnResponse{
|
||||||
Results: structs.TxnResults{
|
Results: structs.TxnResults{
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "test",
|
Key: "test",
|
||||||
Flags: 42,
|
Flags: 42,
|
||||||
Value: nil,
|
Value: nil,
|
||||||
|
@ -82,10 +81,8 @@ func TestTxn_Apply(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
&structs.TxnResult{
|
&structs.TxnResult{
|
||||||
KV: &structs.TxnKVResult{
|
KV: &structs.DirEntry{
|
||||||
DirEnt: &structs.DirEntry{
|
|
||||||
Key: "test",
|
Key: "test",
|
||||||
Flags: 42,
|
Flags: 42,
|
||||||
Value: []byte("test"),
|
Value: []byte("test"),
|
||||||
|
@ -96,7 +93,6 @@ func TestTxn_Apply(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(out, expected) {
|
if !reflect.DeepEqual(out, expected) {
|
||||||
t.Fatalf("bad %v", out)
|
t.Fatalf("bad %v", out)
|
||||||
|
@ -315,7 +311,7 @@ func TestTxn_Apply_LockDelay(t *testing.T) {
|
||||||
}
|
}
|
||||||
if len(out.Results) != 1 ||
|
if len(out.Results) != 1 ||
|
||||||
len(out.Errors) != 0 ||
|
len(out.Errors) != 0 ||
|
||||||
out.Results[0].KV.DirEnt.LockIndex != 2 {
|
out.Results[0].KV.LockIndex != 2 {
|
||||||
t.Fatalf("bad: %v", out)
|
t.Fatalf("bad: %v", out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue