That would be possible. If I understand your meaning, something like:
(require '[insn.core :as insn])
(defn make-pojos [cname m]
(let [[fields subs] (partition-by (comp map? second) m)
const-type #(if (string? %) String :int)]
(doseq [[sname sm] subs]
(make-pojos (str cname \$ (name sname)) sm))
(insn/define
{:name cname
:fields (for [[fname fval] fields]
{:flags [:public :final],
:name (name fname)
:type (const-type fval)})
:methods [{:name :init
:emit `[[:aload 0]
[:invokespecial :super :init [:void]]
~@(mapcat (fn [[fname fval]]
[[:aload 0]
[:ldc fval]
[:putfield :this (name fname)
(const-type fval)]])
fields)
[:return]]}]})))
(def pojo-data {:foo 42, :bar "ethel",
:Sub {:baz 17, :Another {:quux "fred"}}})
(make-pojos "user.Root" pojo-data)
Above I only used string and int values for the fields. Storing arbitrary
field data requires a bit more work, as the JVM only supports a few types
as first class constant data. Now, if you're in the REPL, just do:
(.foo (user.Root.)) ;; => 42
(.bar (user.Root.)) ;; => "ethel"
(.baz (user.Root$Sub.)) ;; => 17
(.quux (user.Root$Sub$Another.)) ;; => "fred"
How you nest the POJO names is up to you. Above I used '$' like Java does.
On Saturday, August 19, 2017 at 1:02:46 PM UTC-7, Didier wrote:
>
> This looks cool, would it be possible to use this to have a deep nested
> clojure map automatically create a Java nesting of pojos? Like each key
> becomes a field of an actual class?
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.