summaryrefslogtreecommitdiff
path: root/hh.go
blob: a8b7cd4df21ed6ee14223892845de39114917721 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package hh

import (
	"encoding/json"
	"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)
}

type ToResponse interface {
	Respond(w http.ResponseWriter, r *http.Request)
}

func JSON(a any) JSONValue {
	return JSONValue{a}
}

type JSONValue struct{ any }

func (j JSONValue) Respond(w http.ResponseWriter, r *http.Request) {
	if err := json.NewEncoder(w).Encode(j); err != nil {
		panic("todo: internal server error: " + err.Error())
	}
}