forked from TrueCloudLab/frostfs-api-go
[#168] refs: Implement binary/JSON encoders/decoders on Signature
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
1519a02d63
commit
4b55b06780
6 changed files with 110 additions and 4 deletions
|
@ -42,3 +42,35 @@ func (s *Signature) SetSign(v []byte) {
|
|||
func (s *Signature) ToV2() *refs.Signature {
|
||||
return (*refs.Signature)(s)
|
||||
}
|
||||
|
||||
// Marshal marshals Signature into a protobuf binary form.
|
||||
//
|
||||
// Buffer is allocated when the argument is empty.
|
||||
// Otherwise, the first buffer is used.
|
||||
func (s *Signature) Marshal(b ...[]byte) ([]byte, error) {
|
||||
var buf []byte
|
||||
if len(b) > 0 {
|
||||
buf = b[0]
|
||||
}
|
||||
|
||||
return (*refs.Signature)(s).
|
||||
StableMarshal(buf)
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals protobuf binary representation of Signature.
|
||||
func (s *Signature) Unmarshal(data []byte) error {
|
||||
return (*refs.Signature)(s).
|
||||
Unmarshal(data)
|
||||
}
|
||||
|
||||
// MarshalJSON encodes Signature to protobuf JSON format.
|
||||
func (s *Signature) MarshalJSON() ([]byte, error) {
|
||||
return (*refs.Signature)(s).
|
||||
MarshalJSON()
|
||||
}
|
||||
|
||||
// UnmarshalJSON decodes Signature from protobuf JSON format.
|
||||
func (s *Signature) UnmarshalJSON(data []byte) error {
|
||||
return (*refs.Signature)(s).
|
||||
UnmarshalJSON(data)
|
||||
}
|
||||
|
|
33
pkg/signature_test.go
Normal file
33
pkg/signature_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSignatureEncoding(t *testing.T) {
|
||||
s := NewSignature()
|
||||
s.SetKey([]byte("key"))
|
||||
s.SetSign([]byte("sign"))
|
||||
|
||||
t.Run("binary", func(t *testing.T) {
|
||||
data, err := s.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
s2 := NewSignature()
|
||||
require.NoError(t, s2.Unmarshal(data))
|
||||
|
||||
require.Equal(t, s, s2)
|
||||
})
|
||||
|
||||
t.Run("json", func(t *testing.T) {
|
||||
data, err := s.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
|
||||
s2 := NewSignature()
|
||||
require.NoError(t, s2.UnmarshalJSON(data))
|
||||
|
||||
require.Equal(t, s, s2)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue