mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
Sessions are now in full sync with the registered database, on acquire(init), set, get, delete, clear, visit, len, release(destroy) as requested by almost everyone. https://github.com/kataras/iris/issues/969
Former-commit-id: 49fcdb93106a78f0a24ad3fb4d8725e35e98451a
This commit is contained in:
49
sessions/transcoding.go
Normal file
49
sessions/transcoding.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package sessions
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type (
|
||||
// Marshaler is the common marshaler interface, used by transcoder.
|
||||
Marshaler interface {
|
||||
Marshal(interface{}) ([]byte, error)
|
||||
}
|
||||
// Unmarshaler is the common unmarshaler interface, used by transcoder.
|
||||
Unmarshaler interface {
|
||||
Unmarshal([]byte, interface{}) error
|
||||
}
|
||||
// Transcoder is the interface that transcoders should implement, it includes just the `Marshaler` and the `Unmarshaler`.
|
||||
Transcoder interface {
|
||||
Marshaler
|
||||
Unmarshaler
|
||||
}
|
||||
)
|
||||
|
||||
// DefaultTranscoder is the default transcoder across databases, it's the JSON by default.
|
||||
// Change it if you want a different serialization/deserialization inside your session databases (when `UseDatabase` is used).
|
||||
var DefaultTranscoder = defaultTranscoder{}
|
||||
|
||||
type defaultTranscoder struct{}
|
||||
|
||||
func (d defaultTranscoder) Marshal(value interface{}) ([]byte, error) {
|
||||
if tr, ok := value.(Marshaler); ok {
|
||||
return tr.Marshal(value)
|
||||
}
|
||||
|
||||
if jsonM, ok := value.(json.Marshaler); ok {
|
||||
return jsonM.MarshalJSON()
|
||||
}
|
||||
|
||||
return json.Marshal(value)
|
||||
}
|
||||
|
||||
func (d defaultTranscoder) Unmarshal(b []byte, outPtr interface{}) error {
|
||||
if tr, ok := outPtr.(Unmarshaler); ok {
|
||||
return tr.Unmarshal(b, outPtr)
|
||||
}
|
||||
|
||||
if jsonUM, ok := outPtr.(json.Unmarshaler); ok {
|
||||
return jsonUM.UnmarshalJSON(b)
|
||||
}
|
||||
|
||||
return json.Unmarshal(b, outPtr)
|
||||
}
|
||||
Reference in New Issue
Block a user