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