|
44 | 44 | conversion: |
45 | 45 | * :add-class -- if true, add :class with the actual class of the object |
46 | 46 | being converted -- this mimics clojure.core/bean. |
| 47 | + * :exceptions -- controls how getter exceptions should be handled: |
| 48 | + * :group -- return an :exceptions hash map in the object that |
| 49 | + contains all the properties that failed, with their exceptions, |
| 50 | + * :omit -- ignore exceptions and omit the properties that caused them, |
| 51 | + * :qualify -- return the exception as :<property>/exception and |
| 52 | + omit the property itself, |
| 53 | + * :return -- simply return the exception as the value of the property. |
47 | 54 | * :omit -- a set of properties (keywords) to omit from the conversion |
48 | 55 | so that unsafe methods are not called." |
49 | 56 | (fn [obj _] (class obj))) |
|
263 | 270 | (let [clazz (.getClass instance)] |
264 | 271 | (if (.isArray clazz) |
265 | 272 | ((:from-shallow (add-array-methods clazz)) instance opts) |
266 | | - (let [getter-map (reduce add-shallow-getter-fn {} (get-property-descriptors clazz))] |
267 | | - (into (if (:add-class opts) {:class (class instance)} {}) |
268 | | - (for [[key getter-fn] (seq getter-map) |
269 | | - :when (not (contains? (:omit opts) key))] |
270 | | - [key (getter-fn instance)])))))) |
| 273 | + (let [getter-map (reduce add-shallow-getter-fn {} (get-property-descriptors clazz)) |
| 274 | + exs (atom []) |
| 275 | + pairs (for [[key getter-fn] (seq getter-map) |
| 276 | + :when (not (contains? (:omit opts) key)) |
| 277 | + :let [[k v] |
| 278 | + (if-let [exh (:exceptions opts)] |
| 279 | + (try |
| 280 | + [key (getter-fn instance)] |
| 281 | + (catch Throwable t |
| 282 | + (case exh |
| 283 | + :group (swap! exs conj [key t]) |
| 284 | + :omit nil |
| 285 | + :qualify [(keyword (name key) |
| 286 | + "exception") t] |
| 287 | + :return [key t]))) |
| 288 | + [key (getter-fn instance)])] |
| 289 | + :when k] |
| 290 | + [k v])] |
| 291 | + (cond-> {} |
| 292 | + (:add-class opts) (assoc :class (class instance)) |
| 293 | + (seq @exs) (assoc :exceptions (into {} @exs)) |
| 294 | + (seq pairs) (into pairs)))))) |
271 | 295 |
|
272 | 296 | (doseq [clazz [String Character Byte Short Integer Long Float Double |
273 | 297 | java.math.BigInteger java.math.BigDecimal]] |
|
0 commit comments