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

Adding overloads to Document for GetValues and Get #385

Merged
merged 8 commits into from
Dec 3, 2020
2 changes: 1 addition & 1 deletion build/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<ICU4NLanguageDataPackageVersion>$(ICU4NPackageVersion)</ICU4NLanguageDataPackageVersion>
<ICU4NRegionDataPackageVersion>$(ICU4NPackageVersion)</ICU4NRegionDataPackageVersion>
<ICU4NTransliteratorPackageVersion>$(ICU4NPackageVersion)</ICU4NTransliteratorPackageVersion>
<J2NPackageVersion>2.0.0-beta-0010</J2NPackageVersion>
<J2NPackageVersion>2.0.0-beta-0011</J2NPackageVersion>
<LiquidTestReportsMarkdownPackageVersion>1.0.9</LiquidTestReportsMarkdownPackageVersion>
<MicrosoftAspNetCoreHttpAbstractionsPackageVersion>2.0.0</MicrosoftAspNetCoreHttpAbstractionsPackageVersion>
<MicrosoftAspNetCoreTestHostPackageVersion>2.0.0</MicrosoftAspNetCoreTestHostPackageVersion>
Expand Down
34 changes: 33 additions & 1 deletion src/Lucene.Net.Misc/Document/LazyDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void FetchRealValues(string name, int fieldNum)
/// <summary>
/// @lucene.internal
/// </summary>
public class LazyField : IIndexableField
public class LazyField : IIndexableField, IFormattable
{
private readonly LazyDocument outerInstance;

Expand Down Expand Up @@ -354,6 +354,38 @@ public virtual TokenStream GetTokenStream(Analyzer analyzer)
{
return GetRealValue().GetTokenStream(analyzer);
}

// LUCENENET specific - method added for better .NET compatibility
public override string ToString()
{
return ToString(null, J2N.Text.StringFormatter.CurrentCulture);
}

// LUCENENET specific - method added for better .NET compatibility
public virtual string ToString(string format)
{
return ToString(format, J2N.Text.StringFormatter.CurrentCulture);
}

// LUCENENET specific - method added for better .NET compatibility
public virtual string ToString(IFormatProvider provider)
NightOwl888 marked this conversation as resolved.
Show resolved Hide resolved
NightOwl888 marked this conversation as resolved.
Show resolved Hide resolved
{
return ToString(null, provider);
}

// LUCENENET specific - method added for better .NET compatibility
public virtual string ToString(string format,IFormatProvider provider)
{
IIndexableField realValue = GetRealValue();
if(realValue is IFormattable formattable)
{
return formattable.ToString(format, provider);
}
else
{
return realValue.ToString();
}
}
}
}
}
83 changes: 82 additions & 1 deletion src/Lucene.Net.Tests/Document/TestField.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using J2N.Globalization;
using J2N.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
Expand All @@ -9,6 +10,8 @@
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Assert = Lucene.Net.TestFramework.Assert;
Expand Down Expand Up @@ -709,7 +712,7 @@ public void TestStoreAndRetrieveFieldType()
// assertEquals(false, fieldType.tokenized());

// assertEquals(FieldType.NumericType.DOUBLE, fieldType.numericType());

// doc.add(field);
// writer.addDocument(doc);
// writer.commit();
Expand All @@ -734,5 +737,83 @@ public void TestStoreAndRetrieveFieldType()

// dir.close();
//}

public enum ToStringCulture
{
Invariant,
France
}

public static IEnumerable<TestCaseData> ToStringData(ToStringCulture cultureEnum)
{
CultureInfo culture = cultureEnum switch
{
ToStringCulture.France => new CultureInfo("fr-FR"),
_ => CultureInfo.InvariantCulture
};

string sep = culture.NumberFormat.NumberDecimalSeparator;

yield return new TestCaseData(new DoubleField("foo", 5d, Field.Store.NO), $"indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=DOUBLE,numericPrecisionStep=4<foo:5{sep}0>");
yield return new TestCaseData(new DoubleField("foo", 5d, Field.Store.YES), $"stored,indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=DOUBLE,numericPrecisionStep=4<foo:5{sep}0>");
yield return new TestCaseData(new DoubleDocValuesField("foo", 5d), "docValueType=NUMERIC<foo:4617315517961601024>");
yield return new TestCaseData(new SingleDocValuesField("foo", 5f), "docValueType=NUMERIC<foo:1084227584>");
yield return new TestCaseData(new SingleField("foo", 5f, Field.Store.NO), $"indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=SINGLE,numericPrecisionStep=4<foo:5{sep}0>");
yield return new TestCaseData(new SingleField("foo", 5f, Field.Store.YES), $"stored,indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=SINGLE,numericPrecisionStep=4<foo:5{sep}0>");
yield return new TestCaseData(new Int32Field("foo", 5, Field.Store.NO), "indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=INT32,numericPrecisionStep=4<foo:5>");
yield return new TestCaseData(new Int32Field("foo", 5, Field.Store.YES), "stored,indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=INT32,numericPrecisionStep=4<foo:5>");
yield return new TestCaseData(new NumericDocValuesField("foo", 5L), "docValueType=NUMERIC<foo:5>");
yield return new TestCaseData(new Int64Field("foo", 5L, Field.Store.NO), "indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=INT64,numericPrecisionStep=4<foo:5>");
yield return new TestCaseData(new Int64Field("foo", 5L, Field.Store.YES), "stored,indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=INT64,numericPrecisionStep=4<foo:5>");
yield return new TestCaseData(new SortedDocValuesField("foo", new BytesRef("bar")), "docValueType=SORTED<foo:[62 61 72]>");
yield return new TestCaseData(new BinaryDocValuesField("foo", new BytesRef("bar")), "docValueType=BINARY<foo:[62 61 72]>");
yield return new TestCaseData(new StringField("foo", "bar", Field.Store.NO), "indexed,omitNorms,indexOptions=DOCS_ONLY<foo:bar>");
yield return new TestCaseData(new StringField("foo", "bar", Field.Store.YES), "stored,indexed,omitNorms,indexOptions=DOCS_ONLY<foo:bar>");
yield return new TestCaseData(new TextField("foo", "bar", Field.Store.NO), "indexed,tokenized<foo:bar>");
yield return new TestCaseData(new TextField("foo", "bar", Field.Store.YES), "stored,indexed,tokenized<foo:bar>");
yield return new TestCaseData(new TextField("foo", new StringReader("bar")), "indexed,tokenized<foo:System.IO.StringReader>");
yield return new TestCaseData(new StoredField("foo", "bar".GetBytes(Encoding.UTF8)), "stored<foo:[62 61 72]>");
yield return new TestCaseData(new StoredField("foo", "bar".GetBytes(Encoding.UTF8), 0, 3), "stored<foo:[62 61 72]>");
yield return new TestCaseData(new StoredField("foo", new BytesRef("bar")), "stored<foo:[62 61 72]>");
yield return new TestCaseData(new StoredField("foo", "bar"), "stored<foo:bar>");
yield return new TestCaseData(new StoredField("foo", 1), "stored<foo:1>");
yield return new TestCaseData(new StoredField("foo", 1D), $"stored<foo:1{sep}0>");
yield return new TestCaseData(new StoredField("foo", 1F), $"stored<foo:1{sep}0>");
yield return new TestCaseData(new StoredField("foo", 1L), "stored<foo:1>");

// Negative Zero
yield return new TestCaseData(new DoubleField("foo", -0.0d, Field.Store.NO), $"indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=DOUBLE,numericPrecisionStep=4<foo:-0{sep}0>");
yield return new TestCaseData(new DoubleDocValuesField("foo", -0.0d), "docValueType=NUMERIC<foo:-9223372036854775808>");
yield return new TestCaseData(new DoubleDocValuesField("foo", 0.0d), "docValueType=NUMERIC<foo:0>");
yield return new TestCaseData(new SingleDocValuesField("foo", -0.0f), "docValueType=NUMERIC<foo:-2147483648>");
yield return new TestCaseData(new SingleDocValuesField("foo", 0.0f), "docValueType=NUMERIC<foo:0>");
yield return new TestCaseData(new SingleField("foo", -0.0f, Field.Store.NO), $"indexed,tokenized,omitNorms,indexOptions=DOCS_ONLY,numericType=SINGLE,numericPrecisionStep=4<foo:-0{sep}0>");
yield return new TestCaseData(new StoredField("foo", -0D), $"stored<foo:-0{sep}0>");
yield return new TestCaseData(new StoredField("foo", -0F), $"stored<foo:-0{sep}0>");
}

[Test]
[LuceneNetSpecific]
[TestCaseSource("ToStringData", new object[] { ToStringCulture.Invariant })]
public void TestToStringInvariant(Field field, string expected)
{
using (var cultureContext = new CultureContext(CultureInfo.InvariantCulture))
{
string actual = field.ToString();
Assert.AreEqual(expected, actual);
}
}

[Test]
[LuceneNetSpecific]
[TestCaseSource("ToStringData", new object[] { ToStringCulture.France })]
public void TestToStringFrance(Field field, string expected)
{
using (var cultureContext = new CultureContext(new CultureInfo("fr-FR")))
{
string actual = field.ToString();
Assert.AreEqual(expected, actual);
}
}
}
}