HIC.TypeGuesser 1.2.4

dotnet add package HIC.TypeGuesser --version 1.2.4
NuGet\Install-Package HIC.TypeGuesser -Version 1.2.4
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.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HIC.TypeGuesser --version 1.2.4
#r "nuget: HIC.TypeGuesser, 1.2.4"
#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.
// Install HIC.TypeGuesser as a Cake Addin
#addin nuget:?package=HIC.TypeGuesser&version=1.2.4

// Install HIC.TypeGuesser as a Cake Tool
#tool nuget:?package=HIC.TypeGuesser&version=1.2.4

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 Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.4 1,118 3/7/2024
1.2.3 322 2/1/2024
1.2.2 83 2/1/2024
1.2.1 91 1/29/2024
1.2.0 74 1/29/2024
1.1.0 10,267 5/16/2023
1.0.3 29,280 6/22/2022
1.0.2 70,642 9/16/2020
1.0.1 14,912 7/6/2020
0.0.5 521 11/1/2019
0.0.4 32,562 9/16/2019
0.0.3 5,384 9/10/2019
0.0.2 2,665 8/30/2019
0.0.1 512 8/29/2019