Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert ftl->clj to a protocol function.

  • Loading branch information...
commit 554eedb8f26901edf5835af5726c3dfcc966af92 1 parent 1511863
@ghoseb authored
Showing with 34 additions and 25 deletions.
  1. +34 −25 src/freemarker_clj/shim.clj
View
59 src/freemarker_clj/shim.clj
@@ -11,39 +11,48 @@
TemplateScalarModel
TemplateSequenceModel]))
+(defprotocol IFtlToClj
+ (ftl->clj [o] "Convert a Ftl object to its Clojure equivalent."))
-(defn ftl->clj
- [x]
- (cond
- (instance? TemplateBooleanModel x)
- (.getAsBoolean x)
- (instance? TemplateCollectionModel x)
- (let [iterator (.iterator x)]
- (loop [acc []]
- (if (.hasNext iterator)
- (conj acc (ftl->clj (.next iterator)))
- acc)))
+(extend-protocol IFtlToClj
+ TemplateBooleanModel
+ (ftl->clj [x]
+ (.getAsBoolean x))
- (instance? TemplateDateModel x)
- (.getAsDate x)
+ TemplateCollectionModel
+ (ftl->clj [x]
+ (let [iterator (.iterator x)]
+ (loop [acc []]
+ (if (.hasNext iterator)
+ (conj acc (ftl->clj (.next iterator)))
+ acc))))
- (instance? TemplateHashModelEx x)
- (zipmap (ftl->clj (.keys x))
- (ftl->clj (.values x)))
+ TemplateDateModel
+ (ftl->clj [x]
+ (.getAsDate x))
- (instance? TemplateNumberModel x)
- (.getAsNumber x)
+ TemplateHashModelEx
+ (ftl->clj [x]
+ (zipmap (ftl->clj (.keys x))
+ (ftl->clj (.values x))))
- (instance? TemplateScalarModel x)
- (.getAsString x)
+ TemplateNumberModel
+ (ftl->clj [x]
+ (.getAsNumber x))
- (instance? TemplateSequenceModel x)
- (for [i (range (.size x))]
- (ftl->clj (.get x i)))
+ TemplateScalarModel
+ (ftl->clj [x]
+ (.getAsString x))
- :else
- (throw (IllegalArgumentException. (format "Don't know how to convert %s (class %s) to clj" x (class x))))))
+ TemplateSequenceModel
+ (ftl->clj [x]
+ (for [i (range (.size x))]
+ (ftl->clj (.get x i))))
+
+ Object
+ (ftl->clj [x]
+ (throw (IllegalArgumentException. (format "Don't know how to convert %s (class %s) to clj" x (class x))))))
(defn fn->method
Please sign in to comment.
Something went wrong with that request. Please try again.