From 7ae7c9b07068864db48f42d21b0c29cacba80e81 Mon Sep 17 00:00:00 2001 From: Clemens Date: Wed, 2 Aug 2023 12:18:12 +0200 Subject: [PATCH] Added normalize --- doc/Translate_ttl_to_spec.md | 2 +- .../activity_pub_poc/core.cljc | 15 ++++- .../activity_pub_poc/core_test.cljc | 56 +++++++++++++------ 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/doc/Translate_ttl_to_spec.md b/doc/Translate_ttl_to_spec.md index 80e61ae..46465f0 100644 --- a/doc/Translate_ttl_to_spec.md +++ b/doc/Translate_ttl_to_spec.md @@ -54,7 +54,7 @@ Maps to (s/def ::id (s/and ... - ::xsd/anyURI)) + (sh/seq-spec ::xsd/anyURI))) ``` ## **rdfs:domain** diff --git a/src/main/cljc/org/domaindrivenarchitecture/activity_pub_poc/core.cljc b/src/main/cljc/org/domaindrivenarchitecture/activity_pub_poc/core.cljc index 1119898..c48480f 100644 --- a/src/main/cljc/org/domaindrivenarchitecture/activity_pub_poc/core.cljc +++ b/src/main/cljc/org/domaindrivenarchitecture/activity_pub_poc/core.cljc @@ -24,12 +24,21 @@ {k (first (normalize-value v))} :else {k (normalize-value v)}))) -(defn-spec serialize string? +(defn-spec normalize string? [spec s/spec? as-map map?] - (let [speced-map (assert (s/valid? spec as-map))] + (let [normalized-map (walk/walk normalize-functional-property identity as-map)] + (do + (assert (s/valid? spec normalized-map)) + normalized-map))) + +(defn-spec serialize string? + [spec s/spec? + normalized-map map?] + (do + (assert (s/valid? spec normalized-map)) (json/write-str (merge {(keyword "@context") "https://www.w3.org/ns/activitystreams"} - (walk/walk normalize-functional-property identity speced-map)) + normalized-map) :escape-slash false))) \ No newline at end of file diff --git a/src/test/cljc/org/domaindrivenarchitecture/activity_pub_poc/core_test.cljc b/src/test/cljc/org/domaindrivenarchitecture/activity_pub_poc/core_test.cljc index eb06645..f91920d 100644 --- a/src/test/cljc/org/domaindrivenarchitecture/activity_pub_poc/core_test.cljc +++ b/src/test/cljc/org/domaindrivenarchitecture/activity_pub_poc/core_test.cljc @@ -6,22 +6,42 @@ [org.domaindrivenarchitecture.activity-pub-poc.activitystreams2 :as as] [org.domaindrivenarchitecture.activity-pub-poc.core :as sut])) +(def tmp ) + +(deftest should-normalize + (is (= {::as/id "http://example.org/likes/1" + ::as/summary "Sally liked a note", + ::as/type ["Like"], + ::as/actor {::as/id "http://example.org/persons/1" + ::as/type "Person", + ::as/name "Sally"}, + ::as/object "http://example.org/notes/1"} + (sut/normalize + ::as/Like + {::as/id ["http://example.org/likes/1"] + ::as/summary "Sally liked a note", + ::as/type ["Like"], + ::as/actor {::as/id ["http://example.org/persons/1"] + ::as/type "Person", + ::as/name "Sally"}, + ::as/object "http://example.org/notes/1"})))) + (deftest should-serialize - (is (= (str "{\"@context\":\"https://www.w3.org/ns/activitystreams\"," - "\"id\":\"http://example.org/likes/1\"," - "\"summary\":\"Sally liked a note\"," - "\"type\":\"Like\"," - "\"actor\":{" - "\"id\":\"http://example.org/persons/1\"," - "\"type\":\"Person\"," - "\"name\":\"Sally\"}," - "\"object\":\"http://example.org/notes/1\"}") - (sut/serialize - ::as/Like - {::as/id ["http://example.org/likes/1"] - ::as/summary "Sally liked a note", - ::as/type "Like", - ::as/actor {::as/id ["http://example.org/persons/1"] - ::as/type "Person", - ::as/name "Sally"}, - ::as/object "http://example.org/notes/1"})))) \ No newline at end of file + (is (= (str "{\"@context\":\"https://www.w3.org/ns/activitystreams\"," + "\"id\":\"http://example.org/likes/1\"," + "\"summary\":\"Sally liked a note\"," + "\"type\":[\"Like\"]," + "\"actor\":{" + "\"id\":\"http://example.org/persons/1\"," + "\"type\":\"Person\"," + "\"name\":\"Sally\"}," + "\"object\":\"http://example.org/notes/1\"}") + (sut/serialize + ::as/Like + {::as/id "http://example.org/likes/1" + ::as/summary "Sally liked a note", + ::as/type ["Like"], + ::as/actor {::as/id "http://example.org/persons/1" + ::as/type "Person", + ::as/name "Sally"}, + ::as/object "http://example.org/notes/1"})))) \ No newline at end of file