| 36 | } |
| 37 | |
| 38 | func parseOrderBy(orderBy string) ([]*OrderByKey, error) { |
| 39 | if orderBy == "" { |
| 40 | return nil, nil |
| 41 | } |
| 42 | |
| 43 | var result []*OrderByKey |
| 44 | re := regexp.MustCompile(`(\w+)\s*(asc|desc)?`) |
| 45 | matches := re.FindAllStringSubmatch(orderBy, -1) |
| 46 | for _, match := range matches { |
| 47 | if len(match) > 3 { |
| 48 | return nil, errors.Errorf("invalid order by %q", orderBy) |
| 49 | } |
| 50 | key := &OrderByKey{ |
| 51 | Key: match[1], |
| 52 | SortOrder: ASC, |
| 53 | } |
| 54 | if len(match) == 3 && match[2] == "desc" { |
| 55 | key.SortOrder = DESC |
| 56 | } |
| 57 | result = append(result, key) |
| 58 | } |
| 59 | return result, nil |
| 60 | } |