VOOZH about

URL: https://www.nuget.org/packages/HIC.TypeGuesser/

⇱ NuGet Gallery | HIC.TypeGuesser 1.2.7




HIC.TypeGuesser 1.2.7

dotnet add package HIC.TypeGuesser --version 1.2.7
 
 
NuGet\Install-Package HIC.TypeGuesser -Version 1.2.7
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HIC.TypeGuesser" Version="1.2.7" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HIC.TypeGuesser" Version="1.2.7" />
 
Directory.Packages.props
<PackageReference Include="HIC.TypeGuesser" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HIC.TypeGuesser --version 1.2.7
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HIC.TypeGuesser, 1.2.7"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HIC.TypeGuesser@1.2.7
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HIC.TypeGuesser&version=1.2.7
 
Install as a Cake Addin
#tool nuget:?package=HIC.TypeGuesser&version=1.2.7
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Type Guesser

👁 Build, test and package
👁 Coverage Status
👁 NuGet Badge

Guess the C# Types for untyped strings e.g. "12.123".

Usage

var guesser = new Guesser();
guesser.AdjustToCompensateForValue("-12.211");
var guess = guesser.Guess;

The resulting guess in this case would be:

Property Value
guess.CSharpType typeof(decimal)
guess.Size.NumbersBeforeDecimalPlace 2
guess.Size.NumbersAfterDecimalPlace 3
guess.Width 7

Guesser also handles adjusting it's guess based on multiple input strings e.g.

var guesser = new Guesser();
guesser.AdjustToCompensateForValue("1,000");
guesser.AdjustToCompensateForValue("0.001");
var guess = guesser.Guess;
Property Value
guess.CSharpType typeof(decimal)
guess.Size.NumbersBeforeDecimalPlace 4
guess.Size.NumbersAfterDecimalPlace 3
guess.Width 8

Once you have guessed a Type for all your strings you can convert all your values to the hard type:

var someStrings = new []{"13:11:59", "9AM"};
var guesser = new Guesser();
guesser.AdjustToCompensateForValues(someStrings);

var parsed = someStrings.Select(guesser.Parse).ToArray();

Assert.AreEqual(new TimeSpan(13, 11, 59), parsed[0]);
Assert.AreEqual(new TimeSpan(9, 0, 0), parsed[1]);

String parsing/converting is largely handled by the excellent Universal Type Converter project by t-bruning.

Guess Order

The order in which Types are tried is (DatabaseTypeRequest.PreferenceOrder):

  • Bool
  • Int
  • Decimal
  • TimeSpan
  • DateTime
  • String

If a string has been accepted as one category e.g. "12" (Int) and an incompatible string arrived e.g. "0.1" then the Guess is changes to either the new Type (Decimal) or to String (i.e. untyped) based on whether the old and new Types are in the same TypeCompatibilityGroup

For example Bool and DateTime are incompatible

"Y" => Bool
"2001-01-01" => DateTime

Guess: String

Guesses are never revised back up again (once you accept a Decimal you never get Int again but you might end up at String)

Zero Prefixes

If an input string is a number that starts with zero e.g. "01" then the estimate will be changed to System.String. This is intended behaviour since some codes e.g. CHI / Barcodes have valid zero prefixes. If this is to be accurately preserved in the database then it must be stored as string (See TestGuesser_PreeceedingZeroes). This also applies to values such as "-01"

Whitespace

Leading and trailing whitespace is ignored for the purposes of determining Type. E.g. " 0.1" is a valid System.Decimal. However it is recorded for the maximum Length required if we later fallback to System.String (See Test TestGuesser_Whitespace).

Strong Typed Objects

Guesser.AdjustToCompensateForValue takes a System.Object. If you are passing objects that are not System.String e.g. from a DataColumn that has an actual Type on it (e.g. System.Float) then Guesser will set the Guess.CSharpType to the provided object Type. It will still calculate the Guess.Size properties if appropriate (See test TestGuesser_HardTypeFloats).

The first time you pass a typed object (excluding DBNull.Value) then it will assume the entire input stream is strongly typed (See IsPrimedWithBonafideType). Any attempts to pass in different object Types in future (or if strings were previously passed in before) will result in a MixedTypingException.

Product Versions Compatible and additional computed target framework versions.
.NET net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on HIC.TypeGuesser:

Package Downloads
HIC.FAnsiSql

FAnsiSql is a database management/ETL library that allows you to perform common SQL operations without having to know which Database Management System (DBMS) you are targetting (e.g. Sql Server, My Sql, Oracle).

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.7 26,267 10/11/2024
1.2.6 6,564 7/16/2024
1.2.5 210 7/11/2024
1.2.4 6,811 3/7/2024
1.2.3 517 2/1/2024
1.2.2 227 2/1/2024
1.2.1 237 1/29/2024
1.2.0 228 1/29/2024
1.1.0 12,643 5/16/2023
1.0.3 32,692 6/22/2022
1.0.2 79,776 9/16/2020
1.0.1 16,960 7/6/2020
0.0.5 781 11/1/2019
0.0.4 37,458 9/16/2019
0.0.3 6,723 9/10/2019
0.0.2 3,211 8/30/2019
0.0.1 780 8/29/2019