Permalink
Browse files

adding new parsing from nm-fn-api branch and writing some tests. next…

… step is to make parsing work with old and new style args lists
  • Loading branch information...
1 parent 47a6f27 commit 7d62384227c6adc7de67f63c69576e1136219118 Bradford Cross committed Mar 6, 2010
@@ -0,0 +1,72 @@
+(ns cascading.clojure.parse
+ (:import (cascading.tuple Tuple TupleEntry Fields))
+ (:use [clojure.contrib.seq-utils :only [find-first indexed]]))
+
+(defn ns-fn-name-pair [v]
+ (let [m (meta v)]
+ [(str (:ns m)) (str (:name m))]))
+
+(defn fn-spec [v-or-coll]
+ "v-or-coll => var or [var & params]
+ Returns an Object array that is used to represent a Clojure function.
+ If the argument is a var, the array represents that function.
+ If the argument is a coll, the array represents the function returned
+ by applying the first element, which should be a var, to the rest of the
+ elements."
+ (cond
+ (var? v-or-coll)
+ (into-array Object (ns-fn-name-pair v-or-coll))
+ (coll? v-or-coll)
+ (into-array Object
+ (concat
+ (ns-fn-name-pair (clojure.core/first v-or-coll))
+ (next v-or-coll)))
+ :else
+ (throw (IllegalArgumentException. (str v-or-coll)))))
+
+(defn- collectify [obj]
+ (if (sequential? obj) obj [obj]))
+
+(defn fields
+ {:tag Fields}
+ [obj]
+ (if (or (nil? obj) (instance? Fields obj))
+ obj
+ (Fields. (into-array String (collectify obj)))))
+
+(defn fields-array
+ [fields-seq]
+ (into-array Fields (clojure.core/map fields fields-seq)))
+
+(defn- fields-obj? [obj]
+ "Returns true for a Fileds instance, a string, or an array of strings."
+ (or
+ (instance? Fields obj)
+ (string? obj)
+ (and (sequential? obj) (every? string? obj))))
+
+(defn- idx-of-first [aseq pred]
+ (clojure.core/first (find-first #(pred (last %)) (indexed aseq))))
+
+(defn parse-args
+ "
+ arr => func-spec in-fields? :fn> func-fields :> out-fields
+
+ returns [in-fields func-fields spec out-fields]
+ "
+ ([arr] (parse-args arr Fields/RESULTS))
+ ([arr defaultout]
+ (let
+ [func-args (clojure.core/first arr)
+ varargs (rest arr)
+ spec (fn-spec func-args)
+ func-var (if (var? func-args) func-args (clojure.core/first func-args))
+ first-elem (clojure.core/first varargs)
+ [in-fields keyargs] (if (or (nil? first-elem)
+ (keyword? first-elem))
+ [Fields/ALL (apply hash-map varargs)]
+ [(fields (clojure.core/first varargs))
+ (apply hash-map (rest varargs))])
+ options (merge {:fn> (:fields (meta func-var)) :> defaultout} keyargs)
+ result [in-fields (fields (:fn> options)) spec (fields (:> options))]]
+ result )))
@@ -31,9 +31,8 @@
;;Note that you can not walk the tuple iterator more than once
;;but you can hold on to the seq and walk that more than once.
(defn maxpairs [it]
- (let [tuples it
- biggest (max-by second tuples)]
- (map #(concat % biggest) (remove #(= % biggest) tuples))))
+ (let [biggest (max-by second it)]
+ (map #(concat % biggest) (remove #(= % biggest) it))))
(deftest buffer-max-and-pair
(test-flow
@@ -0,0 +1,47 @@
+(ns cascading.clojure.parse-test
+ (:use clojure.test)
+ (:import (cascading.tuple Fields))
+ (:use cascading.clojure.parse))
+
+(defn example [x] x)
+
+(def obj-array-class (class (into-array Object [])))
+
+(defn extract-obj-array [x]
+ (map (fn [e]
+ (if (instance? obj-array-class e)
+ (seq e)
+ e))
+ x))
+
+(deftest parse-everything
+ (is (= [(fields ["foo"])
+ (fields ["bar"])
+ ["cascading.clojure.parse-test" "example"]
+ (fields ["baz"])]
+ (extract-obj-array
+ (parse-args [#'example "foo" :fn> "bar" :> "baz"])))))
+
+(deftest parse-no-input-selectors
+ (is (= [Fields/ALL
+ (fields ["bar"])
+ ["cascading.clojure.parse-test" "example"]
+ (fields ["baz"])]
+ (extract-obj-array
+ (parse-args [#'example :fn> "bar" :> "baz"])))))
+
+(deftest parse-no-input-or-output-selectors
+ (is (= [Fields/ALL
+ (fields ["bar"])
+ ["cascading.clojure.parse-test" "example"]
+ Fields/RESULTS]
+ (extract-obj-array
+ (parse-args [#'example :fn> "bar"])))))
+
+(deftest parse-no-input-or-output-or-fn-selectors
+ (is (= [Fields/ALL
+ nil
+ ["cascading.clojure.parse-test" "example"]
+ Fields/RESULTS]
+ (extract-obj-array
+ (parse-args [#'example])))))

0 comments on commit 7d62384

Please sign in to comment.