36 lines
583 B
Go
36 lines
583 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"github.com/gorilla/sessions"
|
||
|
)
|
||
|
|
||
|
func Must[T any](value T, err error) T {
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return value
|
||
|
}
|
||
|
|
||
|
func GetValue[K any, T any](session *sessions.Session, key K) (T, bool) {
|
||
|
value, ok := session.Values[key]
|
||
|
if !ok && value == nil {
|
||
|
return *new(T), false
|
||
|
}
|
||
|
|
||
|
castedValue, ok := value.(T)
|
||
|
if !ok {
|
||
|
return *new(T), false
|
||
|
}
|
||
|
|
||
|
return castedValue, true
|
||
|
}
|
||
|
|
||
|
func GetValueDefault[K any, T any](session *sessions.Session, key K, defaultValue T) T {
|
||
|
v, ok := GetValue[K, T](session, key)
|
||
|
if !ok {
|
||
|
return defaultValue
|
||
|
}
|
||
|
|
||
|
return v
|
||
|
}
|