-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddba8b6
commit 4cd374b
Showing
4 changed files
with
113 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package scs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
"time" | ||
) | ||
|
||
// Codec is the interface for encoding/decoding session data to and from a byte | ||
// slice for use by the session store. | ||
type Codec interface { | ||
Encode(deadline time.Time, values map[string]interface{}) ([]byte, error) | ||
Decode([]byte) (deadline time.Time, values map[string]interface{}, err error) | ||
} | ||
|
||
type gobCodec struct{} | ||
|
||
func (gobCodec) Encode(deadline time.Time, values map[string]interface{}) ([]byte, error) { | ||
aux := &struct { | ||
Deadline time.Time | ||
Values map[string]interface{} | ||
}{ | ||
Deadline: deadline, | ||
Values: values, | ||
} | ||
|
||
var b bytes.Buffer | ||
err := gob.NewEncoder(&b).Encode(&aux) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return b.Bytes(), nil | ||
} | ||
|
||
func (gobCodec) Decode(b []byte) (time.Time, map[string]interface{}, error) { | ||
aux := &struct { | ||
Deadline time.Time | ||
Values map[string]interface{} | ||
}{} | ||
|
||
r := bytes.NewReader(b) | ||
err := gob.NewDecoder(r).Decode(&aux) | ||
if err != nil { | ||
return time.Time{}, nil, err | ||
} | ||
|
||
return aux.Deadline, aux.Values, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.