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.
paket add HIC.TypeGuesser --version 1.2.7                
#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.
// Install HIC.TypeGuesser as a Cake Addin
#addin nuget:?package=HIC.TypeGuesser&version=1.2.7

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

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.
  • 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 2,094 10/11/2024
1.2.6 6,001 7/16/2024
1.2.5 99 7/11/2024
1.2.4 6,216 3/7/2024
1.2.3 385 2/1/2024
1.2.2 118 2/1/2024
1.2.1 134 1/29/2024
1.2.0 115 1/29/2024
1.1.0 12,074 5/16/2023
1.0.3 30,915 6/22/2022
1.0.2 74,585 9/16/2020
1.0.1 15,491 7/6/2020
0.0.5 552 11/1/2019
0.0.4 34,335 9/16/2019
0.0.3 5,880 9/10/2019
0.0.2 2,815 8/30/2019
0.0.1 547 8/29/2019