Compare commits

..

No commits in common. "e44bdc2761488c253effcd787ee28d4add6c1285" and "a612f070dbc30fadd180346642bd3905a7d336e5" have entirely different histories.

4 changed files with 15 additions and 31 deletions

View file

@ -1,30 +1,22 @@
(ns org.domaindrivenarchitecture.activity-pub-poc.core (ns org.domaindrivenarchitecture.activity-pub-poc.core
(:require [clojure.spec.alpha :as s] (:require [clojure.spec.alpha :as s]
[clojure.walk :as walk]
[orchestra.core :refer [defn-spec]] [orchestra.core :refer [defn-spec]]
[org.domaindrivenarchitecture.activity-pub-poc.spec-helper :as sh] [org.domaindrivenarchitecture.activity-pub-poc.spec-helper :as sh]
[org.domaindrivenarchitecture.activity-pub-poc.activitystreams2 :as as]
[clojure.data.json :as json])) [clojure.data.json :as json]))
(defn normalize-value [v] ( defn serialize-element [elem]
(cond (map? v) (if (and (sh/is-functional-property? (key elem))
(walk/walk normalize-functional-property identity v) (sequential? (val elem)))
(sequential? v) {(key elem) (first (val elem))}
(map normalize-value v) {(key elem) (val elem)}))
:else v))
(defn normalize-functional-property [elem]
(let [k (first elem)
v (second elem)]
(cond (and
(sequential? v)
(sh/is-functional-property? k))
{k (first (normalize-value v))}
:else {k (normalize-value v)})))
(defn serialize (defn serialize
[as-map] [as-map]
(json/write-str (json/write-str
(map
serialize-element
(merge (merge
{(keyword "@context") "https://www.w3.org/ns/activitystreams"} {(keyword "@context") "https://www.w3.org/ns/activitystreams"}
(walk/walk normalize-functional-property identity as-map)) as-map))
:escape-slash false)) :escape-slash false))

View file

@ -17,8 +17,4 @@
is-functional-property? boolean? is-functional-property? boolean?
"Checks whether spec is a FunctionalProperty." "Checks whether spec is a FunctionalProperty."
[spec s/spec?] [spec s/spec?]
(if ( s/get-spec spec) (some #(clojure.string/includes? % "FunctionalProperty") (s/describe spec)))
(some
#(clojure.string/includes? % "FunctionalProperty")
(s/describe spec))
false))

View file

@ -3,16 +3,14 @@
[clojure.test :refer [deftest is are testing run-tests]] [clojure.test :refer [deftest is are testing run-tests]]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.string :as str] [clojure.string :as str]
[org.domaindrivenarchitecture.activity-pub-poc.activitystreams2 :as as]
[org.domaindrivenarchitecture.activity-pub-poc.core :as sut])) [org.domaindrivenarchitecture.activity-pub-poc.core :as sut]))
(deftest should-serialize (deftest should-serailize
(is (= (str "{\"@context\":\"https://www.w3.org/ns/activitystreams\"," (is (= (str "{\"@context\":\"https://www.w3.org/ns/activitystreams\","
"\"id\":\"http://example.org/likes/1\"," "\"id\":\"http://example.org/likes/1\","
"\"summary\":\"Sally liked a note\"," "\"summary\":\"Sally liked a note\","
"\"type\":\"Like\"," "\"type\":\"Like\","
"\"actor\":{" "\"actor\":{"
"\"id\":\"http://example.org/persons/1\","
"\"type\":\"Person\"," "\"type\":\"Person\","
"\"name\":\"Sally\"}," "\"name\":\"Sally\"},"
"\"object\":\"http://example.org/notes/1\"}") "\"object\":\"http://example.org/notes/1\"}")
@ -20,7 +18,6 @@
{::as/id ["http://example.org/likes/1"] {::as/id ["http://example.org/likes/1"]
::as/summary "Sally liked a note", ::as/summary "Sally liked a note",
::as/type "Like", ::as/type "Like",
::as/actor {::as/id ["http://example.org/persons/1"] ::as/actor {::as/type "Person",
::as/type "Person",
::as/name "Sally"}, ::as/name "Sally"},
::as/object "http://example.org/notes/1"})))) ::as/object "http://example.org/notes/1"}))))

View file

@ -8,5 +8,4 @@
(deftest shoult-test-spec-for-having-functional-property (deftest shoult-test-spec-for-having-functional-property
(is (sut/is-functional-property? ::as2/id)) (is (sut/is-functional-property? ::as2/id))
(is (not (sut/is-functional-property? ::as2/Like))) (is (not (sut/is-functional-property? ::as2/Like))))
(is (not (sut/is-functional-property? ::as2/NotExisting))))