package sushi import ( "strconv" "strings" ) type FormValue struct { value string exists bool } // Form gets a form field for chaining func (ctx Ctx) Form(key string) FormValue { value := string(ctx.PostArgs().Peek(key)) exists := ctx.PostArgs().Has(key) return FormValue{value: value, exists: exists} } // String returns the value as string func (f FormValue) String() string { return f.value } // StringDefault returns string with default value func (f FormValue) StringDefault(defaultValue string) string { if f.value == "" { return defaultValue } return f.value } // Int returns the value as integer func (f FormValue) Int() int { if f.value == "" { return 0 } if parsed, err := strconv.Atoi(f.value); err == nil { return parsed } return 0 } // IntDefault returns integer with default value func (f FormValue) IntDefault(defaultValue int) int { if f.value == "" { return defaultValue } if parsed, err := strconv.Atoi(f.value); err == nil { return parsed } return defaultValue } // Float returns the value as float64 func (f FormValue) Float() float64 { if f.value == "" { return 0.0 } if parsed, err := strconv.ParseFloat(f.value, 64); err == nil { return parsed } return 0.0 } // FloatDefault returns float64 with default value func (f FormValue) FloatDefault(defaultValue float64) float64 { if f.value == "" { return defaultValue } if parsed, err := strconv.ParseFloat(f.value, 64); err == nil { return parsed } return defaultValue } // Bool returns the value as boolean func (f FormValue) Bool() bool { value := strings.ToLower(f.value) return value == "true" || value == "on" || value == "1" || value == "yes" } // BoolDefault returns boolean with default value func (f FormValue) BoolDefault(defaultValue bool) bool { if f.value == "" { return defaultValue } return f.Bool() } // Exists returns true if the field was present in the form func (f FormValue) Exists() bool { return f.exists } // IsEmpty returns true if the field is empty or doesn't exist func (f FormValue) IsEmpty() bool { return f.value == "" } // GetFormArray gets multiple form values as string slice func (ctx Ctx) GetFormArray(key string) []string { var values []string for k, v := range ctx.PostArgs().All() { if string(k) == key { values = append(values, string(v)) } } return values }