Skip to content

Commit 3439a34

Browse files
committed
api: created constructors for predicates
Closes #TNTP-4539.
1 parent d9295a0 commit 3439a34

File tree

6 files changed

+271
-17
lines changed

6 files changed

+271
-17
lines changed

go.mod

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,32 @@ module github.com/tarantool/go-storage
33
go 1.24.0
44

55
require (
6+
github.com/stretchr/testify v1.11.1
67
github.com/tarantool/go-tarantool/v2 v2.4.0
7-
go.etcd.io/etcd/client/v3 v3.6.4
8+
go.etcd.io/etcd/client/v3 v3.6.5
89
)
910

1011
require (
1112
github.com/coreos/go-semver v0.3.1 // indirect
1213
github.com/coreos/go-systemd/v22 v22.6.0 // indirect
14+
github.com/davecgh/go-spew v1.1.1 // indirect
1315
github.com/gogo/protobuf v1.3.2 // indirect
1416
github.com/golang/protobuf v1.5.4 // indirect
1517
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
1619
github.com/tarantool/go-iproto v1.1.0 // indirect
1720
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
1821
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
19-
go.etcd.io/etcd/api/v3 v3.6.4 // indirect
20-
go.etcd.io/etcd/client/pkg/v3 v3.6.4 // indirect
22+
go.etcd.io/etcd/api/v3 v3.6.5 // indirect
23+
go.etcd.io/etcd/client/pkg/v3 v3.6.5 // indirect
2124
go.uber.org/multierr v1.11.0 // indirect
2225
go.uber.org/zap v1.27.0 // indirect
2326
golang.org/x/net v0.44.0 // indirect
2427
golang.org/x/sys v0.36.0 // indirect
2528
golang.org/x/text v0.29.0 // indirect
26-
google.golang.org/genproto/googleapis/api v0.0.0-20250908214217-97024824d090 // indirect
27-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250908214217-97024824d090 // indirect
29+
google.golang.org/genproto/googleapis/api v0.0.0-20250922171735-9219d122eba9 // indirect
30+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9 // indirect
2831
google.golang.org/grpc v1.75.1 // indirect
2932
google.golang.org/protobuf v1.36.9 // indirect
33+
gopkg.in/yaml.v3 v3.0.1 // indirect
3034
)

go.sum

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 h1:8Tjv8EJ+pM1xP8mK6egEbD1OgnV
2020
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2/go.mod h1:pkJQ2tZHJ0aFOVEEot6oZmaVEZcRme73eIFmhiVuRWs=
2121
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
2222
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
23+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
24+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
25+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
26+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2327
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2428
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
25-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
26-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
29+
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
30+
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
31+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
32+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
2733
github.com/tarantool/go-iproto v1.1.0 h1:HULVOIHsiehI+FnHfM7wMDntuzUddO09DKqu2WnFQ5A=
2834
github.com/tarantool/go-iproto v1.1.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
2935
github.com/tarantool/go-tarantool/v2 v2.4.0 h1:cfGngxdknpVVbd/vF2LvaoWsKjsLV9i3xC859XgsJlI=
@@ -34,12 +40,12 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh
3440
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
3541
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
3642
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
37-
go.etcd.io/etcd/api/v3 v3.6.4 h1:7F6N7toCKcV72QmoUKa23yYLiiljMrT4xCeBL9BmXdo=
38-
go.etcd.io/etcd/api/v3 v3.6.4/go.mod h1:eFhhvfR8Px1P6SEuLT600v+vrhdDTdcfMzmnxVXXSbk=
39-
go.etcd.io/etcd/client/pkg/v3 v3.6.4 h1:9HBYrjppeOfFjBjaMTRxT3R7xT0GLK8EJMVC4xg6ok0=
40-
go.etcd.io/etcd/client/pkg/v3 v3.6.4/go.mod h1:sbdzr2cl3HzVmxNw//PH7aLGVtY4QySjQFuaCgcRFAI=
41-
go.etcd.io/etcd/client/v3 v3.6.4 h1:YOMrCfMhRzY8NgtzUsHl8hC2EBSnuqbR3dh84Uryl7A=
42-
go.etcd.io/etcd/client/v3 v3.6.4/go.mod h1:jaNNHCyg2FdALyKWnd7hxZXZxZANb0+KGY+YQaEMISo=
43+
go.etcd.io/etcd/api/v3 v3.6.5 h1:pMMc42276sgR1j1raO/Qv3QI9Af/AuyQUW6CBAWuntA=
44+
go.etcd.io/etcd/api/v3 v3.6.5/go.mod h1:ob0/oWA/UQQlT1BmaEkWQzI0sJ1M0Et0mMpaABxguOQ=
45+
go.etcd.io/etcd/client/pkg/v3 v3.6.5 h1:Duz9fAzIZFhYWgRjp/FgNq2gO1jId9Yae/rLn3RrBP8=
46+
go.etcd.io/etcd/client/pkg/v3 v3.6.5/go.mod h1:8Wx3eGRPiy0qOFMZT/hfvdos+DjEaPxdIDiCDUv/FQk=
47+
go.etcd.io/etcd/client/v3 v3.6.5 h1:yRwZNFBx/35VKHTcLDeO7XVLbCBFbPi+XV4OC3QJf2U=
48+
go.etcd.io/etcd/client/v3 v3.6.5/go.mod h1:ZqwG/7TAFZ0BJ0jXRPoJjKQJtbFo/9NIY8uoFFKcCyo=
4349
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
4450
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
4551
go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ=
@@ -91,14 +97,16 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
9197
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9298
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
9399
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
94-
google.golang.org/genproto/googleapis/api v0.0.0-20250908214217-97024824d090 h1:d8Nakh1G+ur7+P3GcMjpRDEkoLUcLW2iU92XVqR+XMQ=
95-
google.golang.org/genproto/googleapis/api v0.0.0-20250908214217-97024824d090/go.mod h1:U8EXRNSd8sUYyDfs/It7KVWodQr+Hf9xtxyxWudSwEw=
96-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250908214217-97024824d090 h1:/OQuEa4YWtDt7uQWHd3q3sUMb+QOLQUg1xa8CEsRv5w=
97-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250908214217-97024824d090/go.mod h1:GmFNa4BdJZ2a8G+wCe9Bg3wwThLrJun751XstdJt5Og=
100+
google.golang.org/genproto/googleapis/api v0.0.0-20250922171735-9219d122eba9 h1:jm6v6kMRpTYKxBRrDkYAitNJegUeO1Mf3Kt80obv0gg=
101+
google.golang.org/genproto/googleapis/api v0.0.0-20250922171735-9219d122eba9/go.mod h1:LmwNphe5Afor5V3R5BppOULHOnt2mCIf+NxMd4XiygE=
102+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9 h1:V1jCN2HBa8sySkR5vLcCSqJSTMv093Rw9EJefhQGP7M=
103+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9/go.mod h1:HSkG/KdJWusxU1F6CNrwNDjBMgisKxGnc5dAZfT0mjQ=
98104
google.golang.org/grpc v1.75.1 h1:/ODCNEuf9VghjgO3rqLcfg8fiOP0nSluljWFlDxELLI=
99105
google.golang.org/grpc v1.75.1/go.mod h1:JtPAzKiq4v1xcAB2hydNlWI2RnF85XXcV0mhKXr2ecQ=
100106
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
101107
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
102108
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
109+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
110+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
103111
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
104112
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

predicate/op_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package predicate_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/tarantool/go-storage/predicate"
9+
)
10+
11+
func TestOpString(t *testing.T) {
12+
t.Parallel()
13+
14+
tests := []struct {
15+
name string
16+
op predicate.Op
17+
expected string
18+
}{
19+
{"OpEqual", predicate.OpEqual, "Equal"},
20+
{"OpNotEqual", predicate.OpNotEqual, "NotEqual"},
21+
{"OpGreater", predicate.OpGreater, "Greater"},
22+
{"OpLess", predicate.OpLess, "Less"},
23+
{"UnknownOp", predicate.Op(99), "Unknown"},
24+
}
25+
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
t.Parallel()
29+
30+
result := tt.op.String()
31+
assert.Equal(t, tt.expected, result)
32+
})
33+
}
34+
}

predicate/predicate.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,92 @@ type Predicate interface {
1414
// Value returns the comparison value for the predicate.
1515
Value() any
1616
}
17+
18+
// predicate is the concrete implementation of the Predicate interface.
19+
// It represents a condition used for conditional operations in transactions.
20+
type predicate struct {
21+
key []byte
22+
op Op
23+
target Target
24+
value interface{}
25+
}
26+
27+
// Key returns the key that this predicate applies to.
28+
func (p predicate) Key() []byte {
29+
return p.key
30+
}
31+
32+
// Operation returns the comparison operation (Equal, NotEqual, Greater, Less).
33+
func (p predicate) Operation() Op {
34+
return p.op
35+
}
36+
37+
// Target returns what aspect of the record to compare (e.g. Version or Value).
38+
func (p predicate) Target() Target {
39+
return p.target
40+
}
41+
42+
// Value returns the comparison value for the predicate.
43+
func (p predicate) Value() interface{} {
44+
return p.value
45+
}
46+
47+
// ValueNotEqual creates a predicate that checks if a key's value is not equal to the specified value.
48+
func ValueNotEqual(key []byte, value interface{}) Predicate {
49+
return &predicate{
50+
key: key,
51+
op: OpNotEqual,
52+
target: TargetValue,
53+
value: value,
54+
}
55+
}
56+
57+
// ValueEqual creates a predicate that checks if a key's value equals the specified value.
58+
func ValueEqual(key []byte, value interface{}) Predicate {
59+
return &predicate{
60+
key: key,
61+
op: OpEqual,
62+
target: TargetValue,
63+
value: value,
64+
}
65+
}
66+
67+
// VersionEqual creates a predicate that checks if a key's version equals the specified version.
68+
func VersionEqual(key []byte, version int64) Predicate {
69+
return &predicate{
70+
key: key,
71+
op: OpEqual,
72+
target: TargetVersion,
73+
value: version,
74+
}
75+
}
76+
77+
// VersionNotEqual creates a predicate that checks if a key's version is not equal to the specified version.
78+
func VersionNotEqual(key []byte, version int64) Predicate {
79+
return &predicate{
80+
key: key,
81+
op: OpNotEqual,
82+
target: TargetVersion,
83+
value: version,
84+
}
85+
}
86+
87+
// VersionGreater creates a predicate that checks if a key's version is greater than the specified version.
88+
func VersionGreater(key []byte, version int64) Predicate {
89+
return &predicate{
90+
key: key,
91+
op: OpGreater,
92+
target: TargetVersion,
93+
value: version,
94+
}
95+
}
96+
97+
// VersionLess creates a predicate that checks if a key's version is less than the specified version.
98+
func VersionLess(key []byte, version int64) Predicate {
99+
return &predicate{
100+
key: key,
101+
op: OpLess,
102+
target: TargetVersion,
103+
value: version,
104+
}
105+
}

predicate/predicate_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package predicate_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/tarantool/go-storage/predicate"
9+
)
10+
11+
func TestValueNotEqual(t *testing.T) {
12+
t.Parallel()
13+
14+
key := []byte("test-key")
15+
value := "test-value"
16+
p := predicate.ValueNotEqual(key, value)
17+
18+
assert.Equal(t, key, p.Key())
19+
assert.Equal(t, predicate.OpNotEqual, p.Operation())
20+
assert.Equal(t, predicate.TargetValue, p.Target())
21+
assert.Equal(t, value, p.Value())
22+
}
23+
24+
func TestValueEqual(t *testing.T) {
25+
t.Parallel()
26+
27+
key := []byte("test-key")
28+
value := 42
29+
p := predicate.ValueEqual(key, value)
30+
31+
assert.Equal(t, key, p.Key())
32+
assert.Equal(t, predicate.OpEqual, p.Operation())
33+
assert.Equal(t, predicate.TargetValue, p.Target())
34+
assert.Equal(t, value, p.Value())
35+
}
36+
37+
func TestVersionEqual(t *testing.T) {
38+
t.Parallel()
39+
40+
key := []byte("test-key")
41+
version := int64(123)
42+
p := predicate.VersionEqual(key, version)
43+
44+
assert.Equal(t, key, p.Key())
45+
assert.Equal(t, predicate.OpEqual, p.Operation())
46+
assert.Equal(t, predicate.TargetVersion, p.Target())
47+
assert.Equal(t, version, p.Value())
48+
}
49+
50+
func TestVersionNotEqual(t *testing.T) {
51+
t.Parallel()
52+
53+
key := []byte("test-key")
54+
version := int64(456)
55+
p := predicate.VersionNotEqual(key, version)
56+
57+
assert.Equal(t, key, p.Key())
58+
assert.Equal(t, predicate.OpNotEqual, p.Operation())
59+
assert.Equal(t, predicate.TargetVersion, p.Target())
60+
assert.Equal(t, version, p.Value())
61+
}
62+
63+
func TestVersionGreater(t *testing.T) {
64+
t.Parallel()
65+
66+
key := []byte("test-key")
67+
version := int64(789)
68+
p := predicate.VersionGreater(key, version)
69+
70+
assert.Equal(t, key, p.Key())
71+
assert.Equal(t, predicate.OpGreater, p.Operation())
72+
assert.Equal(t, predicate.TargetVersion, p.Target())
73+
assert.Equal(t, version, p.Value())
74+
}
75+
76+
func TestVersionLess(t *testing.T) {
77+
t.Parallel()
78+
79+
key := []byte("test-key")
80+
version := int64(999)
81+
p := predicate.VersionLess(key, version)
82+
83+
assert.Equal(t, key, p.Key())
84+
assert.Equal(t, predicate.OpLess, p.Operation())
85+
assert.Equal(t, predicate.TargetVersion, p.Target())
86+
assert.Equal(t, version, p.Value())
87+
}

predicate/target_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package predicate_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/tarantool/go-storage/predicate"
9+
)
10+
11+
func TestTargetString(t *testing.T) {
12+
t.Parallel()
13+
14+
tests := []struct {
15+
name string
16+
target predicate.Target
17+
expected string
18+
}{
19+
{"TargetVersion", predicate.TargetVersion, "Version"},
20+
{"TargetValue", predicate.TargetValue, "Value"},
21+
{"UnknownTarget", predicate.Target(99), "Unknown"},
22+
}
23+
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
t.Parallel()
27+
28+
result := tt.target.String()
29+
assert.Equal(t, tt.expected, result)
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)