From 99e9f91445ce27b8edb61fc4669e0e652d6ce2a0 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 14 Jun 2014 18:54:56 -0400 Subject: [PATCH] change :stringify-model-map-keys to something more all-encompassing --- src/clj_jtwig/convert.clj | 17 ++++++++++++++--- src/clj_jtwig/core.clj | 2 +- src/clj_jtwig/options.clj | 13 +++++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/clj_jtwig/convert.clj b/src/clj_jtwig/convert.clj index d2d45d6..acd37dc 100644 --- a/src/clj_jtwig/convert.clj +++ b/src/clj_jtwig/convert.clj @@ -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 diff --git a/src/clj_jtwig/core.clj b/src/clj_jtwig/core.clj index c561617..4f2f26f 100644 --- a/src/clj_jtwig/core.clj +++ b/src/clj_jtwig/core.clj @@ -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] diff --git a/src/clj_jtwig/options.clj b/src/clj_jtwig/options.clj index dde2336..4678b08 100644 --- a/src/clj_jtwig/options.clj +++ b/src/clj_jtwig/options.clj @@ -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 - - })) \ No newline at end of file + ; 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})) \ No newline at end of file