scijava / scijava-common

A plugin framework and application container with built-in extensibility mechanism :electric_plug:
BSD 2-Clause "Simplified" License
87 stars 52 forks source link

Improve array -> `String` conversions from `DefaultConverter` #435

Closed gselzer closed 2 years ago

gselzer commented 2 years ago

This PR fixes the issue described in imagej/napari-imagej#65 by improving the conversion of arrays to Strings.

We make a few changes:

Concerns:

This should be enough to close imagej/napari-imagej#65 once this change makes its way into a release.

gselzer commented 2 years ago

On second thought, why do we shove so much case logic into DefaultConverter? Why aren't these all split into separate Converters?

ctrueden commented 2 years ago

why do we shove so much case logic into DefaultConverter? Why aren't these all split into separate Converters?

Hysterical raisins. There didn't used to be modular converters, and at one point I split a bunch of logic into separate plugins, but was not able to fully split everything. Please feel free to do it, as your time allows!

gselzer commented 2 years ago

Sounds good. It's Priority.EXTREMELY_LOW as far as I'm concerned, so maybe I'll just make an issue about it.

ctrueden commented 2 years ago

@gselzer To answer your specific questions:

Is the converted String specific enough? Do we need to add the component type into the String somehow?

I think it is OK not to. The conversion is allowed to be lossy. If we baked in the component type of the array, it would make reconstruction from string more rigorous, but also more complicated. It would also make converted arrays harder for humans to read.

Might this break backwards compatibility, for either change?

I don't think it breaks backwards compatibility in a broad sense, but specifically for strings passed to ArrayUtils.toCollection, it does. Previously, strings would be wrapped into singleton collections. We should not change this behavior.

Can/should we test more types?

I pointed out some cases where the conversion is currently problematic. We should test those cases. In particular, for nested arrays, and strings containing special characters that need escaping, we should test.

gselzer commented 2 years ago

I'm not sure we want to do this.

Do you mean that we might want to close the PR?

If we do, then I think we need a more robust stringification of the array data. In that case, I would recommend escaping each string into a YAML-compliant form, since YAML solves essentially the same problem of how to express a properly typed array in string form.

I'm not sure that I understand what you mean by a "YAML-compliant form". Can you elaborate on what you mean? I do agree, looking back, that we should probably be using some sort of brackets if possible; I'm not sure why I did not do that before.

Is the converted String specific enough? Do we need to add the component type into the String somehow?

I think it is OK not to. The conversion is allowed to be lossy. If we baked in the component type of the array, it would make reconstruction from string more rigorous, but also more complicated. It would also make converted arrays harder for humans to read.

:+1:

Might this break backwards compatibility, for either change?

I don't think it breaks backwards compatibility in a broad sense, but specifically for strings passed to ArrayUtils.toCollection, it does. Previously, strings would be wrapped into singleton collections. We should not change this behavior.

Yeah, you are right. I removed the changes made to that method.

Can/should we test more types?

I pointed out some cases where the conversion is currently problematic. We should test those cases. In particular, for nested arrays, and strings containing special characters that need escaping, we should test.

Thanks, I tested nested arrays, empty arrays, is there anything else that we should be testing?

gselzer commented 2 years ago

Thanks again for taking a look @ctrueden

gselzer commented 2 years ago

@ctrueden I think we are in a much better place now with Parsington, thanks for the suggestion!

ctrueden commented 2 years ago

@gselzer I added a couple more asserts, checking that the result strings converted from arrays are what we expect, before the tests continue and convert them back into arrays again. I wanted to be totally sure that strings with more challenging characters are escaped correctly.

ctrueden commented 2 years ago

@gselzer Turns out there were still problems after I merged this PR. Now fixed in 42db182fa90ebe4fc79163713b8cf5ac7ee9b143 and 497eff6eca3ee9687acddd9453859fef40071390.

gselzer commented 2 years ago

Thanks for those fixes @ctrueden. Admittedly I went through and made the changes pretty quick.