Created
September 27, 2019 13:57
-
-
Save eraserhd/263b2ecc859714baa9d82d8aa78ddeef to your computer and use it in GitHub Desktop.
Another win for meander
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- schema->type | |
[schema] | |
(let [types (concat | |
(m/search schema | |
{"format" "date-time"} :db.type/instant | |
{"format" "uri"} :db.type/uri | |
{"type" "array"} '(Array nil) | |
{"type" "boolean"} :db.type/boolean | |
{"type" "integer"} :db.type/long | |
{"type" "string"} :db.type/string | |
{"items" (m/pred some? ?item)} (list 'Array (schema->type ?item)) | |
{"$ref" (m/pred some? ?ref)} (if (= ?ref "#uuid") | |
:db.type/uuid | |
(subs ?ref 1))) | |
(map schema->type (get schema "allOf")))] | |
(reduce narrower-type nil types))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- schema->type | |
[schema] | |
(let [types (concat | |
(case (get schema "type") | |
nil [] | |
"boolean" [:db.type/boolean] | |
"string" [:db.type/string] | |
"integer" [:db.type/long] | |
"array" ['(Array nil)]) | |
(case (get schema "format") | |
"date-time" [:db.type/instant] | |
[]) | |
(when-let [items (get schema "items")] | |
[(list 'Array (schema->type items))]) | |
(let [$ref (get schema "$ref")] | |
(cond | |
(nil? $ref) [] | |
(= $ref "#uuid") [:db.type/uuid] | |
:else [(subs $ref 1)])) | |
(map schema->type (get schema "allOf")))] | |
(reduce narrower-type nil types))) |
Note, I didn't put an example of the version which uses m/cata
because its effectively the same but just a bit more noisy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@eraserhd Here's an example of the macro expansion in the first case but with a small tweak on that
"$ref"
key usingepsilon
version0.0.221
.I'm fairly sure this macro expansion is superior to previous versions.