hossein1376 / grape

Modern, zero-dependency HTTP library for Go
https://pkg.go.dev/github.com/hossein1376/grape
MIT License
149 stars 5 forks source link

query param #8

Open mybigman opened 1 week ago

mybigman commented 1 week ago

Since you have the following helpers

h.ParamInt()
h.ParamInt64()

Could you add say

h.Query() // or
h.QueryParam() // or
h.ParamQuery()

Thanks

hossein1376 commented 1 week ago

It's a really good idea. I'm a little busy now, but I'll look into it at the first chance.

mybigman commented 3 days ago

i created this... feel free to use it

func GetQueryParamWithDefault[T any](r *http.Request, key string, val T) T {
    value := r.URL.Query().Get(key)
    if value == "" {
        return val
    }

    switch any(val).(type) {
    case int:
        if intValue, err := strconv.Atoi(value); err == nil {
            return any(intValue).(T)
        }
    case float64:
        if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
            return any(floatValue).(T)
        }
    case string:
        return any(value).(T)
    }

    return val
}
hossein1376 commented 1 day ago

Appreciate it. Have you considered making pull requests?

mybigman commented 1 day ago

I will when I am feeling better, everyone in our house hold are so sick :(

hossein1376 commented 1 day ago

I hope you're getting well soon.