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
Adding overloads to Document for GetValues and Get #385
Adding overloads to Document for GetValues and Get #385
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. Great work! This is looking good so far, but I have requested a few changes before this can be merged.
While it wasn't part of the original scope of #362, after seeing the impact of this it occurred to me that the user experience can be improved by doing the following:
- Remove
ToString(IFormatProvider provider)
fromIIndexableField
, as this is a leaky abstraction. The fact this forcesToString(IFormatProvider)
into the tests is proof of this. Instead, we should allowIIndexableField
implementations to opt in if they want to support formatting by implementingIFormattable
. Don't inheritIFormattable
fromIIndexableField
, allow them to be used independently. - We technically only need 1 implementation of
ToString
on eachIIndexableField
type, the rest of the overloads can just cascade their call. See this example in J2N. - Change logic of formattable
Field.ToString()
methods to test forif (field is IFormattable formattable)
and then call theformattable.ToString(string, IFormatProvider)
overload. The reasonable default values of the parameters arenull
andJ2N.Text.StringFormatter.CurrentCulture
for those overloads that don't include both. In the casenull
is passed, just use the original string format (in the case of field it was{0}<{1}:{2}>
). If the formattable check doesn't pass, simply call the defaultfield.ToString()
overload. - Implement
IFormattable
on the built-inField
andLazyField
types. Also add the other 3 overloadsToString()
,ToString(string format)
andToString(IFormatProvider provider)
so each built-inIIndexableField
type has all 4 overloads. - Change the
Document
class to be similar, implementingIFormattable
on the built-in types but allowing implementers to opt out of formatting. However, since a format string doesn't make much sense in this case, explicitly implementIFormattable
(which will hide it from the API ofDocument
). Therefore, theDocument
class will still only have 2 public overloadsToString()
andToString(IFormatProvider)
that will cascade their call to an explictly implmentedToString(string, IFormatProvider)
method.
If you analyze the source of StringBuilder.Append
, it shows exactly what is expected of how to support IFormattable
.
I think we should consider supporting ICustomFormatter
too, but we need to ponder exactly how it should be done. No need to make it part of this PR.
Can you explain what you mean by
Do you mean to change logic in class Also, I don't really understand 5. either. Especially
Can you elaborate? |
I guess it is simpler just to show the code than to describe it. We test for In all implementations of IIndexableField (4 overloads of
|
Is everything ok with my last commit or should I change something? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My apologies for not getting to this sooner. I started working on some tests for this (as per #362), but discovered a formatting bug in J2N and then got sidetracked. In Java, floating point numbers are always formatted with at least 1 decimal place, but in .NET the decimal is removed if it is a whole number. In J2N that 1 decimal place is sometimes being truncated.
I'll work on getting the bug fixed and completing the tests, but in the meantime there are a few changes that are requested from the last commit.
…ider to J2N.Text.StringFormatter and added support for passing other custom formatters
…) implementation to shared private method
…e attempting to cast
c0d91c4
to
eb11e88
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reverted the merge, since it caused a conflict. For future reference please rebase against master rather than merge it.
I also made some minor changes to close #362.
Adding overload for GetValues() and Get() to support in Document. GetStringValue() method in now only being called once.