Skip to content

Commit

Permalink
object: Support numerical matchers in search queries
Browse files Browse the repository at this point in the history
NeoFS API protocol was recently extended with numerical `>`, `>=`, `<`
and `<=` match operators. These operators should be provided by SDK so
users could use them in their search object queries.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Jan 30, 2024
1 parent 03ed6db commit 11c51d7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
67 changes: 58 additions & 9 deletions object/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const (
MatchStringNotEqual
MatchNotPresent
MatchCommonPrefix
MatchNumGT
MatchNumGE
MatchNumLT
MatchNumLE
)

// ToV2 converts [SearchMatchType] to v2 [v2object.MatchType] enum value.
Expand All @@ -37,6 +41,14 @@ func (m SearchMatchType) ToV2() v2object.MatchType {
return v2object.MatchNotPresent
case MatchCommonPrefix:
return v2object.MatchCommonPrefix
case MatchNumGT:
return 5
case MatchNumGE:
return 6
case MatchNumLT:
return 7
case MatchNumLE:
return 8
default:
return v2object.MatchUnknown
}
Expand All @@ -53,6 +65,14 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
m = MatchNotPresent
case v2object.MatchCommonPrefix:
m = MatchCommonPrefix
case 5:
m = MatchNumGT
case 6:
m = MatchNumGE
case 7:
m = MatchNumLT
case 8:
m = MatchNumLE
default:
m = MatchUnknown
}
Expand All @@ -67,9 +87,24 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
// - [MatchStringNotEqual]: STRING_NOT_EQUAL;
// - [MatchNotPresent]: NOT_PRESENT;
// - [MatchCommonPrefix]: COMMON_PREFIX;
// - [MatchNumGT], default: NUM_GT;
// - [MatchNumGE], default: NUM_GE;
// - [MatchNumLT], default: NUM_LT;
// - [MatchNumLE], default: NUM_LE;
// - [MatchUnknown], default: MATCH_TYPE_UNSPECIFIED.
func (m SearchMatchType) EncodeToString() string {
return m.ToV2().String()
switch m {
default:
return m.ToV2().String()
case MatchNumGT:
return "NUM_GT"
case MatchNumGE:
return "NUM_GE"
case MatchNumLT:
return "NUM_LT"
case MatchNumLE:
return "NUM_LE"
}
}

// String implements [fmt.Stringer].
Expand All @@ -86,15 +121,27 @@ func (m SearchMatchType) String() string {
//
// Returns true if s was parsed successfully.
func (m *SearchMatchType) DecodeString(s string) bool {
var g v2object.MatchType

ok := g.FromString(s)

if ok {
*m = SearchMatchFromV2(g)
switch s {
default:
var g v2object.MatchType

ok := g.FromString(s)

if ok {
*m = SearchMatchFromV2(g)
}

return ok
case "NUM_GT":
*m = MatchNumGT
case "NUM_GE":
*m = MatchNumGE
case "NUM_LT":
*m = MatchNumLT
case "NUM_LE":
*m = MatchNumLE
}

return ok
return true
}

type stringEncoder interface {
Expand Down Expand Up @@ -200,6 +247,8 @@ func (f *SearchFilters) addFilter(op SearchMatchType, key string, val stringEnco
}

// AddFilter adds a filter to group by simple plain parameters.
//
// If op is numerical (e.g. [MatchNumGT], value must be a base-10 integer.
func (f *SearchFilters) AddFilter(key, value string, op SearchMatchType) {
f.addFilter(op, key, staticStringer(value))
}
Expand Down
8 changes: 8 additions & 0 deletions object/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var eqV2Matches = map[object.SearchMatchType]v2object.MatchType{
object.MatchStringNotEqual: v2object.MatchStringNotEqual,
object.MatchNotPresent: v2object.MatchNotPresent,
object.MatchCommonPrefix: v2object.MatchCommonPrefix,
object.MatchNumGT: 5,
object.MatchNumGE: 6,
object.MatchNumLT: 7,
object.MatchNumLE: 8,
}

func TestMatch(t *testing.T) {
Expand Down Expand Up @@ -265,6 +269,10 @@ func TestSearchMatchType_String(t *testing.T) {
{val: toPtr(object.MatchStringNotEqual), str: "STRING_NOT_EQUAL"},
{val: toPtr(object.MatchNotPresent), str: "NOT_PRESENT"},
{val: toPtr(object.MatchUnknown), str: "MATCH_TYPE_UNSPECIFIED"},
{val: toPtr(object.MatchNumGT), str: "NUM_GT"},
{val: toPtr(object.MatchNumGE), str: "NUM_GE"},
{val: toPtr(object.MatchNumLT), str: "NUM_LT"},
{val: toPtr(object.MatchNumLE), str: "NUM_LE"},
})
}

Expand Down

0 comments on commit 11c51d7

Please sign in to comment.