From 49bd3e0e0117768138f47f0c97accece15605025 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Thu, 10 Apr 2025 21:33:41 +0200 Subject: extract value extracting and convertion to functions; support uuid --- hh.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'hh.go') diff --git a/hh.go b/hh.go index 16c25d2..84df871 100644 --- a/hh.go +++ b/hh.go @@ -1 +1,44 @@ package hh + +import ( + "net/http" + "strconv" + + "github.com/google/uuid" +) + +func ExtractFromForm(r *http.Request, name string) (string, bool) { + value := r.Form[name] + if len(value) == 0 { + return "", true + } + return value[0], false +} + +func ExtractFromPath(r *http.Request, name string) (string, bool) { + value := r.PathValue(name) + if value == "" { + return "", true + } + return value, false +} + +func ExtractFromCookie(r *http.Request, name string) (string, bool) { + value, err := r.Cookie(name) + if err != nil { + return "", true + } + return value.Value, false +} + +func ConvertToInt(value string) (int, error) { + return strconv.Atoi(value) +} + +func ConvertToString(value string) (string, error) { + return value, nil +} + +func ConvertToUuidUUID(value string) (uuid.UUID, error) { + return uuid.Parse(value) +} -- cgit v1.2.3