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

Throw exceptions on invalid conversions #112

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.6] - 2024-07-16

- Throw exceptions on invalid conversion attempts

## [1.2.5] - 2024-07-11

- Internal performance improvements, using ReadOnlySpan<char> instead of string internally
Expand Down Expand Up @@ -84,7 +88,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial port of content from [FAnsiSql](https://github.com/HicServices/FAnsiSql)

[Unreleased]: https://github.com/HicServices/TypeGuesser/compare/v1.2.5...main
[Unreleased]: https://github.com/HicServices/TypeGuesser/compare/v1.2.6...main
[1.2.6]: https://github.com/HicServices/TypeGuesser/compare/v1.2.5...v1.2.6
[1.2.5]: https://github.com/HicServices/TypeGuesser/compare/v1.2.4...v1.2.5
[1.2.4]: https://github.com/HicServices/TypeGuesser/compare/v1.2.3...v1.2.4
[1.2.3]: https://github.com/HicServices/TypeGuesser/compare/v1.2.2...v1.2.3
Expand Down
6 changes: 3 additions & 3 deletions SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("1.2.5")]
[assembly: AssemblyFileVersion("1.2.5")]
[assembly: AssemblyInformationalVersion("1.2.5")]
[assembly: AssemblyVersion("1.2.6")]
[assembly: AssemblyFileVersion("1.2.6")]
[assembly: AssemblyInformationalVersion("1.2.6")]
3 changes: 2 additions & 1 deletion TypeGuesser/Deciders/BoolTypeDecider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo newCulture)

candidateString = StripWhitespace(candidateString);

return candidateString.Length switch
bool? b = candidateString.Length switch
{
1 => "1tTyYjJ0fFnN".IndexOf(candidateString[0]) != -1 ? "0fFnN".IndexOf(candidateString[0]) == -1 : null,
2 => candidateString.Equals("ja",StringComparison.OrdinalIgnoreCase) ? true :
Expand All @@ -41,6 +41,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo newCulture)
5 => candidateString.Equals("false",StringComparison.OrdinalIgnoreCase) ? false : null,
_ => null
};
return b ?? throw new Exception("Invalid bool");
}

private static ReadOnlySpan<char> StripWhitespace(ReadOnlySpan<char> candidateString)
Expand Down
2 changes: 1 addition & 1 deletion TypeGuesser/Deciders/DecimalTypeDecider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo culture)
}

/// <inheritdoc />
protected override object? ParseImpl(ReadOnlySpan<char> value) => decimal.TryParse(value, NumberStyles.Any, Culture.NumberFormat, out var d) ? d : null;
protected override object ParseImpl(ReadOnlySpan<char> value) => decimal.Parse(value, NumberStyles.Any, Culture.NumberFormat);

/// <inheritdoc/>
protected override bool IsAcceptableAsTypeImpl(ReadOnlySpan<char> candidateString, IDataTypeSize? sizeRecord)
Expand Down
2 changes: 1 addition & 1 deletion TypeGuesser/Deciders/IntTypeDecider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override IDecideTypesForStrings CloneImpl(CultureInfo newCulture)
}

/// <inheritdoc />
protected override object? ParseImpl(ReadOnlySpan<char> value) => int.TryParse(value, NumberStyles.Any, Culture.NumberFormat, out var i) ? i : null;
protected override object ParseImpl(ReadOnlySpan<char> value) => int.Parse(value, NumberStyles.Any, Culture.NumberFormat);

/// <inheritdoc/>
protected override bool IsAcceptableAsTypeImpl(ReadOnlySpan<char> candidateString, IDataTypeSize? sizeRecord)
Expand Down
4 changes: 2 additions & 2 deletions TypeGuesser/Guesser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ public void AdjustToCompensateForValue(object? o)
SR.Guesser_AdjustToCompensateForValue_GuesserPassedMixedTypeValues,o,o.GetType(),
Guess.CSharpType));

var oToString = o.ToString();
var oToString = o.ToString() ?? string.Empty;

//we might need to fallback on a string later on, in this case we should always record the maximum length of input seen before even if it is acceptable as int, double, dates etc
Guess.Width = Math.Max(Guess.Width ?? -1,GetStringLength(oToString??string.Empty));
Guess.Width = Math.Max(Guess.Width ?? -1, GetStringLength(oToString.AsSpan()));

//if it's a string
if (o is string oAsString)
Expand Down