on the way to recursion

This commit is contained in:
Michael Jerger 2023-08-02 10:50:55 +02:00
parent a612f070db
commit d838ab5967

View file

@ -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
serialize-element
(merge (merge
{(keyword "@context") "https://www.w3.org/ns/activitystreams"} {(keyword "@context") "https://www.w3.org/ns/activitystreams"}
as-map)) (walk/walk normalize-functional-property identity as-map))
:escape-slash false)) :escape-slash false))