package api import ( "net/http" "strconv" ) // asString coerces an arbitrary JSON value to a string. func asString(v any) string { switch t := v.(type) { case string: return t case nil: return "" default: return "" } } // asInt coerces a JSON number/string to an int. func asInt(v any) int { switch t := v.(type) { case float64: return int(t) case int: return t case string: if n, err := strconv.Atoi(t); err == nil { return n } } return 0 } // asStringSlice coerces a JSON array (or single string) to []string. func asStringSlice(v any) []string { switch t := v.(type) { case []any: out := make([]string, 0, len(t)) for _, e := range t { if s, ok := e.(string); ok { out = append(out, s) } } return out case []string: return t case string: if t == "" { return nil } return []string{t} } return nil } // queryInt parses a query parameter as an int, falling back to def. func queryInt(r *http.Request, key string, def int) int { if v := r.URL.Query().Get(key); v != "" { if n, err := strconv.Atoi(v); err == nil { return n } } return def }