I'm parsing a form in Go and I frequently find groups of checkboxes which need to be processed into text like so:
[ ] Foo
[x] Bar
[ ] Baz
[x] Bat
where the output should be a comma-separated list "BarText, BatText" corresponding to the checked items, or "None" if none of the items are checked. What is a good way to handle this situation? Repeating the logic each time seems like a bad idea.
In the spirit of YAGNI there's no need to handle possible future changes like translations into other languages (actually this example is highly unlikely to be useful in the present context).
Efficiency is unimportant for this application.
Edit: code looks like this (source):
func handleCheckboxesForm(w http.ResponseWriter, r *http.Request) {
b := func(name string) string { // helper function for boolean values in the form
return r.FormValue(name) == "on"
}
text := "Header stuff here"
mytext := ""
if b("nfpa-alk") {
mytext += ", alkaline"
}
if b("nfpa-acid") {
mytext += ", acid"
}
if b("nfpa-w") {
mytext += ", reacts violently with water"
}
if b("nfpa-alk") || b("nfpa-acid") || b("nfpa-w") {
text += mytext[2:] + "
"
} else {
text += "none
"
}
// lots of other checkbox groups here
// do stuff with text
}