mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
refactor: enhance sorting logic in GetSortsFromString function
- Replaced JSON unmarshalling with string parsing to handle sorting parameters more flexibly. - Improved handling of sorting direction by allowing both ascending and descending specifications through prefixes. - Enhanced code readability and maintainability by simplifying the sorting logic and removing unnecessary error handling.
This commit is contained in:
@@ -191,8 +191,31 @@ func GetSortsFromString(sortStr string) (sorts []entity.Sort, err error) {
|
||||
if sortStr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if err := json.Unmarshal([]byte(sortStr), &sorts); err != nil {
|
||||
return nil, err
|
||||
parts := strings.Split(sortStr, ",")
|
||||
for _, part := range parts {
|
||||
trimmed := strings.TrimSpace(part)
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(trimmed, "-") {
|
||||
key := strings.TrimLeft(trimmed, "+")
|
||||
sorts = append(sorts, entity.Sort{
|
||||
Key: key,
|
||||
Direction: constants.DESCENDING,
|
||||
})
|
||||
} else if strings.HasPrefix(trimmed, "+") {
|
||||
key := strings.TrimLeft(trimmed, "+")
|
||||
sorts = append(sorts, entity.Sort{
|
||||
Key: key,
|
||||
Direction: constants.ASCENDING,
|
||||
})
|
||||
} else {
|
||||
key := strings.TrimLeft(trimmed, "-")
|
||||
sorts = append(sorts, entity.Sort{
|
||||
Key: key,
|
||||
Direction: constants.ASCENDING,
|
||||
})
|
||||
}
|
||||
}
|
||||
return sorts, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user