From 478dd0216e7244b650129fa15489d23f4993e927 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Thu, 24 Oct 2024 20:28:31 -0300 Subject: [PATCH] feat(cookies): MarshalToWriter helper/alias function --- handlers/pages/dashboard.templ | 4 +--- lib/cookies/cookies.go | 9 +++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/handlers/pages/dashboard.templ b/handlers/pages/dashboard.templ index b7d7f27..6e417fd 100644 --- a/handlers/pages/dashboard.templ +++ b/handlers/pages/dashboard.templ @@ -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 { diff --git a/lib/cookies/cookies.go b/lib/cookies/cookies.go index 645046e..83661bb 100644 --- a/lib/cookies/cookies.go +++ b/lib/cookies/cookies.go @@ -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)