GNU social JP
  • FAQ
  • Login
GNU social JPは日本のGNU socialサーバーです。
Usage/ToS/admin/test/Pleroma FE
  • Public

    • Public
    • Network
    • Groups
    • Featured
    • Popular
    • People

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 }

Download link

https://storage.mstdn.in.th/media_attachments/files/112/583/203/414/255/902/original/c571add5bfaaf23a.png

Notices where this attachment appears

  1. Embed this notice
    veer66 (veer66@mstdn.in.th)'s status on Monday, 10-Jun-2024 04:43:44 JST veer66 veer66

    Prepare to generate SQL in #Clojure and #Golang

    Go's main advantage lies in its familiarity for programmers from various languages like Pascal, Java, JavaScript, Python, and C.

    Clojure Spec fits in data validation and generate an explanation as Exception's message.

    Moreover, instead of using the mutable builder pattern like a data mapper for Go, Clojure with HoneySQL uses plain data structure, so writing a unit test is simple.

    In conversation Monday, 10-Jun-2024 04:43:44 JST from mstdn.in.th permalink
  • Help
  • About
  • FAQ
  • TOS
  • Privacy
  • Source
  • Version
  • Contact

GNU social JP is a social network, courtesy of GNU social JP管理人. It runs on GNU social, version 2.0.2-dev, available under the GNU Affero General Public License.

Creative Commons Attribution 3.0 All GNU social JP content and data are available under the Creative Commons Attribution 3.0 license.