Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode-base64-encoded-vals #83

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/gosura/helpers/relay.clj
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,42 @@
{}
arguments)))

(defmulti decode-all-global-ids
(fn [arguments]
(type arguments)))

(defmethod decode-all-global-ids clojure.lang.PersistentVector
[args]
(reduce-kv (fn [acc k v]
(assoc acc k (decode-all-global-ids v)))
[]
args))
namenu marked this conversation as resolved.
Show resolved Hide resolved

(defmethod decode-all-global-ids clojure.lang.PersistentList
[args]
(reduce-kv (fn [acc k v]
(assoc acc k (decode-all-global-ids v)))
'()
args))
namenu marked this conversation as resolved.
Show resolved Hide resolved

(defmethod decode-all-global-ids clojure.lang.IPersistentMap
[args]
(reduce-kv (fn [acc k v]
(assoc acc k (decode-all-global-ids v)))
{}
args))
namenu marked this conversation as resolved.
Show resolved Hide resolved

(defmethod decode-all-global-ids java.lang.String
[v]
(try
(decode-global-id->db-id v)
(catch Exception _
v)))

(defmethod decode-all-global-ids :default
[v]
v)

(comment
(decode-cursor "TlBZAHFkAW4BZAE=") ;; => {:id 1, :ordered-values [1]})
(decode-cursor "b2Zmc2V0OjM=") ;; => {:offset "3"}
Expand Down
7 changes: 5 additions & 2 deletions src/gosura/helpers/resolver2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@
- :return-camel-case? - 반환값을 camelCase 로 변환할지 설정합니다. (기본값 true)
- :required-keys-in-parent - 부모(hash-map)로부터 필요한 required keys를 설정합니다.
- :decode-ids-by-keys - 키 목록을 받아서 resolver args의 global id들을 db id로 변환 해줍니다.
- :decode-base64-encoded-vals - base64 인코딩된 값을 전부 디코딩하고 id값을 반환합니다.
namenu marked this conversation as resolved.
Show resolved Hide resolved
- :filters - args에 추가할 key-value 값을 필터로 넣습니다.
"
[{:keys [this ctx arg parent]} option args body]
(let [{:keys [auth kebab-case? return-camel-case? required-keys-in-parent filters decode-ids-by-keys]
(let [{:keys [auth kebab-case? return-camel-case? required-keys-in-parent filters decode-ids-by-keys decode-base64-encoded-vals]
:or {kebab-case? true
return-camel-case? true
required-keys-in-parent []}} option
required-keys-in-parent []
decode-base64-encoded-vals true}} option
result (gensym 'result_)
auth-filter-opts `(auth/->auth-result ~auth ~ctx)
config-filter-opts `(auth/config-filter-opts ~filters ~ctx)
arg `(merge ~arg ~auth-filter-opts ~config-filter-opts)
arg' (if kebab-case? `(transform-keys->kebab-case-keyword ~arg) arg)
arg' (if decode-ids-by-keys `(relay/decode-global-ids-by-keys ~arg' ~decode-ids-by-keys) arg')
arg' (if decode-base64-encoded-vals `(relay/decode-all-global-ids ~arg') arg')
parent' (if kebab-case? `(transform-keys->kebab-case-keyword ~parent) parent)
keys-not-found `(keys-not-found ~parent' ~required-keys-in-parent)
params (if (nil? this) [ctx arg' parent'] [this ctx arg' parent'])
Expand Down
41 changes: 38 additions & 3 deletions test/gosura/helpers/relay_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,43 @@
:b "bm90aWNlOjEwMA=="
:c {:d ["bm90aWNlOjEwMA==" "bm90aWNlOjEwMA=="]}}
[:a :d])
{:a "100", :b "bm90aWNlOjEwMA==", :c {:d ["100" "100"]}}))))
{:a "100"
:b "bm90aWNlOjEwMA=="
:c {:d ["100" "100"]}}))))

(deftest decode-all-global-ids
(testing "decode key"
(is (= (gosura-relay/decode-all-global-ids {:a "bm90aWNlOjEwMA=="})
{:a "100"})))
(testing "decode vector"
(is (= (gosura-relay/decode-all-global-ids ["bm90aWNlOjEwMA==" "bm90aWNlOjEwMA=="])
["100" "100"])))
(testing "decode vector in map"
(is (= (gosura-relay/decode-all-global-ids {:a ["bm90aWNlOjEwMA==" "bm90aWNlOjEwMA=="]})
{:a ["100" "100"]})))
(testing "nested"
(is (= (gosura-relay/decode-all-global-ids {:a "bm90aWNlOjEwMA=="
:b "bm90aWNlOjEwMA=="
:c {:d ["bm90aWNlOjEwMA==" "bm90aWNlOjEwMA=="]}})
{:a "100"
:b "100"
:c {:d ["100" "100"]}})))
(testing "not encoded string is okay"
(is (= (gosura-relay/decode-all-global-ids {:a "bm90aWNlOjEwMA=="
:b "100"
:c {:d ["bm90aWNlOjEwMA==" "bm90aWNlOjEwMA=="]}})
{:a "100"
:b "100"
:c {:d ["100" "100"]}})))
(testing "keyword and number is okay"
(is (= (gosura-relay/decode-all-global-ids {:a "bm90aWNlOjEwMA=="
:b 100
:f :100
:c {:d ["bm90aWNlOjEwMA==" "bm90aWNlOjEwMA=="]}})
{:a "100"
:b 100
:f :100
:c {:d ["100" "100"]}}))))

(comment
(run-tests)
)
(run-tests))