aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHank Shen <swh@admpub.com>2023-12-06 07:34:46 +0300
committerHank Shen <swh@admpub.com>2023-12-06 07:34:46 +0300
commit53756c3ef985f8b24346d829c673156861066871 (patch)
tree766ba6e643e5e21058b3f942c0c9eb014dfe10f1
parentc33b59bcab7ae16914ebeae200a6195204e76b3a (diff)
-rw-r--r--defaults.go5
-rw-r--r--litecodec.go56
-rw-r--r--litecodec_test.go20
-rw-r--r--setter.go2
4 files changed, 5 insertions, 78 deletions
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)
}
}
}