From 53756c3ef985f8b24346d829c673156861066871 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Wed, 6 Dec 2023 12:34:46 +0800 Subject: update --- defaults.go | 5 +++++ litecodec.go | 56 ------------------------------------------------------- litecodec_test.go | 20 -------------------- setter.go | 2 -- 4 files changed, 5 insertions(+), 78 deletions(-) create mode 100644 defaults.go delete mode 100644 litecodec.go delete mode 100644 litecodec_test.go diff --git a/defaults.go b/defaults.go new file mode 100644 index 0000000..a552a84 --- /dev/null +++ b/defaults.go @@ -0,0 +1,5 @@ +package securecookie + +var Gob = GobEncoder{} +var JSON = JSONEncoder{} +var Nop = NopEncoder{} diff --git a/litecodec.go b/litecodec.go deleted file mode 100644 index 0de4674..0000000 --- a/litecodec.go +++ /dev/null @@ -1,56 +0,0 @@ -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 deleted file mode 100644 index 875ee7c..0000000 --- a/litecodec_test.go +++ /dev/null @@ -1,20 +0,0 @@ -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 2ad7052..cb56171 100644 --- a/setter.go +++ b/setter.go @@ -49,8 +49,6 @@ func SetSerializer(codecs []Codec, sz Serializer) { for _, c := range codecs { if codec, ok := c.(*SecureCookie); ok { codec.SetSerializer(sz) - } else if codec, ok := c.(*LiteCodec); ok { - codec.SetSerializer(sz) } } } -- cgit v1.2.3