change :stringify-model-map-keys to something more all-encompassing
This commit is contained in:
parent
4315ddfa54
commit
99e9f91445
|
@ -1,5 +1,6 @@
|
||||||
(ns clj-jtwig.convert
|
(ns clj-jtwig.convert
|
||||||
"functions for converting data types both ways between clojure and java")
|
"functions for converting data types both ways between clojure and java"
|
||||||
|
(:use [clj-jtwig.options]))
|
||||||
|
|
||||||
(defprotocol JavaToClojure
|
(defprotocol JavaToClojure
|
||||||
(to-clojure [x]))
|
(to-clojure [x]))
|
||||||
|
@ -21,7 +22,12 @@
|
||||||
(.entrySet)
|
(.entrySet)
|
||||||
(reduce
|
(reduce
|
||||||
(fn [m [k v]]
|
(fn [m [k v]]
|
||||||
(assoc m (to-clojure k) (to-clojure v)))
|
(assoc m
|
||||||
|
(if (and (:auto-convert-map-keywords @options)
|
||||||
|
(string? k))
|
||||||
|
(keyword k)
|
||||||
|
(to-clojure k))
|
||||||
|
(to-clojure v)))
|
||||||
{})))
|
{})))
|
||||||
|
|
||||||
java.lang.Object
|
java.lang.Object
|
||||||
|
@ -45,7 +51,12 @@
|
||||||
(to-java [x]
|
(to-java [x]
|
||||||
(let [hashmap (new java.util.HashMap (count x))]
|
(let [hashmap (new java.util.HashMap (count x))]
|
||||||
(doseq [[k v] x]
|
(doseq [[k v] x]
|
||||||
(.put hashmap (to-java k) (to-java v)))
|
(.put hashmap
|
||||||
|
(if (and (:auto-convert-map-keywords @options)
|
||||||
|
(keyword? k))
|
||||||
|
(name k)
|
||||||
|
(to-java k))
|
||||||
|
(to-java v)))
|
||||||
hashmap))
|
hashmap))
|
||||||
|
|
||||||
clojure.lang.IPersistentCollection
|
clojure.lang.IPersistentCollection
|
||||||
|
|
|
@ -107,7 +107,7 @@
|
||||||
|
|
||||||
(defn- make-model-map [model-map-values]
|
(defn- make-model-map [model-map-values]
|
||||||
(let [model-map-obj (new JtwigModelMap)
|
(let [model-map-obj (new JtwigModelMap)
|
||||||
values (if (:stringify-model-map-keys @options)
|
values (if (:auto-convert-map-keywords @options)
|
||||||
(stringify-keys model-map-values)
|
(stringify-keys model-map-values)
|
||||||
model-map-values)]
|
model-map-values)]
|
||||||
(doseq [[k v] values]
|
(doseq [[k v] values]
|
||||||
|
|
|
@ -30,9 +30,10 @@
|
||||||
; whenever these functions are used
|
; whenever these functions are used
|
||||||
:check-for-minified-web-resources true
|
:check-for-minified-web-resources true
|
||||||
|
|
||||||
; whether or not to automatically stringify the keys of model-maps. Jtwig requires that all
|
; automatically convert keyword keys in maps to/from strings as necessary when being passed
|
||||||
; the keys will be strings for model value resolution to work correctly. if you are already
|
; in model-maps, when passed to Jtwig functions and when returned as values from Jtwig
|
||||||
; setting your keys as maps, then you can turn this option off to save a bit on performance
|
; functions. this does incur a slight performance penalty, but is best turned on to avoid
|
||||||
:stringify-model-map-keys true
|
; having to do any manual conversions yourself and to keep your Clojure code as idiomatic
|
||||||
|
; as possible. Jtwig model-maps at the very least do require all the keys to be strings
|
||||||
}))
|
; (not keywords) to ensure that model-map value resolution works as expected.
|
||||||
|
:auto-convert-map-keywords true}))
|
Reference in a new issue