From 5452554b58e525e444fce7479fc956cb2f8fea21 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 16 Nov 2020 12:30:46 +0300 Subject: [PATCH] [#198] sdk: Implement XHeader type Signed-off-by: Leonard Lyubich --- pkg/xheader.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ pkg/xheader_test.go | 25 ++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 pkg/xheader.go create mode 100644 pkg/xheader_test.go diff --git a/pkg/xheader.go b/pkg/xheader.go new file mode 100644 index 00000000..84d645cf --- /dev/null +++ b/pkg/xheader.go @@ -0,0 +1,47 @@ +package pkg + +import ( + "github.com/nspcc-dev/neofs-api-go/v2/session" +) + +// XHeader represents v2-compatible XHeader. +type XHeader session.XHeader + +// NewXHeaderFromV2 wraps v2 XHeader message to XHeader. +func NewXHeaderFromV2(v *session.XHeader) *XHeader { + return (*XHeader)(v) +} + +// NewXHeader creates, initializes and returns blank XHeader instance. +func NewXHeader() *XHeader { + return NewXHeaderFromV2(new(session.XHeader)) +} + +// ToV2 converts XHeader to v2 XHeader message. +func (x *XHeader) ToV2() *session.XHeader { + return (*session.XHeader)(x) +} + +// Key returns key to X-Header. +func (x *XHeader) Key() string { + return (*session.XHeader)(x). + GetKey() +} + +// SetKey sets key to X-Header. +func (x *XHeader) SetKey(k string) { + (*session.XHeader)(x). + SetKey(k) +} + +// Value returns value of X-Header. +func (x *XHeader) Value() string { + return (*session.XHeader)(x). + GetValue() +} + +// SetValue sets value of X-Header. +func (x *XHeader) SetValue(k string) { + (*session.XHeader)(x). + SetValue(k) +} diff --git a/pkg/xheader_test.go b/pkg/xheader_test.go new file mode 100644 index 00000000..8f652278 --- /dev/null +++ b/pkg/xheader_test.go @@ -0,0 +1,25 @@ +package pkg + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestXHeader(t *testing.T) { + x := NewXHeader() + + key := "some key" + val := "some value" + + x.SetKey(key) + x.SetValue(val) + + require.Equal(t, key, x.Key()) + require.Equal(t, val, x.Value()) + + xV2 := x.ToV2() + + require.Equal(t, key, xV2.GetKey()) + require.Equal(t, val, xV2.GetValue()) +}