MCPcopy
hub / github.com/gofiber/fiber / parseSortQuery

Function parseSortQuery

middleware/paginate/paginate.go:96–127  ·  view source on GitHub ↗
(query string, allowedSorts []string, defaultSort string)

Source from the content-addressed store, hash-verified

94}
95
96func parseSortQuery(query string, allowedSorts []string, defaultSort string) []SortField {
97 if query == "" {
98 return []SortField{{Field: defaultSort, Order: ASC}}
99 }
100
101 fields := strings.Split(query, ",")
102 sortFields := make([]SortField, 0, len(fields))
103
104 for _, field := range fields {
105 field = utils.TrimSpace(field)
106 if field == "" {
107 continue
108 }
109 order := ASC
110 if strings.HasPrefix(field, "-") {
111 order = DESC
112 field = utils.TrimSpace(field[1:])
113 }
114 if field == "" {
115 continue
116 }
117 if slices.Contains(allowedSorts, field) {
118 sortFields = append(sortFields, SortField{Field: field, Order: order})
119 }
120 }
121
122 if len(sortFields) == 0 {
123 return []SortField{{Field: defaultSort, Order: ASC}}
124 }
125
126 return sortFields
127}

Callers 2

Test_ParseSortQueryFunction · 0.85
NewFunction · 0.85

Calls 1

ContainsMethod · 0.80

Tested by 1

Test_ParseSortQueryFunction · 0.68