Skip to content

Commit

Permalink
Merge pull request #112 from MrDave1999/feature/issue_111
Browse files Browse the repository at this point in the history
Added conversion of properties to `UpperCaseSnakeCase`
  • Loading branch information
MrDave1999 committed Nov 17, 2022
2 parents 6c4040f + bec6844 commit bd0041a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Binder/EnvBinder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text;
using System;
using System.Collections.Generic;
using System.Reflection;
Expand Down Expand Up @@ -42,19 +43,22 @@ public TSettings Bind<TSettings>() where TSettings : new()
/// <inheritdoc />
public TSettings Bind<TSettings>(out EnvValidationResult result) where TSettings : new()
{
var settings = new TSettings();
var type = typeof(TSettings);
result = _validationResult;
var sb = new StringBuilder(capacity: 40);
var envVars = _configuration.EnvVars;
var settings = new TSettings();
var type = typeof(TSettings);
var properties = _configuration.BindNonPublicProperties ? type.GetPublicAndNonPublicProperties() : type.GetProperties();
result = _validationResult;
foreach (PropertyInfo property in properties)
{
if(IsReadOnlyOrWriteOnly(property))
continue;

var envKeyAttribute = (EnvKeyAttribute)Attribute.GetCustomAttribute(property, typeof(EnvKeyAttribute));
var variableName = envKeyAttribute is not null ? envKeyAttribute.Name : property.Name;
var retrievedValue = _configuration.EnvVars[variableName];

var variableName = envKeyAttribute is not null ? envKeyAttribute.Name : property.Name;
var retrievedValue = envVars[variableName];
retrievedValue ??= envKeyAttribute is not null ? retrievedValue : envVars[variableName.ToUpperCaseSnakeCase(sb)];
sb.Clear();
if (retrievedValue is null)
{
string errorMsg;
Expand Down
16 changes: 16 additions & 0 deletions src/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,21 @@ public static bool EndsWith(this string str, char value)
int len = str.Length;
return len != 0 && str[len - 1] == value;
}

/// <summary>
/// Converts from PascalCase to UpperCaseSnakeCase.
/// </summary>
/// <param name="str"></param>
/// <param name="sb"></param>
/// <returns></returns>
public static string ToUpperCaseSnakeCase(this string str, StringBuilder sb)
{
int len = str.Length;
sb.Append(str[0]);
for (int i = 1; i < len; i++)
sb.Append(char.IsUpper(str[i]) ? $"_{str[i]}" : char.ToUpper(str[i]));

return sb.ToString();
}
}
}
5 changes: 5 additions & 0 deletions tests/Binder/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public class AppSettings
private string IgnoredProperty { get; set; }
}

public class SettingsExample0
{
public string RealKey { get; set; }
}

public class SettingsExample1
{
public string SecretKey { get; set; }
Expand Down
10 changes: 10 additions & 0 deletions tests/Binder/EnvBinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ namespace DotEnv.Core.Tests.Binder;
[TestClass]
public class EnvBinderTests
{
[TestMethod]
public void Bind_WhenPropertyNameDoesNotMatchRealKey_ShouldReturnSettingsInstance()
{
SetEnvironmentVariable("REAL_KEY", "1234D");

var settings = new EnvBinder().Bind<SettingsExample0>();

Assert.AreEqual(expected: "1234D", actual: settings.RealKey);
}

[TestMethod]
public void Bind_WhenPropertiesAreLinkedToTheDefaultProviderInstance_ShouldReturnSettingsInstance()
{
Expand Down

0 comments on commit bd0041a

Please sign in to comment.