Closed ctrueden closed 1 year ago
It would be cool if, through solving this PR, we could formalize 1 or 2 or 3 as the intent of the ConvertService
, although I admit that may be infeasible for SciJava Common if any consensus would be a breaking change.
This issue has been mentioned on Image.sc Forum. There might be relevant details there:
The
DefaultConverter
was not being tested directly. With 4e40e04189cafcfad4c66357cbad17727dbf6325, now it is. But there are many cases that do not work, marked with commented out tests. (Also many missing tests, but at least it's a start!)This issue exists to document some of the known problems, and thoughts on how best to resolve them.
Just to give an inkling of the problems highlighted by this issue:
:point_up: This patch breaks three tests. :open_mouth:
So let's consider the following questions:
What would you expect to happen in the following circumstances? You ask SciJava Common to convert some data you have, to a particular type:
"{{0, 1}, {2, 3}}"
→double[][]
. Do you expect: A)[[0d, 1d], [2d, 3d]]
? B)null
?"{{0, 1}, {curtis, 3}}"
→Double[][]
. Do you expect: A)[[0d, 1d], [null, 3d]]
? B)null
?"{{0, 1}, {curtis, 3}}"
→double[][]
. Do you expect: A)[[0d, 1d], [NaN, 3d]]
? B)null
?"5"
→double[]
. Do you expect: A)[5d]
? B)null
?"5"
→double[][]
. Do you expect: A)[[5d]]
? B)null
?"curtis"
→double[]
. Do you expect: A)[NaN]
? B)null
?"curtis"
→Double[]
. Do you expect: A)[null]
? B)null
?"curtis"
→Double[][]
. Do you expect: A)[[null]]
? B)null
?I think the following behavior is what we should shoot for:
Object[]
,Object[][]
, etc., of correct dimensionality.null
.E.g. for (8) above, we would go
"curtis"
→new Object[][] {{"curtis"}}
→new Double[][] {{ convert("curtis", Double.class) }}
.The remaining question is: what to do if any/all of those leaves are not convertible?
After some discussion between myself, @hinerm, and @gselzer, we narrowed down the following potentially reasonable behavior:
If all of the leaf elements are convertible to the destination element type, then the converted object is populated. Otherwise, the whole collection is inconvertible, and we return
null
for the whole collection, andfalse
forcanConvert
. This would answer the above list withnull
for questions 2, 3, 6, 7, 8.If any of the leaf elements are convertible to the destination element type, then the converted object is populated, filling any inconvertible elements with
null
. This would answer the above list withnull
for questions 6, 7, 8—but 2 and 3 would work.Regardless of whether any leaf elements are convertible to the destination element type, the converted object is populated, filling nulls as in (2). This would answer the above list with "As all the way down" i.e. no
null
results.Part of this issue is deciding which of these three behaviors would be best. Then fixing the
DefaultConverter
to work that way—in both itscanConvert
andconvert
methods—and uncommenting the disabled tests (and adjusting them as needed) to validate it.