LEFT (s/def :db1/orders (s/coll-of (s/tuple #{:title :created :status} #{:+ :-}))) (defn orders->map [orders] (when-not (s/valid? :db1/orders orders) (throw (ex-info (s/explain-data :db1/orders orders)))) (->> orders (mapv #(let [[field order-dir] %] [field (case order-dir :+ :asc :- :desc)])) (array-map :order-by))) (deftest generate-orders-maps (is (= {:order-by []} (orders->map []))) (is (= {:order-by [[:title :desc]]} (orders->map [[:title :-]]))) (is (= {:order-by [[:status :asc]]} (orders->map [[:status :+]])))) RIGHT func buildOrders(builder *squirrel.SelectBuilder, orders []entity.Order) error { for _, order := range orders { var fieldName string switch order.Field { case entity.Title: fieldName = "title" case entity.Date: fieldName = "created" case entity.Status: fieldName = "status" default: return fmt.Errorf("invalid field: %d", order.Field) } var sortOrder string switch order.SortingOrder { case entity.ASC: sortOrder = "ASC" case entity.DESC: sortOrder = "DESC" default: return fmt.Errorf("invalid sorting order: %d", order.SortingOrder) } orderExpr := fieldName + " " + sortOrder *builder = builder.OrderBy(orderExpr) } return nil }
https://storage.mstdn.in.th/media_attachments/files/112/583/203/414/255/902/original/c571add5bfaaf23a.png