Skip to content

Instantly share code, notes, and snippets.

@gfredericks
Created June 18, 2010 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfredericks/444122 to your computer and use it in GitHub Desktop.
Save gfredericks/444122 to your computer and use it in GitHub Desktop.
;;
;; Several methods that were helpful in development when working with large
;; legacy java objects. I wouldn't be too surprised if somebody told me that
;; emacs does some of this stuff already
;;
;; This function (which is arguably not really an appropriate use of multimethods)
;; is an alternative to the (bean) method, which I found returns a "map" that
;; will throw an exception if
;; you pass in a missing key (unlike a vanilla map, which just returns nil). So
;; this converts to a vanilla map
(defmulti safe-bean #(.isArray (class %)))
(defmethod safe-bean true [ob] (apply vector ob))
(defmethod safe-bean false [ob]
(apply hash-map (reduce concat (bean ob))))
;;
;; This method is similar to bean, but works on heavily nested java objects:
;; Pass in a nested map with a list of keys, and it will return a copy where
;; the java object reached by the list of keys has been expanded by (safe-bean)
;;
(defn bean-expand
"Just for dev stuff. Not a real function."
([ob] (safe-bean ob))
([ob key & keyz]
(assoc ob key (apply bean-expand (cons (ob key) keyz)))))
;;
;; And this is an example use of the above function, where a single java object is
;; run through bean-expand some twenty times, opening up all the relevant objects
;; as maps
;;
(defn generate-object-structure-file
[]
(let [bc (...get-object...)
obj (reduce
(fn [ob keys]
(apply
bean-expand
(cons ob keys)))
bc
[[:master]
[:suspense]
[:suspense :suspenseRecord]
[:master :beneficiaries]
[:master :subscriberRecord]
[:master :dependents]
[:master :address]
[:master :coverage]
[:suspense :suspenseRecord :rateInformation]
[:suspense :suspenseRecord :zipCode]
[:suspense :suspenseRecord :beneficiaryData]
[:suspense :suspenseRecord :beneficiaryData 1]
[:suspense :suspenseRecord :suspenseInformation]
[:suspense :suspenseRecord :dependentData]
[:suspense :suspenseRecord :dependentData 1]
[:master :beneficiaries :beneficiaryData]
[:master :beneficiaries :beneficiaryData 1]
[:master :dependents :dependentData]
[:master :dependents :dependentData 1]
[:master :address :zipCode]
[:master :coverage :rate_information]])]
(spit-it obj)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment