From 6d8612fd51fec57c5375a894e5b40020578a0072 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 5 Nov 2020 12:03:26 +0300 Subject: [PATCH] [#189] sdk/netmap: Refactor Operation enum Replace alias to v2 type Operation with v2-compatible implementation. Signed-off-by: Leonard Lyubich --- pkg/netmap/operation.go | 107 +++++++++++++++++++++++++++++++++++ pkg/netmap/operation_test.go | 55 ++++++++++++++++++ pkg/netmap/policy.go | 1 - 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 pkg/netmap/operation.go create mode 100644 pkg/netmap/operation_test.go diff --git a/pkg/netmap/operation.go b/pkg/netmap/operation.go new file mode 100644 index 0000000..2a8281c --- /dev/null +++ b/pkg/netmap/operation.go @@ -0,0 +1,107 @@ +package netmap + +import ( + "github.com/nspcc-dev/neofs-api-go/v2/netmap" +) + +// Operation is an enumeration of v2-compatible filtering operations. +type Operation uint32 + +const ( + _ Operation = iota + + // OpEQ is an "Equal" operation. + OpEQ + + // OpNE is a "Not equal" operation. + OpNE + + // OpGT is a "Greater than" operation. + OpGT + + // OpGE is a "Greater than or equal to" operation. + OpGE + + // OpLT is a "Less than" operation. + OpLT + + // OpLE is a "Less than or equal to" operation. + OpLE + + // OpOR is an "OR" operation. + OpOR + + // OpAND is an "AND" operation. + OpAND +) + +// OperationFromV2 converts v2 Operation to Operation. +func OperationFromV2(op netmap.Operation) Operation { + switch op { + default: + return 0 + case netmap.OR: + return OpOR + case netmap.AND: + return OpAND + case netmap.GE: + return OpGE + case netmap.GT: + return OpGT + case netmap.LE: + return OpLE + case netmap.LT: + return OpLT + case netmap.EQ: + return OpEQ + case netmap.NE: + return OpNE + } +} + +// ToV2 converts Operation to v2 Operation. +func (op Operation) ToV2() netmap.Operation { + switch op { + default: + return netmap.UnspecifiedOperation + case OpOR: + return netmap.OR + case OpAND: + return netmap.AND + case OpGE: + return netmap.GE + case OpGT: + return netmap.GT + case OpLE: + return netmap.LE + case OpLT: + return netmap.LT + case OpEQ: + return netmap.EQ + case OpNE: + return netmap.NE + } +} + +func (op Operation) String() string { + switch op { + default: + return "UNSPECIFIED" + case OpNE: + return "NE" + case OpEQ: + return "EQ" + case OpLT: + return "LT" + case OpLE: + return "LE" + case OpGT: + return "GT" + case OpGE: + return "GE" + case OpAND: + return "AND" + case OpOR: + return "OR" + } +} diff --git a/pkg/netmap/operation_test.go b/pkg/netmap/operation_test.go new file mode 100644 index 0000000..c60479b --- /dev/null +++ b/pkg/netmap/operation_test.go @@ -0,0 +1,55 @@ +package netmap + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/v2/netmap" + "github.com/stretchr/testify/require" +) + +func TestOperationFromV2(t *testing.T) { + for _, item := range []struct { + op Operation + opV2 netmap.Operation + }{ + { + op: 0, + opV2: netmap.UnspecifiedOperation, + }, + { + op: OpEQ, + opV2: netmap.EQ, + }, + { + op: OpNE, + opV2: netmap.NE, + }, + { + op: OpOR, + opV2: netmap.OR, + }, + { + op: OpAND, + opV2: netmap.AND, + }, + { + op: OpLE, + opV2: netmap.LE, + }, + { + op: OpLT, + opV2: netmap.LT, + }, + { + op: OpGT, + opV2: netmap.GT, + }, + { + op: OpGE, + opV2: netmap.GE, + }, + } { + require.Equal(t, item.op, OperationFromV2(item.opV2)) + require.Equal(t, item.opV2, item.op.ToV2()) + } +} diff --git a/pkg/netmap/policy.go b/pkg/netmap/policy.go index 3ed5cc3..be5c6e8 100644 --- a/pkg/netmap/policy.go +++ b/pkg/netmap/policy.go @@ -10,7 +10,6 @@ type Selector = netmap.Selector type Filter = netmap.Filter type Replica = netmap.Replica type Clause = netmap.Clause -type Operation = netmap.Operation func PlacementPolicyToJSON(p *PlacementPolicy) ([]byte, error) { return netmap.PlacementPolicyToJSON(p)