-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.clj
56 lines (46 loc) · 1.61 KB
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(ns com.github.ivarref.clj-paginate.impl.utils
(:require [clojure.edn :as edn]
[clojure.string :as str])
(:import (java.io StringWriter Writer)))
(defn maybe-decode-cursor [cursor]
(when cursor
(when (and (string? cursor)
(not-empty cursor)
(str/starts-with? cursor "{"))
(edn/read-string cursor))))
(defn get-cursor [opts]
(or
(let [cursor (or (get opts :after) (get opts :before))]
(when (and (string? cursor)
(not-empty cursor)
(str/starts-with? cursor "{"))
(edn/read-string cursor)))
{}))
(defn cursor-pre [cursor]
(let [s (pr-str (dissoc cursor :cursor))
s (subs s 0 (dec (count s)))]
(str s " :cursor [")))
(defn node-cursor [cursor-pre node sort-attrs]
(with-open [^Writer sw (StringWriter.)]
(.append sw ^String cursor-pre)
(doseq [attr sort-attrs]
(print-method (get node attr) sw)
(.append sw " "))
(.append sw "]}")
(.toString sw)))
(defn get-edges [nodes batch-f f sort-attrs cursor]
(let [nodes (vec nodes)]
(if (empty? nodes)
[]
(let [cursor-pre-str (cursor-pre cursor)
batch-nodes (batch-f nodes)]
(loop [i 0
res (transient [])]
(if (= i (count nodes))
(persistent! res)
(recur (inc i)
(conj! res
{:node (f (nth batch-nodes i))
:cursor (node-cursor cursor-pre-str
(nth nodes i)
sort-attrs)}))))))))