1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

organise sessions examples

Former-commit-id: 682472d2cf4ebfc740687522fe5eef77b5bb1a72
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-05-07 07:34:17 +03:00
parent b4365cee8d
commit cd62ba3712
11 changed files with 190 additions and 411 deletions

View File

@@ -30,6 +30,10 @@ func newProvider() *provider {
// RegisterDatabase sets a session database.
func (p *provider) RegisterDatabase(db Database) {
if db == nil {
return
}
p.mu.Lock() // for any case
p.db = db
p.mu.Unlock()

View File

@@ -5,8 +5,13 @@ import (
"encoding/gob"
"encoding/json"
"reflect"
"time"
)
func init() {
gob.Register(time.Time{})
}
type (
// Marshaler is the common marshaler interface, used by transcoder.
Marshaler interface {
@@ -23,6 +28,12 @@ type (
}
)
type (
defaultTranscoder struct{}
// GobTranscoder can be set to `DefaultTranscoder` to modify the database(s) transcoder.
GobTranscoder struct{}
)
var (
_ Transcoder = (*defaultTranscoder)(nil)
_ Transcoder = (*GobTranscoder)(nil)
@@ -53,12 +64,6 @@ var (
DefaultTranscoder Transcoder = defaultTranscoder{}
)
type (
defaultTranscoder struct{}
// GobTranscoder can be set to `DefaultTranscoder` to modify the database(s) transcoder.
GobTranscoder struct{}
)
func (defaultTranscoder) Marshal(value interface{}) ([]byte, error) {
if tr, ok := value.(Marshaler); ok {
return tr.Marshal(value)
@@ -91,17 +96,10 @@ func (GobTranscoder) Marshal(value interface{}) ([]byte, error) {
err error
)
switch v := value.(type) {
case reflect.Value:
if v, ok := value.(reflect.Value); ok {
err = enc.EncodeValue(v)
case string,
int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64,
complex64, complex128:
err = enc.Encode(&v)
default:
err = enc.Encode(value)
} else {
err = enc.Encode(&value)
}
if err != nil {
@@ -114,10 +112,7 @@ func (GobTranscoder) Marshal(value interface{}) ([]byte, error) {
// Unmarshal parses the gob-encoded data "b" and stores the result
// in the value pointed to by "outPtr".
func (GobTranscoder) Unmarshal(b []byte, outPtr interface{}) error {
var (
r = bytes.NewBuffer(b)
dec = gob.NewDecoder(r)
)
dec := gob.NewDecoder(bytes.NewBuffer(b))
if v, ok := outPtr.(reflect.Value); ok {
return dec.DecodeValue(v)