Add Config.GetValueOrDefault() extensions

This commit is contained in:
Keith Dahlby 2013-08-25 13:04:13 -05:00
Родитель 885de34f54
Коммит 72a4c15229
2 изменённых файлов: 182 добавлений и 0 удалений

Просмотреть файл

@ -110,6 +110,11 @@ namespace LibGit2Sharp.Tests
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.True(repo.Config.Get<bool>("core.ignorecase").Value);
Assert.True(repo.Config.GetValueOrDefault<bool>("core.ignorecase"));
Assert.Equal(false, repo.Config.GetValueOrDefault<bool>("missing.key"));
Assert.Equal(true, repo.Config.GetValueOrDefault<bool>("missing.key", true));
Assert.Equal(true, repo.Config.GetValueOrDefault<bool>("missing.key", () => true));
}
}
@ -119,6 +124,12 @@ namespace LibGit2Sharp.Tests
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal(2, repo.Config.Get<int>("unittests.intsetting").Value);
Assert.Equal(2, repo.Config.GetValueOrDefault<int>("unittests.intsetting"));
Assert.Equal(2, repo.Config.GetValueOrDefault<int>("unittests.intsetting", ConfigurationLevel.Local));
Assert.Equal(0, repo.Config.GetValueOrDefault<int>("missing.key"));
Assert.Equal(4, repo.Config.GetValueOrDefault<int>("missing.key", 4));
Assert.Equal(4, repo.Config.GetValueOrDefault<int>("missing.key", () => 4));
}
}
@ -128,6 +139,11 @@ namespace LibGit2Sharp.Tests
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal(15234, repo.Config.Get<long>("unittests.longsetting").Value);
Assert.Equal(15234, repo.Config.GetValueOrDefault<long>("unittests.longsetting"));
Assert.Equal(0, repo.Config.GetValueOrDefault<long>("missing.key"));
Assert.Equal(4, repo.Config.GetValueOrDefault<long>("missing.key", 4));
Assert.Equal(4, repo.Config.GetValueOrDefault<long>("missing.key", () => 4));
}
}
@ -138,6 +154,35 @@ namespace LibGit2Sharp.Tests
{
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote.origin.fetch").Value);
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote", "origin", "fetch").Value);
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>("remote.origin.fetch"));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>("remote.origin.fetch", ConfigurationLevel.Local));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>("remote", "origin", "fetch"));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.GetValueOrDefault<string>(new[] { "remote", "origin", "fetch" }));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key"));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key", default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>("missing.key", default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", () => "value"));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing.key", ConfigurationLevel.Local, () => "value"));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing", "config", "key"));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>("missing", "config", "key", default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>("missing", "config", "key", default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing", "config", "key", "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>("missing", "config", "key", () => "value"));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }));
Assert.Equal(null, repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, default(Func<string>)));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, "value"));
Assert.Equal("value", repo.Config.GetValueOrDefault<string>(new[] { "missing", "key" }, () => "value"));
}
}

Просмотреть файл

@ -1,3 +1,4 @@
using System;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
@ -66,5 +67,141 @@ namespace LibGit2Sharp
return config.Get<T>(new[] { firstKeyPart, secondKeyPart, thirdKeyPart });
}
/// <summary>
/// Get a configuration value for the given key,
/// or <paramref name="defaultValue" /> if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="key">The key</param>
/// <param name="defaultValue">The default value if the key is not set.</param>
/// <returns>The configuration value, or the default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string key, T defaultValue = default(T))
{
return ValueOrDefault(config.Get<T>(key), defaultValue);
}
/// <summary>
/// Get a configuration value for the given key,
/// or <paramref name="defaultValue" /> if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="key">The key.</param>
/// <param name="level">The configuration file into which the key should be searched for.</param>
/// <param name="defaultValue">The selector used to generate a default value if the key is not set.</param>
/// <returns>The configuration value, or the default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string key, ConfigurationLevel level, T defaultValue = default(T))
{
return ValueOrDefault(config.Get<T>(key, level), defaultValue);
}
/// <summary>
/// Get a configuration value for the given key parts,
/// or <paramref name="defaultValue" /> if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="keyParts">The key parts.</param>
/// <param name="defaultValue">The default value if the key is not set.</param>
/// <returns>The configuration value, or the default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string[] keyParts, T defaultValue = default(T))
{
return ValueOrDefault(config.Get<T>(keyParts), defaultValue);
}
/// <summary>
/// Get a configuration value for the given key parts,
/// or <paramref name="defaultValue" /> if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="firstKeyPart">The first key part.</param>
/// <param name="secondKeyPart">The second key part.</param>
/// <param name="thirdKeyPart">The third key part.</param>
/// <param name="defaultValue">The default value if the key is not set.</param>
/// <returns>The configuration value, or the default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string firstKeyPart, string secondKeyPart, string thirdKeyPart, T defaultValue = default(T))
{
return ValueOrDefault(config.Get<T>(firstKeyPart, secondKeyPart, thirdKeyPart), defaultValue);
}
/// <summary>
/// Get a configuration value for the given key,
/// or a value generated by <paramref name="defaultValueSelector" />
/// if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="key">The key</param>
/// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
/// <returns>The configuration value, or a generated default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string key, Func<T> defaultValueSelector)
{
return ValueOrDefault(config.Get<T>(key), defaultValueSelector);
}
/// <summary>
/// Get a configuration value for the given key,
/// or a value generated by <paramref name="defaultValueSelector" />
/// if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="key">The key.</param>
/// <param name="level">The configuration file into which the key should be searched for.</param>
/// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
/// <returns>The configuration value, or a generated default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string key, ConfigurationLevel level, Func<T> defaultValueSelector)
{
return ValueOrDefault(config.Get<T>(key, level), defaultValueSelector);
}
/// <summary>
/// Get a configuration value for the given key parts,
/// or a value generated by <paramref name="defaultValueSelector" />
/// if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="keyParts">The key parts.</param>
/// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
/// <returns>The configuration value, or a generated default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string[] keyParts, Func<T> defaultValueSelector)
{
return ValueOrDefault(config.Get<T>(keyParts), defaultValueSelector);
}
/// <summary>
/// Get a configuration value for the given key parts,
/// or a value generated by <paramref name="defaultValueSelector" />
/// if the key is not set.
/// </summary>
/// <typeparam name="T">The configuration value type.</typeparam>
/// <param name="config">The configuration being worked with.</param>
/// <param name="firstKeyPart">The first key part.</param>
/// <param name="secondKeyPart">The second key part.</param>
/// <param name="thirdKeyPart">The third key part.</param>
/// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
/// <returns>The configuration value, or a generated default.</returns>
public static T GetValueOrDefault<T>(this Configuration config, string firstKeyPart, string secondKeyPart, string thirdKeyPart, Func<T> defaultValueSelector)
{
return ValueOrDefault(config.Get<T>(firstKeyPart, secondKeyPart, thirdKeyPart), defaultValueSelector);
}
private static T ValueOrDefault<T>(ConfigurationEntry<T> value, T defaultValue)
{
return value == null ? defaultValue : value.Value;
}
private static T ValueOrDefault<T>(ConfigurationEntry<T> value, Func<T> defaultValueSelector)
{
Ensure.ArgumentNotNull(defaultValueSelector, "defaultValueSelector");
return value == null
? defaultValueSelector()
: value.Value;
}
}
}