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

SQL-224 Reactions: encode condition names #361

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 29 additions & 10 deletions src/main/lrsql/ops/util/reaction.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

(defn- render-col
[bk condition-name col]
(bp/-snip-col bk {:col (format "%s.%s" condition-name col)}))
(bp/-snip-col bk {:col (format "%s.%s"
(ru/encode-condition-name condition-name)
col)}))

(s/fdef render-ref
:args (s/cat :bk rs/reaction-backend?
Expand All @@ -46,7 +48,7 @@
(render-col bk condition-name "registration")
(bp/-snip-json-extract
bk
{:col (format "%s.payload" condition-name)
{:col (format "%s.payload" (ru/encode-condition-name condition-name))
:path path
:datatype datatype})))

Expand Down Expand Up @@ -117,7 +119,9 @@
;; Clause special cases
"contains"
(bp/-snip-contains bk
{:col (format "%s.payload" condition-name)
{:col (format
"%s.payload"
(ru/encode-condition-name condition-name))
:path path
:right right-snip
:datatype (infer-type path val)})
Expand Down Expand Up @@ -150,8 +154,9 @@
[path ident-val] statement-identity]
(bp/-snip-clause
bk
{:left (render-ref bk condition-name path
:datatype (infer-type path ident-val))
{:left (render-ref
bk condition-name path
:datatype (infer-type path ident-val))
:op "="
:right (bp/-snip-val bk {:val ident-val})})))}))

Expand Down Expand Up @@ -204,11 +209,12 @@
bk
{:select (mapv
(fn [cn]
[(format "%s.payload" cn) cn])
(let [ecn (ru/encode-condition-name cn)]
[(format "%s.payload" ecn) ecn]))
condition-names)
:from (mapv
(fn [cn]
["xapi_statement" cn])
["xapi_statement" (ru/encode-condition-name cn)])
condition-names)
:where
(bp/-snip-and
Expand Down Expand Up @@ -236,9 +242,22 @@
(defn query-reaction
"For the given reaction input, return matching statements named for conditions."
[bk tx input]
(bp/-query-reaction bk tx
{:sql (query-reaction-sqlvec
bk input)}))
(mapv
(fn [row]
(reduce-kv
(fn [m k v]
(assoc m
(-> k
name
ru/decode-condition-name
keyword)
v))
{}
row))
(bp/-query-reaction
bk tx
{:sql (query-reaction-sqlvec
bk input)})))

(s/fdef query-active-reactions
:args (s/cat :bx rs/reaction-backend?
Expand Down
29 changes: 28 additions & 1 deletion src/main/lrsql/util/reaction.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[lrsql.spec.common :as cs]
[lrsql.spec.reaction :as rs]
[lrsql.spec.statement :as ss]
[xapi-schema.spec :as xs]))
[xapi-schema.spec :as xs]
[buddy.core.codecs :as bc]
[buddy.core.codecs.base64 :as b64]))

(s/fdef path->string
:args (s/cat :path ::rs/path
Expand Down Expand Up @@ -116,3 +118,28 @@
(-> input
(dissoc :reactionId)
(assoc :reaction-id reactionId)))

(s/fdef encode-condition-name
:args (s/cat :condition-name ::rs/condition-name)
:ret string?)

(defn encode-condition-name
"Given a condition name string, encode it as hex."
[condition-name]
(-> condition-name
b64/encode
bc/bytes->hex
(->> (format "cond_%s")))) ;; add prefix to be valid SQL colname
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahahaha - good way to ensure beginning character compatibility i guess

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷


(s/fdef decode-condition-name
:args (s/cat :encoded-condition-name string?)
:ret ::rs/condition-name)

(defn decode-condition-name
"Convert a condition name back to human-readable"
[encoded-condition-name]
(-> encoded-condition-name
(subs 5) ;; remove prefix
bc/hex->bytes
b64/decode
bc/bytes->str))
14 changes: 14 additions & 0 deletions src/test/lrsql/util/reaction_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,17 @@
{:data {:type :lrsql.util.reaction/invalid-path
:path ["completed_a" "actor" "birthday"]}
:cause "No value found at [\"completed_a\" \"actor\" \"birthday\"]"}))))

(deftest encode-condition-name-test
(testing "unique"
(is (= (r/encode-condition-name "foo")
(r/encode-condition-name "foo")))
(is (not= (r/encode-condition-name "foo")
(r/encode-condition-name "bar"))))
(testing "alphanumeric valid col name"
(is (re-matches #"[a-z][a-z0-9\_]*" (r/encode-condition-name "foo"))))
(testing "reversible"
(is (-> "foo"
r/encode-condition-name
r/decode-condition-name
(= "foo")))))
Loading