feat(cookies): MarshalToWriter helper/alias function

This commit is contained in:
Guz
2024-10-24 20:28:31 -03:00
parent c27b0a4e12
commit 478dd0216e
2 changed files with 10 additions and 3 deletions

View File

@@ -39,10 +39,8 @@ func (p *Dashboard) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Print(hasCookie, c)
if ck, err := cookies.Marshal(c); err != nil {
if err := cookies.MarshalToWriter(c, w); err != nil {
rerrors.InternalError(err).ServeHTTP(w, r)
} else {
http.SetCookie(w, ck)
}
if err := p.Component().Render(r.Context(), w); err != nil {

View File

@@ -39,6 +39,15 @@ func Marshal(v any) (*http.Cookie, error) {
return c, err
}
func MarshalToWriter(v any, w http.ResponseWriter) error {
if ck, err := Marshal(v); err != nil {
return err
} else {
http.SetCookie(w, ck)
}
return nil
}
func Unmarshal(c *http.Cookie, v any) error {
if m, ok := v.(Unmarshaler); ok {
return m.UnmarshalCookie(c)