Skip to content

Commit

Permalink
Merge pull request #144 from holdenmai/65-CollectionAliases
Browse files Browse the repository at this point in the history
Issue 65: Picking up the attributes when transforming to a collection
  • Loading branch information
aloneguid authored Nov 4, 2022
2 parents adf833f + bb71920 commit e2ef7b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Config.Net.Tests/Stores/JsonFileCOnfigStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ public void Write_new_value_hierarchically()
Assert.Equal("111", _store.Read(key));
}

[Fact]
public void AliasesOnCollections()
{
IMyConfigUsingAliases myConfig = new ConfigurationBuilder<IMyConfigUsingAliases>()
.UseConfigStore(_store)
.Build();

Assert.NotNull(myConfig.Credentials);
foreach (ICredsWithAlias c in myConfig.Credentials)
{
if (c.Name == "user1")
{
Assert.Equal("pass1", c.Pass);
}
else if (c.Name == "user2")
{
Assert.Equal("pass2", c.Pass);
}
else
{
Assert.Equal("user1", c.Name);
}
}

[Fact]
public void TestCreatingFileInMissingFolder()
{
Expand All @@ -76,4 +100,17 @@ public void Dispose()
_store.Dispose();
}
}
public interface ICredsWithAlias
{
[Option(Alias = "Username")]
string Name { get; set; }
[Option(Alias = "Password")]
string Pass { get; set; }
}

public interface IMyConfigUsingAliases
{
[Option(Alias = "Creds")]
IEnumerable<ICredsWithAlias> Credentials { get; }
}
}
1 change: 1 addition & 0 deletions src/Config.Net/Core/Box/BoxFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private static void DiscoverProperties(Type t, ValueHandler valueHandler, Dictio
if(isCollection)
{
rbox = new CollectionResultBox(pi.Name, rbox);
AddAttributes(rbox, pi, valueHandler);
}

result[pi.Name] = rbox;
Expand Down

0 comments on commit e2ef7b2

Please sign in to comment.