Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
Added summary to ConfigDataSource attribute
Added AsCustomFromRaw method
Changed all As* methods to use parsed data instead of raw one
Changed AssemblyVersion to 0.6
  • Loading branch information
AgathokakologicalBit committed Aug 6, 2017
1 parent 4177e85 commit 1d7fb5f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
7 changes: 7 additions & 0 deletions ConfigManager/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ namespace ConfigManager
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public class ConfigDataSource : Attribute
{
/// <summary>
/// Hold Config value path as data source
/// </summary>
public string DataPath { get; private set; } = "";

/// <summary>
/// Specifies path from wich data will be loaded into target field
/// </summary>
/// <param name="path">Path to value(-s)</param>
public ConfigDataSource(string path)
{
if (path == null)
Expand Down
2 changes: 1 addition & 1 deletion ConfigManager/ConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static Config()
{
methodLoadToClass = typeof(Config).GetMethod("LoadToClass", new[] { typeof(ConfigValue) });
methodLoadToCollection = typeof(Config).GetMethod("LoadToCollection");
methodAsCustom = typeof(ConfigValue).GetMethod("AsCustom");
methodAsCustom = typeof(ConfigValue).GetMethod("AsCustomFromRaw");
}

#region Loaders
Expand Down
42 changes: 30 additions & 12 deletions ConfigManager/ConfigValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public List<ConfigValue> GetAllByPath(string path)
int position = 0;
while (position < pathLower.Length)
{
if (Char.IsDigit(pathLower[0]))
if (Char.IsDigit(pathLower[position]))
{
string indexStr = new string(
pathLower.Skip(position).TakeWhile(Char.IsDigit).ToArray()
Expand All @@ -194,7 +194,7 @@ public List<ConfigValue> GetAllByPath(string path)
targets.Add(newTarget);
position += indexStr.Length;
}
else if (Char.IsLetter(pathLower[0]))
else if (Char.IsLetter(pathLower[position]))
{
string key = new string(
pathLower.Skip(position).TakeWhile(Char.IsLetter).ToArray()
Expand All @@ -208,11 +208,11 @@ public List<ConfigValue> GetAllByPath(string path)
targets = newTargets;
position += key.Length;
}
else if (pathLower[0] == '.')
else if (pathLower[position] == '.')
{
position += 1;
}
else if (pathLower[0] == '$' && Char.IsDigit(pathLower.ElementAtOrDefault(1)))
else if (pathLower[position] == '$' && Char.IsDigit(pathLower.ElementAtOrDefault(1)))
{
string indexStr = new string(
pathLower.Skip(position).TakeWhile(c => Char.IsDigit(c) || c == '$').ToArray()
Expand Down Expand Up @@ -346,39 +346,45 @@ public bool ContainsPath(string path)
/// Gets data as raw string
/// </summary>
/// <returns>String data</returns>
public string AsString() => _data;
public string AsRawString() => _data;

/// <summary>
/// Gets data as parsed string
/// </summary>
/// <returns>String data</returns>
public string AsString() => _parsedData[0]._data;
/// <summary>
/// Gets data as escaped raw string
/// Gets data as escaped parsed string
/// </summary>
/// <returns>Escaped string data</returns>
public string AsEscapedString() => EscapeString(_data);
public string AsEscapedString() => EscapeString(_parsedData[0]._data);

/// <summary>
/// Gets data as boolean value
/// </summary>
/// <returns>Boolean value</returns>
public bool AsBoolean() => bool.Parse(_data);
public bool AsBoolean() => bool.Parse(_parsedData[0]._data);

/// <summary>
/// Gets data as 32 bit integer(int) value
/// </summary>
/// <returns>Int32 value</returns>
public Int32 AsInt() => Int32.Parse(_data);
public Int32 AsInt() => Int32.Parse(_parsedData[0]._data);
/// <summary>
/// Gets data as 64 bit integer(long) value
/// </summary>
/// <returns>Int64 value</returns>
public Int64 AsLong() => Int64.Parse(_data);
public Int64 AsLong() => Int64.Parse(_parsedData[0]._data);
/// <summary>
/// Gets data as single precision floating point(float) value
/// </summary>
/// <returns>Single precision floating point value</returns>
public float AsFloat() => Single.Parse(_data);
public float AsFloat() => Single.Parse(_parsedData[0]._data);
/// <summary>
/// Gets data as double precision floating point(double) value
/// </summary>
/// <returns>Double precision floating point value</returns>
public double AsDouble() => Double.Parse(_data);
public double AsDouble() => Double.Parse(_parsedData[0]._data);

/// <summary>
/// Gets data as a list of ConfigValues.
Expand Down Expand Up @@ -413,6 +419,18 @@ public bool ContainsPath(string path)
/// </summary>
/// <returns>Converted data</returns>
public T AsCustom<T>()
{
var tc = TypeDescriptor.GetConverter(typeof(T));
return (T)tc.ConvertFromString(_parsedData[0]._data);
}

/// <summary>
/// Gets RAW data as value with specified type.
///
/// Throws NotSupportedException if data can not be converted to target type.
/// </summary>
/// <returns>Converted raw data</returns>
public T AsCustomFromRaw<T>()
{
var tc = TypeDescriptor.GetConverter(typeof(T));
return (T)tc.ConvertFromString(_data);
Expand Down
4 changes: 2 additions & 2 deletions ConfigManager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("0.6.0.0")]
[assembly: AssemblyFileVersion("0.6.0.0")]

0 comments on commit 1d7fb5f

Please sign in to comment.