Skip to content

Commit

Permalink
Added support for sub-sections, this can be used to return the ini se…
Browse files Browse the repository at this point in the history
…ction, which contains a sub-section, but a the sub-section type.

[release]
  • Loading branch information
Lakritzator committed Nov 20, 2016
1 parent 269fdfb commit 540aee9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Dapplo.Config.Tests/ConfigTests/IniConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ public async Task TestIniAfterLoad()
Assert.True(iniTest.SomeValues["dapplo"] == 2015);
}

/// <summary>
/// This method tests that the initialization of the ini works.
/// Including the after load
/// </summary>
[Fact]
public async Task TestSubIni()
{
var iniConfig = Create();
await ConfigureMemoryStreamAsync();

var iniTest = await iniConfig.RegisterAndGetAsync<IIniConfigTest>().ConfigureAwait(false);

var subIniTest = iniConfig.GetSubSection<IIniConfigSubInterfaceTest>();
Assert.Equal("It works!", subIniTest.SubValuewithDefault);
}

/// <summary>
/// This method tests IIniSection events
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
#region using

using System.ComponentModel;
using Dapplo.Config.Ini;

#endregion

namespace Dapplo.Config.Tests.ConfigTests.Interfaces
{
public interface IIniConfigSubInterfaceTest
public interface IIniConfigSubInterfaceTest : ISubSection
{
string SubValue { get; set; }

Expand Down
1 change: 1 addition & 0 deletions Dapplo.Config/Dapplo.Config.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="Ini\IniSectionEventArgs.cs" />
<Compile Include="Ini\IniValue.cs" />
<Compile Include="Ini\IniConfig.cs" />
<Compile Include="Ini\ISubSection.cs" />
<Compile Include="Language\Implementation\ILanguageInternal.cs" />
<Compile Include="Support\TypeDescriptorContext.cs" />
<Compile Include="Language\ILanguage.cs" />
Expand Down
30 changes: 30 additions & 0 deletions Dapplo.Config/Ini/ISubSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Dapplo - building blocks for desktop applications
// Copyright (C) 2016 Dapplo
//
// For more information see: http://dapplo.net/
// Dapplo repositories are hosted on GitHub: https://github.com/dapplo
//
// This file is part of Dapplo.Config
//
// Dapplo.Config is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Dapplo.Config is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have a copy of the GNU Lesser General Public License
// along with Dapplo.Config. If not, see <http://www.gnu.org/licenses/lgpl.txt>.

namespace Dapplo.Config.Ini
{
/// <summary>
/// Marker interface for sub sections
/// </summary>
public interface ISubSection
{
}
}
14 changes: 13 additions & 1 deletion Dapplo.Config/Ini/IniConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,18 @@ public T Get<T>() where T : IIniSection
return (T) this[type];
}

/// <summary>
/// Get the IniSection which contains the sub section.
/// </summary>
/// <typeparam name="T">Type which extends ISubSection</typeparam>
/// <returns>T</returns>
public T GetSubSection<T>() where T : ISubSection
{
var type = typeof(T);

return (T)Sections.FirstOrDefault(s => type.IsInstanceOfType(s));
}

/// <summary>
/// Get the specified ini type
/// </summary>
Expand Down Expand Up @@ -872,7 +884,7 @@ internal void ResetInternal()
{
// Eventually set the values back, after save
Action<IIniSection> afterSaveAction;
if (intercepted != null && _afterLoadActions.TryGetValue(intercepted.InterceptedType, out afterSaveAction))
if (intercepted != null && _afterSaveActions.TryGetValue(intercepted.InterceptedType, out afterSaveAction))
{
afterSaveAction(iniSection);
}
Expand Down

0 comments on commit 540aee9

Please sign in to comment.