Skip to content

Commit 08139c9

Browse files
committed
Add support for replace operation to the agency
1 parent 178c882 commit 08139c9

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

agency/operation.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ type keyArrayErase struct {
7474
value interface{}
7575
}
7676

77+
type keyArrayReplace struct {
78+
keyCommon
79+
newValue interface{}
80+
oldValue interface{}
81+
}
82+
7783
// NewKeyDelete returns a new key operation which must be removed from the agency
7884
func NewKeyDelete(key []string) KeyChanger {
7985
return &keyDelete{
@@ -127,6 +133,17 @@ func NewKeyArrayErase(key []string, value interface{}) KeyChanger {
127133
}
128134
}
129135

136+
// NewKeyArrayReplace returns a new key operation for replacing element in the array.
137+
func NewKeyArrayReplace(key []string, oldValue, newValue interface{}) KeyChanger {
138+
return &keyArrayReplace{
139+
keyCommon: keyCommon{
140+
key: key,
141+
},
142+
newValue: newValue,
143+
oldValue: oldValue,
144+
}
145+
}
146+
130147
func (k *keyDelete) GetOperation() string {
131148
return "delete"
132149
}
@@ -229,3 +246,23 @@ func (k *keyArrayErase) GetURL() string {
229246
func (k *keyArrayErase) GetVal() interface{} {
230247
return k.value
231248
}
249+
250+
func (k *keyArrayReplace) GetOperation() string {
251+
return "replace"
252+
}
253+
254+
func (k *keyArrayReplace) GetTTL() time.Duration {
255+
return 0
256+
}
257+
258+
func (k *keyArrayReplace) GetNew() interface{} {
259+
return k.newValue
260+
}
261+
262+
func (k *keyArrayReplace) GetURL() string {
263+
return ""
264+
}
265+
266+
func (k *keyArrayReplace) GetVal() interface{} {
267+
return k.oldValue
268+
}

test/agency_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,67 @@ func writeTransaction(t *testing.T, transient bool) {
815815
},
816816
},
817817
},
818+
{
819+
name: "Replace element in array",
820+
requests: []Request{
821+
{
822+
transaction: TransactionTest{
823+
keys: []agency.KeyChanger{
824+
agency.NewKeyArrayPush([]string{rootKeyAgency, "test", "array"}, map[string]interface{}{
825+
"database": "db",
826+
"collection": "col",
827+
"shard": 0,
828+
}),
829+
},
830+
},
831+
},
832+
{
833+
transaction: TransactionTest{
834+
keys: []agency.KeyChanger{
835+
agency.NewKeyArrayPush([]string{rootKeyAgency, "test", "array"}, map[string]interface{}{
836+
"database": "db",
837+
"collection": "col",
838+
"shard": 1,
839+
}),
840+
},
841+
},
842+
},
843+
{
844+
transaction: TransactionTest{
845+
keys: []agency.KeyChanger{
846+
agency.NewKeyArrayReplace([]string{rootKeyAgency, "test", "array"},
847+
map[string]interface{}{
848+
"database": "db",
849+
"collection": "col",
850+
"shard": 0,
851+
}, map[string]interface{}{
852+
"database": "db",
853+
"collection": "col",
854+
"shard": 2,
855+
}),
856+
},
857+
},
858+
},
859+
},
860+
expectedResult: map[string]interface{}{
861+
rootKeyAgency: map[string]interface{}{
862+
"test": map[string]interface{}{
863+
"array": []interface{}{
864+
map[string]interface{}{
865+
"database": "db",
866+
"collection": "col",
867+
"shard": float64(2),
868+
},
869+
map[string]interface{}{
870+
"database": "db",
871+
"collection": "col",
872+
"shard": float64(1),
873+
},
874+
},
875+
},
876+
},
877+
},
878+
},
818879
}
819880

820881
for _, testCase := range testCases {

0 commit comments

Comments
 (0)