change :stringify-model-map-keys to something more all-encompassing

This commit is contained in:
Gered 2014-06-14 18:54:56 -04:00
parent 4315ddfa54
commit 99e9f91445
3 changed files with 22 additions and 10 deletions

View file

@ -1,5 +1,6 @@
(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
(to-clojure [x]))
@ -21,7 +22,12 @@
(.entrySet)
(reduce
(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
@ -45,7 +51,12 @@
(to-java [x]
(let [hashmap (new java.util.HashMap (count 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))
clojure.lang.IPersistentCollection

View file

@ -107,7 +107,7 @@
(defn- make-model-map [model-map-values]
(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)
model-map-values)]
(doseq [[k v] values]

View file

@ -30,9 +30,10 @@
; whenever these functions are used
:check-for-minified-web-resources true
; whether or not to automatically stringify the keys of model-maps. Jtwig requires that all
; the keys will be strings for model value resolution to work correctly. if you are already
; setting your keys as maps, then you can turn this option off to save a bit on performance
:stringify-model-map-keys true
}))
; automatically convert keyword keys in maps to/from strings as necessary when being passed
; in model-maps, when passed to Jtwig functions and when returned as values from Jtwig
; functions. this does incur a slight performance penalty, but is best turned on to avoid
; 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}))