Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow finer configuration of how certain Objects get printed #307

Open
MrEbbinghaus opened this issue Oct 12, 2023 · 3 comments
Open

Allow finer configuration of how certain Objects get printed #307

MrEbbinghaus opened this issue Oct 12, 2023 · 3 comments

Comments

@MrEbbinghaus
Copy link

I just came across the issue that ArrayLists do not break.

(def alist (java.util.ArrayList.))

(dotimes [_ 100] (.add alist "SOME STRING"))

(zprint/czprint alist 30)
["SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING"]

The problem is, that this looks like a clojure/edn value that should be broken, but it is just a string from .toString.

(.toString alist)
"[SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING, SOME STRING]"

Coercing that ArrayList to a clojure vector would solve that problem.

(vec alist)
["SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING" ...]
 
(zprint/czprint (vec alist) {:width 30, :max-length 8})
["SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING"
 ...]

Is there a way to configure that behaviour?
I imagine something like:

(def config {:width 30, 
             :max-length 8, 
             java.util.List {:transform vec}}
(zprint/czprint alist config)
["SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING"
 "SOME STRING" "SOME STRING"
 ...]

This could work in general:

(zprint/czprint 5 
  {java.lang.Number {:transform #(str "Integer(" % ")")}})

; Integer(5)

(Notation is just an example, of course.)

@kkinnear
Copy link
Owner

Thanks for asking! This is an interesting problem, as the java.util.ArrayList doesn't trigger any of the "it must be one of these, print it this way", and just falls through into printing as a string. I'm sure that there are other Java types that will end up the same way. I'm tentatively open to the idea of some sort of class or type based transformation, although I'm not clear on where to put it. One option is for the things that don't show up as something else, another is to do it prior to the tests for what something is -- which is what you are implying, I think.

I'll give this some thought and see what I can figure out. If you happen to have any other examples of real-world Java classes that are getting the same (.toString ...) treatment, it would be interesting to have more than one example to experiment with and to write tests for. Thanks!

@MrEbbinghaus
Copy link
Author

Maybe this is a case for clojure.datafy?

something -datafy-> data -zprint-> formatted string feels like a good way to separate the concerns.

@kkinnear
Copy link
Owner

This is now implemented in 1.2.9.

There is a new option map key to transform random Java classes into other, more manageable data types: :modify-sexpr-by-type.

I wrote up a whole big thing in the reference manual about how this works (using your example data above). Search the reference manual for :modify-sexpr-by-type and hopefully that will explain how to do what you want. Let me know if it isn't clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants