startpage/utils.go

36 lines
583 B
Go
Raw Normal View History

2022-09-05 14:17:07 +02:00
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
}