You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.3 KiB
Clojure
44 lines
1.3 KiB
Clojure
(ns org.domaindrivenarchitecture.fed-poc.core
|
|
(:require [clojure.spec.alpha :as s]
|
|
[clojure.walk :as walk]
|
|
[orchestra.core :refer [defn-spec]]
|
|
[org.domaindrivenarchitecture.fed-poc.spec-helper :as sh]
|
|
[clojure.data.json :as json]))
|
|
|
|
(declare normalize-functional-property)
|
|
|
|
(defn normalize-value [v]
|
|
(cond (map? v)
|
|
(walk/walk normalize-functional-property identity v)
|
|
(sequential? v)
|
|
(map normalize-value v)
|
|
:else v))
|
|
|
|
; TODO: Consider using tail recursion in case of prod usage
|
|
(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-spec normalize string?
|
|
[spec s/spec?
|
|
as-map 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"}
|
|
normalized-map)
|
|
:escape-slash false))) |