func ParseSearch(u *url.URL) (brew.Search, error, []string) { args := u.Query() search := brew.Search{} funcs := []func(*brew.Search, url.Values) error{ parseMulticolor, parseRarity, parseTypes, parseSupertypes, parseColors, parseSubtypes, parseFormat, parseStatus, parseMultiverseIDs, parseSets, parseName, parseRules, parsePaging, } var err error results := []string{} for _, fun := range funcs { if e := fun(&search, args); e != nil { results = append(results, e.Error()) err = fmt.Errorf("Errors while processing the search") } } // By default, include 100 cards search.Limit = 100 return search, err, results }
func parseMulticolor(s *brew.Search, args url.Values) error { switch args.Get("multicolor") { case "true": s.IncludeMulticolor = true s.Multicolor = true case "false": s.IncludeMulticolor = true s.Multicolor = false case "": s.IncludeMulticolor = false return nil default: return fmt.Errorf("Multicolor should be either 'true' or 'false'") } return nil }
func parseMultiverseIDs(s *brew.Search, args url.Values) error { ids := args["multiverseid"][:] for _, m := range args["m"] { ids = append(ids, m) } s.MultiverseIDs = ids return nil }
func parsePaging(s *brew.Search, args url.Values) error { s.Limit = 100 pagenum := args.Get("page") if pagenum == "" { return nil } page, err := strconv.Atoi(pagenum) if err != nil { return err } if page < 0 { return fmt.Errorf("Page parameter must be >= 0") } s.Page = page s.Offset = s.Page * s.Limit return nil }
// TODO: Add validation func parseSets(s *brew.Search, args url.Values) error { s.Sets = toLower(args["set"]) return nil }
// TODO: Add validation func parseSubtypes(s *brew.Search, args url.Values) error { s.Subtypes = toLower(args["subtype"]) return nil }