From c33b59bcab7ae16914ebeae200a6195204e76b3a Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Wed, 6 Dec 2023 11:54:31 +0800 Subject: update --- litecodec.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ litecodec_test.go | 20 +++++++++++++++++++ setter.go | 2 +- unsafecodec.go | 56 ----------------------------------------------------- unsafecodec_test.go | 20 ------------------- 5 files changed, 77 insertions(+), 77 deletions(-) create mode 100644 litecodec.go create mode 100644 litecodec_test.go delete mode 100644 unsafecodec.go delete mode 100644 unsafecodec_test.go diff --git a/litecodec.go b/litecodec.go new file mode 100644 index 0000000..0de4674 --- /dev/null +++ b/litecodec.go @@ -0,0 +1,56 @@ +package securecookie + +import "fmt" + +var _ Codec = (*LiteCodec)(nil) + +func NewLiteCodec() *LiteCodec { + s := &LiteCodec{ + sz: GobEncoder{}, + } + return s +} + +// LiteCodec encodes and decodes +type LiteCodec struct { + err error + sz Serializer +} + +// Encoding sets the encoding/serialization method for cookies. +// +// Default is encoding/gob. To encode special structures using encoding/gob, +// they must be registered first using gob.Register(). +func (s *LiteCodec) SetSerializer(sz Serializer) *LiteCodec { + s.sz = sz + + return s +} + +// Encode encodes a value. +func (s *LiteCodec) Encode(name string, value interface{}) (string, error) { + if s.err != nil { + return "", s.err + } + var err error + var b []byte + // Serialize. + if b, err = s.sz.Serialize(value); err != nil { + return "", cookieError{cause: fmt.Errorf(`%w: %s`, err, name), typ: usageError} + } + // Done. + return string(b), nil +} + +// Decode decodes a value. The dst argument must be a pointer. +func (s *LiteCodec) Decode(name, value string, dst interface{}, _ ...int) error { + if s.err != nil { + return s.err + } + // Deserialize. + if err := s.sz.Deserialize([]byte(value), dst); err != nil { + return cookieError{cause: fmt.Errorf(`%w: %s`, err, name), typ: decodeError} + } + // Done. + return nil +} diff --git a/litecodec_test.go b/litecodec_test.go new file mode 100644 index 0000000..875ee7c --- /dev/null +++ b/litecodec_test.go @@ -0,0 +1,20 @@ +package securecookie + +import "testing" + +func TestLiteCodec(t *testing.T) { + c := NewLiteCodec() + str := `TestLiteCodec` + encoded, err := c.Encode(`name`, str) + if err != nil { + t.Error(err) + } + var decoded string + err = c.Decode(`name`, encoded, &decoded) + if err != nil { + t.Error(err) + } + if decoded != str { + t.Fatalf("expected %s, got: %#v", str, decoded) + } +} diff --git a/setter.go b/setter.go index a82dee9..2ad7052 100644 --- a/setter.go +++ b/setter.go @@ -49,7 +49,7 @@ func SetSerializer(codecs []Codec, sz Serializer) { for _, c := range codecs { if codec, ok := c.(*SecureCookie); ok { codec.SetSerializer(sz) - } else if codec, ok := c.(*UnsafeCodec); ok { + } else if codec, ok := c.(*LiteCodec); ok { codec.SetSerializer(sz) } } diff --git a/unsafecodec.go b/unsafecodec.go deleted file mode 100644 index 6853658..0000000 --- a/unsafecodec.go +++ /dev/null @@ -1,56 +0,0 @@ -package securecookie - -import "fmt" - -var _ Codec = (*UnsafeCodec)(nil) - -func NewUnsafeCodec() *UnsafeCodec { - s := &UnsafeCodec{ - sz: GobEncoder{}, - } - return s -} - -// UnsafeCodec encodes and decodes -type UnsafeCodec struct { - err error - sz Serializer -} - -// Encoding sets the encoding/serialization method for cookies. -// -// Default is encoding/gob. To encode special structures using encoding/gob, -// they must be registered first using gob.Register(). -func (s *UnsafeCodec) SetSerializer(sz Serializer) *UnsafeCodec { - s.sz = sz - - return s -} - -// Encode encodes a value. -func (s *UnsafeCodec) Encode(name string, value interface{}) (string, error) { - if s.err != nil { - return "", s.err - } - var err error - var b []byte - // Serialize. - if b, err = s.sz.Serialize(value); err != nil { - return "", cookieError{cause: fmt.Errorf(`%w: %s`, err, name), typ: usageError} - } - // Done. - return string(b), nil -} - -// Decode decodes a value. The dst argument must be a pointer. -func (s *UnsafeCodec) Decode(name, value string, dst interface{}, _ ...int) error { - if s.err != nil { - return s.err - } - // Deserialize. - if err := s.sz.Deserialize([]byte(value), dst); err != nil { - return cookieError{cause: fmt.Errorf(`%w: %s`, err, name), typ: decodeError} - } - // Done. - return nil -} diff --git a/unsafecodec_test.go b/unsafecodec_test.go deleted file mode 100644 index d295907..0000000 --- a/unsafecodec_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package securecookie - -import "testing" - -func TestUnsafeCodec(t *testing.T) { - c := NewUnsafeCodec() - str := `TestUnsafeCodec` - encoded, err := c.Encode(`name`, str) - if err != nil { - t.Error(err) - } - var decoded string - err = c.Decode(`name`, encoded, &decoded) - if err != nil { - t.Error(err) - } - if decoded != str { - t.Fatalf("expected %s, got: %#v", str, decoded) - } -} -- cgit v1.2.3