Commits

Anonymous committed 123c7b9

add assert-as and as-map functions to util.clj

Comments (0)

Files changed (1)

src/main/clj/org/bituf/sqlrat/util.clj

   "Common utility functions")
 
 
-(defn as-vector
-  "Converts anything to a vector"
-  [anything]
-  (if (vector? anything) anything
-    (if (or (seq? anything) (set? anything)) (into [] anything)
-      (if (map? anything) (into [] (vals anything))
-        [anything]))))
-
-
-(defn as-set
-  "Converts anything to a set"
-  [anything]
-  (if (set? anything) anything
-    (if (or (seq? anything) (vector? anything)) (into #{} anything)
-      (if (map? anything) (into #{} (vals anything))
-        [anything]))))
-
-
 (defn bad-arg#
   "Throw IllegalArgumentException with specified arguments. Use this when you
    encounter bad/invalid parameters."
   [reason & more]
   (throw (IllegalArgumentException. (apply str reason more))))
 
+
+(defn assert-as
+  [item ^Class expected-type]
+  (try
+    (assert (isa? (type item) expected-type))
+    (catch AssertionError e
+      (throw (AssertionError. (str "Expected " expected-type " but found "
+                                (type item)))))))
+
+
+(defn as-vector
+  "Convert anything to a vector and return the result"
+  [anything]
+  (if (vector? anything) anything
+    (if (or (seq? anything) (set? anything)) (into [] anything)
+      (if (map? anything) (into [] (vals anything))
+        [anything]))))
+
+
+(defn as-set
+  "Convert anything to a set and return the result"
+  [anything]
+  (if (set? anything) anything
+    (if (or (seq? anything) (vector? anything)) (into #{} anything)
+      (if (map? anything) (into #{} (vals anything))
+        [anything]))))
+
+
+(defn as-map
+  "Convert any collection to a map and return the result"
+  [anything]
+  (if (map? anything) anything
+    (if (coll? anything) (apply array-map anything)
+      (bad-arg# (str "Expected collection, but found " (type anything) ": "
+                  anything)))))