Skip to content
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Nothing yet.

## [1.21.4] - 2026-03-10

### Added

- Getting decimals and doubles from environment variables.

## [1.21.3] - 2026-01-23

### Fixed
Expand Down Expand Up @@ -317,7 +323,8 @@ Nothing yet.

- Implemented StringExtensions.

[unreleased]: https://github.com/Logitar/Logitar.NET/compare/v1.21.3...HEAD
[unreleased]: https://github.com/Logitar/Logitar.NET/compare/v1.21.4...HEAD
[1.21.4]: https://github.com/Logitar/Logitar.NET/compare/v1.21.3...v1.21.4
[1.21.3]: https://github.com/Logitar/Logitar.NET/compare/v1.21.2...v1.21.3
[1.21.2]: https://github.com/Logitar/Logitar.NET/compare/v1.21.1...v1.21.2
[1.21.1]: https://github.com/Logitar/Logitar.NET/compare/v1.21.0...v1.21.1
Expand Down
38 changes: 37 additions & 1 deletion src/Logitar/EnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,42 @@ public static class EnvironmentHelper
return !string.IsNullOrWhiteSpace(value) && bool.TryParse(value.Trim(), out bool boolean) ? boolean : null;
}

/// <summary>
/// Retrieves the decimal value of an environment variable.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the environment value.</returns>
public static decimal GetDecimal(string variable, decimal defaultValue = 0m) => TryGetDecimal(variable) ?? defaultValue;
/// <summary>
/// Retrieves the decimal value of an environment variable. Returns null if it is not found.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <returns>The value of the environment value, or null if not found.</returns>
public static decimal? TryGetDecimal(string variable)
{
string value = Environment.GetEnvironmentVariable(variable);
return !string.IsNullOrWhiteSpace(value) && decimal.TryParse(value.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out decimal number) ? number : null;
}

/// <summary>
/// Retrieves the double value of an environment variable.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The value of the environment value.</returns>
public static double GetDouble(string variable, double defaultValue = 0d) => TryGetDouble(variable) ?? defaultValue;
/// <summary>
/// Retrieves the double value of an environment variable. Returns null if it is not found.
/// </summary>
/// <param name="variable">The name of the variable.</param>
/// <returns>The value of the environment value, or null if not found.</returns>
public static double? TryGetDouble(string variable)
{
string value = Environment.GetEnvironmentVariable(variable);
return !string.IsNullOrWhiteSpace(value) && double.TryParse(value.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out double number) ? number : null;
}

/// <summary>
/// Retrieves the enum value of an environment variable.
/// </summary>
Expand Down Expand Up @@ -59,7 +95,7 @@ public static T GetEnum<T>(string variable, T defaultValue = default) where T :
public static int? TryGetInt32(string variable)
{
string value = Environment.GetEnvironmentVariable(variable);
return !string.IsNullOrWhiteSpace(value) && int.TryParse(value.Trim(), out int integer) ? integer : null;
return !string.IsNullOrWhiteSpace(value) && int.TryParse(value.Trim(), out int number) ? number : null;
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Logitar/Logitar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/Logitar/Logitar.NET</RepositoryUrl>
<AssemblyVersion>10.1.1.0</AssemblyVersion>
<AssemblyVersion>10.2.0.0</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Version>10.1.1</Version>
<Version>10.2.0</Version>
<NeutralLanguage>en-CA</NeutralLanguage>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageReleaseNotes>Fixed license year.</PackageReleaseNotes>
<PackageReleaseNotes>Getting decimals and doubles from environment variables.</PackageReleaseNotes>
<PackageTags>logitar net framework</PackageTags>
<PackageProjectUrl>https://github.com/Logitar/Logitar.NET/tree/dev/src/Logitar</PackageProjectUrl>
</PropertyGroup>
Expand Down
96 changes: 83 additions & 13 deletions tests/Logitar.UnitTests/EnvironmentHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,45 @@ public void Given_NotFound_When_GetBoolean_Then_DefaultValue(bool defaultValue)
Assert.Equal(defaultValue, variable);
}

[Fact(DisplayName = "GetDecimal: it should return the found variable value.")]
public void Given_Found_When_GetDecimal_Then_DecimalValue()
{
decimal value = 3.14159m;
Environment.SetEnvironmentVariable(VariableName, value.ToString());
decimal variable = EnvironmentHelper.GetDecimal(VariableName);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "GetDecimal: it should return the default variable value.")]
public void Given_NotFound_When_GetDecimal_Then_DefaultValue()
{
decimal defaultValue = -1m;
decimal variable = EnvironmentHelper.GetDecimal(VariableName, defaultValue);
Assert.Equal(defaultValue, variable);
}

[Fact(DisplayName = "GetDouble: it should return the found variable value.")]
public void Given_Found_When_GetDouble_Then_DoubleValue()
{
double value = 3.14159d;
Environment.SetEnvironmentVariable(VariableName, value.ToString());
double variable = EnvironmentHelper.GetDouble(VariableName);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "GetDouble: it should return the default variable value.")]
public void Given_NotFound_When_GetDouble_Then_DefaultValue()
{
double defaultValue = -1d;
double variable = EnvironmentHelper.GetDouble(VariableName, defaultValue);
Assert.Equal(defaultValue, variable);
}

[Theory(DisplayName = "GetEnum: it should return the found variable value.")]
[InlineData(DayOfWeek.Sunday)]
public void Given_Found_When_GetEnum_Then_BooleanValue(DayOfWeek value)
public void Given_Found_When_GetEnum_Then_EnumValue(DayOfWeek value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
DayOfWeek variable = EnvironmentHelper.GetEnum<DayOfWeek>(VariableName);
Expand All @@ -45,7 +81,7 @@ public void Given_NotFound_When_GetEnum_Then_DefaultValue(DayOfWeek defaultValue

[Theory(DisplayName = "GetInt32: it should return the found variable value.")]
[InlineData(42)]
public void Given_Found_When_GetInt32_Then_BooleanValue(int value)
public void Given_Found_When_GetInt32_Then_IntegerValue(int value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
int variable = EnvironmentHelper.GetInt32(VariableName);
Expand All @@ -63,7 +99,7 @@ public void Given_NotFound_When_GetInt32_Then_DefaultValue(int defaultValue)

[Theory(DisplayName = "GetString: it should return the found variable value.")]
[InlineData(" Test ")]
public void Given_Found_When_GetString_Then_BooleanValue(string value)
public void Given_Found_When_GetString_Then_StringValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value);
string variable = EnvironmentHelper.GetString(VariableName);
Expand All @@ -81,7 +117,7 @@ public void Given_NotFound_When_GetString_Then_DefaultValue(string defaultValue)

[Theory(DisplayName = "GetTimeSpan: it should return the found variable value.")]
[InlineData(" 12:34:56 ")]
public void Given_Found_When_GetTimeSpan_Then_BooleanValue(string value)
public void Given_Found_When_GetTimeSpan_Then_TimeSpanValue(string value)
{
TimeSpan timeSpan = TimeSpan.Parse(value);
Environment.SetEnvironmentVariable(VariableName, value);
Expand Down Expand Up @@ -112,14 +148,14 @@ public void Given_Found_When_TryGetBoolean_Then_BooleanValue(bool value)
}

[Fact(DisplayName = "TryGetBoolean: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetBoolean_Then_BooleanValue()
public void Given_NotFound_When_TryGetBoolean_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetBoolean(VariableName));
}

[Theory(DisplayName = "TryGetEnum: it should return the found variable value.")]
[InlineData(DayOfWeek.Saturday)]
public void Given_Found_When_TryGetEnum_Then_BooleanValue(DayOfWeek value)
public void Given_Found_When_TryGetEnum_Then_EnumValue(DayOfWeek value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
DayOfWeek? variable = EnvironmentHelper.TryGetEnum<DayOfWeek>(VariableName);
Expand All @@ -129,14 +165,48 @@ public void Given_Found_When_TryGetEnum_Then_BooleanValue(DayOfWeek value)
}

[Fact(DisplayName = "TryGetEnum: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetEnum_Then_BooleanValue()
public void Given_NotFound_When_TryGetEnum_Then_Nullalue()
{
Assert.Null(EnvironmentHelper.TryGetEnum<DayOfWeek>(VariableName));
}

[Fact(DisplayName = "TryGetDecimal: it should return the found variable value.")]
public void Given_Found_When_TryGetDecimal_Then_IntegerValue()
{
decimal value = 3.14159m;
Environment.SetEnvironmentVariable(VariableName, value.ToString());
decimal? variable = EnvironmentHelper.TryGetDecimal(VariableName);
Assert.NotNull(variable);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "TryGetDecimal: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetDecimal_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetDecimal(VariableName));
}

[Fact(DisplayName = "TryGetDouble: it should return the found variable value.")]
public void Given_Found_When_TryGetDouble_Then_IntegerValue()
{
double value = 3.14159d;
Environment.SetEnvironmentVariable(VariableName, value.ToString());
double? variable = EnvironmentHelper.TryGetDouble(VariableName);
Assert.NotNull(variable);
Assert.Equal(value, variable);
Environment.SetEnvironmentVariable(VariableName, null);
}

[Fact(DisplayName = "TryGetDouble: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetDouble_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetDouble(VariableName));
}

[Theory(DisplayName = "TryGetInt32: it should return the found variable value.")]
[InlineData(999)]
public void Given_Found_When_TryGetInt32_Then_BooleanValue(int value)
public void Given_Found_When_TryGetInt32_Then_IntegerValue(int value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
int? variable = EnvironmentHelper.TryGetInt32(VariableName);
Expand All @@ -146,14 +216,14 @@ public void Given_Found_When_TryGetInt32_Then_BooleanValue(int value)
}

[Fact(DisplayName = "TryGetInt32: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetInt32_Then_BooleanValue()
public void Given_NotFound_When_TryGetInt32_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetInt32(VariableName));
}

[Theory(DisplayName = "TryGetString: it should return the found variable value.")]
[InlineData(" Test ")]
public void Given_Found_When_TryGetString_Then_BooleanValue(string value)
public void Given_Found_When_TryGetString_Then_StringValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value.ToString());
string? variable = EnvironmentHelper.TryGetString(VariableName);
Expand All @@ -163,14 +233,14 @@ public void Given_Found_When_TryGetString_Then_BooleanValue(string value)
}

[Fact(DisplayName = "TryGetString: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetString_Then_BooleanValue()
public void Given_NotFound_When_TryGetString_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetString(VariableName));
}

[Theory(DisplayName = "TryGetTimeSpan: it should return the found variable value.")]
[InlineData(" 7.00:00:00 ")]
public void Given_Found_When_TryGetTimeSpan_Then_BooleanValue(string value)
public void Given_Found_When_TryGetTimeSpan_Then_TimeSpanValue(string value)
{
Environment.SetEnvironmentVariable(VariableName, value);
TimeSpan? variable = EnvironmentHelper.TryGetTimeSpan(VariableName);
Expand All @@ -180,7 +250,7 @@ public void Given_Found_When_TryGetTimeSpan_Then_BooleanValue(string value)
}

[Fact(DisplayName = "TryGetTimeSpan: it should return null when the variable is not found.")]
public void Given_NotFound_When_TryGetTimeSpan_Then_BooleanValue()
public void Given_NotFound_When_TryGetTimeSpan_Then_NullValue()
{
Assert.Null(EnvironmentHelper.TryGetTimeSpan(VariableName));
}
Expand Down