Skip to content

Instantly share code, notes, and snippets.

@ghadishayban
Created September 18, 2016 16:44
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 ghadishayban/c36368d1de6ce15ba2a782c3cfc9211a to your computer and use it in GitHub Desktop.
Save ghadishayban/c36368d1de6ce15ba2a782c3cfc9211a to your computer and use it in GitHub Desktop.
better impl of #'clojure.core/bean
(deftype DataWrapper [prop-map obj]
clojure.lang.ILookup
(valAt [_ k]
(when-let [getter (prop-map k)]
(getter obj)))
(valAt [_ k not-found]
(if-let [getter (prop-map k)]
(getter obj)
not-found))
clojure.lang.IKVReduce
(kvreduce [_ rf init]
(reduce-kv
(fn [acc k getter] (rf acc k (getter obj)))
init prop-map))
clojure.lang.Seqable
(seq [this]
;; snapshot
(.kvreduce this (fn [l k v]
(cons (clojure.lang.MapEntry/create k v) l)) '())))
(defmethod print-method DataWrapper [d ^java.io.Writer w]
(-> (reduce-kv assoc {} d)
(print-method w)))
(defn data-accessor
"Returns a function that turns an instance of the specified class
into a printable implementation of ILookup, Seqable & IKVReduce"
[^Class c]
(let [pmap (reduce (fn [m ^java.beans.PropertyDescriptor pd]
(let [name (.getName pd)
method (.getReadMethod pd)]
(if (and method (zero? (alength (.getParameterTypes method))))
(assoc m (keyword name) (fn [obj] (clojure.lang.Reflector/prepRet
(.getPropertyType pd)
(.invoke method obj nil))))
m)))
{}
(.. java.beans.Introspector
(getBeanInfo c)
(getPropertyDescriptors)))]
(fn [obj]
(->DataWrapper pmap obj))))
(map (data-accessor java.util.zip.ZipEntry)
(enumeration-seq (.entries zipfile)))
=>
({:creationTime nil, :compressedSize 8355, :lastAccessTime nil, :directory false, :method 8, :name "profiles-types.json", :time 1445690720000, :extra nil, :size 91555, :crc 3900300913, :lastModifiedTime #object[java.nio.file.attribute.FileTime 0x6b8d283f "2015-10-24T12:45:20Z"], :class java.util.zip.ZipEntry, :comment nil}
{:creationTime nil, :compressedSize 92033, :lastAccessTime nil, :directory false, :method 8, :name "profiles-resources.json", :time 1445690720000, :extra nil, :size 951340, :crc 1620907508, :lastModifiedTime #object[java.nio.file.attribute.FileTime 0x44a91c1 "2015-10-24T12:45:20Z"], :class java.util.zip.ZipEntry, :comment nil}
{:creationTime nil, :compressedSize 99372, :lastAccessTime nil, :directory false, :method 8, :name "profiles-others.json", :time 1445690722000, :extra nil, :size 1471896, :crc 3169460679, :lastModifiedTime #object[java.nio.file.attribute.FileTime 0x9ea195c "2015-10-24T12:45:22Z"], :class java.util.zip.ZipEntry, :comment nil}
{:creationTime nil, :compressedSize 26790, :lastAccessTime nil, :directory false, :method 8, :name "extension-definitions.json", :time 1445690722000, :extra nil, :size 459735, :crc 3882596927, :lastModifiedTime #object[java.nio.file.attribute.FileTime 0x2d5e090f "2015-10-24T12:45:22Z"], :class java.util.zip.ZipEntry, :comment nil}
{:creationTime nil, :compressedSize 46944, :lastAccessTime nil, :directory false, :method 8, :name "search-parameters.json", :time 1445690722000, :extra nil, :size 644873, :crc 259694272, :lastModifiedTime #object[java.nio.file.attribute.FileTime 0x43a9b7aa "2015-10-24T12:45:22Z"], :class java.util.zip.ZipEntry, :comment nil})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment