forked from TrueCloudLab/frostfs-api-go
Add stable marshal of header filter in acl package
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
e5faf622df
commit
5623ce0124
2 changed files with 122 additions and 0 deletions
92
v2/acl/marshal.go
Normal file
92
v2/acl/marshal.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package acl
|
||||
|
||||
import (
|
||||
"github.com/nspcc-dev/neofs-api-go/util/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
FilterHeaderTypeField = 1
|
||||
FilterMatchTypeField = 2
|
||||
FilterNameField = 3
|
||||
FilterValueField = 4
|
||||
)
|
||||
|
||||
func (t *Table) StableMarshal(buf []byte) ([]byte, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (t *Table) StableSize() int {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (r *Record) StableMarshal(buf []byte) ([]byte, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (r *Record) StableSize() int {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) {
|
||||
if f == nil {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
if buf == nil {
|
||||
buf = make([]byte, f.StableSize())
|
||||
}
|
||||
|
||||
var (
|
||||
offset, n int
|
||||
err error
|
||||
)
|
||||
|
||||
n, err = proto.EnumMarshal(FilterHeaderTypeField, buf, int32(f.hdrType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
offset += n
|
||||
|
||||
n, err = proto.EnumMarshal(FilterMatchTypeField, buf[offset:], int32(f.matchType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
offset += n
|
||||
|
||||
n, err = proto.StringMarshal(FilterNameField, buf[offset:], f.name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
offset += n
|
||||
|
||||
n, err = proto.StringMarshal(FilterValueField, buf[offset:], f.value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (f *HeaderFilter) StableSize() (size int) {
|
||||
if f == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
size += proto.EnumSize(FilterHeaderTypeField, int32(f.hdrType))
|
||||
size += proto.EnumSize(FilterMatchTypeField, int32(f.matchType))
|
||||
size += proto.StringSize(FilterNameField, f.name)
|
||||
size += proto.StringSize(FilterValueField, f.value)
|
||||
|
||||
return size
|
||||
}
|
||||
|
||||
func (t *HeaderType) StableMarshal(buf []byte) ([]byte, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (t *HeaderType) StableSize() int {
|
||||
panic("not implemented")
|
||||
}
|
30
v2/acl/marshal_test.go
Normal file
30
v2/acl/marshal_test.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package acl_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/v2/acl"
|
||||
grpc "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHeaderFilter_StableMarshal(t *testing.T) {
|
||||
filterFrom := new(acl.HeaderFilter)
|
||||
transport := new(grpc.EACLRecord_FilterInfo)
|
||||
|
||||
t.Run("non empty", func(t *testing.T) {
|
||||
filterFrom.SetHeaderType(acl.HeaderTypeObject)
|
||||
filterFrom.SetMatchType(acl.MatchTypeStringEqual)
|
||||
filterFrom.SetName("Hello")
|
||||
filterFrom.SetValue("World")
|
||||
|
||||
wire, err := filterFrom.StableMarshal(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = transport.Unmarshal(wire)
|
||||
require.NoError(t, err)
|
||||
|
||||
filterTo := acl.HeaderFilterFromGRPCMessage(transport)
|
||||
require.Equal(t, filterFrom, filterTo)
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue