on the way to recursion
This commit is contained in:
parent
a612f070db
commit
d838ab5967
1 changed files with 19 additions and 11 deletions
|
@ -1,22 +1,30 @@
|
||||||
(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 serialize-element [elem]
|
(defn normalize-value [v]
|
||||||
(if (and (sh/is-functional-property? (key elem))
|
(cond (map? v)
|
||||||
(sequential? (val elem)))
|
(walk/walk normalize-functional-property identity v)
|
||||||
{(key elem) (first (val elem))}
|
(sequential? v)
|
||||||
{(key elem) (val elem)}))
|
(map normalize-value v)
|
||||||
|
: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
|
(merge
|
||||||
serialize-element
|
{(keyword "@context") "https://www.w3.org/ns/activitystreams"}
|
||||||
(merge
|
(walk/walk normalize-functional-property identity as-map))
|
||||||
{(keyword "@context") "https://www.w3.org/ns/activitystreams"}
|
|
||||||
as-map))
|
|
||||||
:escape-slash false))
|
:escape-slash false))
|
Loading…
Reference in a new issue