Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@geraldodev
Last active April 23, 2020 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraldodev/f637da76bdac78fe8f7e330db43fe311 to your computer and use it in GitHub Desktop.
Save geraldodev/f637da76bdac78fe8f7e330db43fe311 to your computer and use it in GitHub Desktop.
(ns app.malli-teste
(:require
[clojure.zip :as z]
[clojure.pprint :refer [pprint]]
[malli.core :as m]
[malli.util :as mu]
[malli.error :as me]
))
(defn make-malli-node
[n c]
(m/into-schema (m/name n) (m/properties n) c))
(defn have-children?
[s]
(when (vector? s)
(when-let [tag (first s)]
(when (get m/base-registry tag)
true))))
(defn malli-map?
[s]
(when (vector? s)
(let [tag (first s)]
(= :map tag))))
(defn malli-map-entry?
[x]
(and (vector? x)
(<= 2 (count x) 3)))
(defn safe-children
[s]
(try
(m/children s)
(catch Exception _
(if (and (malli-map-entry? s)
(have-children? (last s)))
(do
(prn "map item with children " (first s))
(last s))
(do
(prn "no children for " s)
nil)))))
(defn malli-branch?
[x]
(if (malli-map-entry? x)
true
(safe-children x)))
(defn malli-zipper
[root]
(z/zipper malli-branch? safe-children make-malli-node root))
(defn ride-malli-zipper
[schema]
(loop [loc (malli-zipper schema)]
(if (z/end? loc)
(println "end")
(let [n (z/node loc)
n (try
(m/name n)
(catch Exception e
(cond (sequential? n)
(first n)
(keyword? n)
(str n " keyword")
(symbol? n)
(str n " symbol")
:else "no name")
))]
(println n)
(recur (z/next loc) )))))
(def Schema
(m/schema
[:and
[:map
{:foo :bar}
[:nome string?]
[:sexo string? [:enum "masculino" "feminino"]]
[:endereco [:map
[:cep string?]
[:estado string?]
[:telefone {:optional true} string?]] ]
]
[:fn 'identity]
]))
(comment
(ride-malli-zipper Schema)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment