Configuration bind

Append configset Bind
pull/1/head
Ogoun 5 years ago
parent 96bc650326
commit bf039e2dde

@ -7,8 +7,9 @@ namespace ConfigurationTests
public string Url; public string Url;
public int BatchSize; public int BatchSize;
public IEnumerable<string> Sheme; public IEnumerable<string> Sheme;
public IEnumerable<int> Port; public int[] Port;
public ServiceConfig Service; public ServiceConfig Service;
public IEnumerable<int> List;
} }
public class ServiceConfig public class ServiceConfig

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using ZeroLevel; using ZeroLevel;
namespace ConfigurationTests namespace ConfigurationTests
@ -10,21 +8,7 @@ namespace ConfigurationTests
static void Main(string[] args) static void Main(string[] args)
{ {
var list = new List<int>(); var config = Configuration.ReadSetFromIniFile("config.ini").Bind<AppConfig>();
for (int i = 0; i < 100; i++)
{
list.Add(i);
}
var collection = list.Chunkify(6).ToList();
foreach (var t in collection)
{
Console.WriteLine(string.Join("; ", t.Select(n => n.ToString("D2"))));
}
Console.ReadKey();
return;
var config = Configuration.ReadFromIniFile("config.ini").Bind<AppConfig>();
Console.WriteLine(config.Url); Console.WriteLine(config.Url);
Console.WriteLine(config.BatchSize); Console.WriteLine(config.BatchSize);
Console.WriteLine("Ports"); Console.WriteLine("Ports");
@ -37,7 +21,16 @@ namespace ConfigurationTests
{ {
Console.WriteLine($"\t{sheme}"); Console.WriteLine($"\t{sheme}");
} }
Console.WriteLine($"Range list: {string.Join(", ", config.List)}");
Console.WriteLine("Service");
Console.WriteLine($"\tAppKey: {config.Service.AppKey}");
Console.WriteLine($"\tAppName: {config.Service.AppName}");
Console.WriteLine($"\tServiceGroup: {config.Service.ServiceGroup}");
Console.WriteLine($"\tServiceType: {config.Service.ServiceType}");
Console.ReadKey(); Console.ReadKey();
} }
} }
} }

@ -6,9 +6,10 @@ sheme=https
port=80 port=80
port=90 port=90
port=8800 port=8800
list=1-5,7,9
[service] [service]
AppName=TestApp AppName=TestApp
AppKey=test.app AppKey=test.app
ServiceGroup=seo ServiceGroup=System
ServiceType=site ServiceType=service

@ -65,9 +65,6 @@ namespace ZeroLevel.Services.Collections
private class ArrayBuilder private class ArrayBuilder
: IArrayBuilder : IArrayBuilder
{ {
private readonly IInvokeWrapper _wrapper;
private readonly Invoker _insert;
private readonly object _instance; private readonly object _instance;
public ArrayBuilder(Type entityType, int count) public ArrayBuilder(Type entityType, int count)

@ -463,10 +463,11 @@ namespace ZeroLevel.Services.Config
} }
} }
public T Bind<T>() public T Bind<T>() => (T)Bind(typeof(T));
public object Bind(Type type)
{ {
var mapper = TypeMapper.Create<T>(true); var mapper = TypeMapper.Create(type, true);
var instance = (T)TypeHelpers.CreateInitialState(typeof(T)); var instance = TypeHelpers.CreateInitialState(type);
mapper.TraversalMembers(member => mapper.TraversalMembers(member =>
{ {
if (Contains(member.Name)) if (Contains(member.Name))
@ -476,7 +477,33 @@ namespace ZeroLevel.Services.Config
{ {
case 0: return; case 0: return;
case 1: // field case 1: // field
member.Setter(instance, First(member.Name)); if (TypeHelpers.IsArray(member.ClrType))
{
var itemType = member.ClrType.GetElementType();
var elements = SplitRange(First(member.Name), itemType).ToArray();
var arrayBuilder = CollectionFactory.CreateArray(itemType, elements.Length);
int index = 0;
foreach (var item in elements)
{
arrayBuilder.Set(item, index);
index++;
}
member.Setter(instance, arrayBuilder.Complete());
}
else if (TypeHelpers.IsEnumerable(member.ClrType) && member.ClrType != typeof(string))
{
var itemType = member.ClrType.GenericTypeArguments.First();
var collectionBuilder = CollectionFactory.Create(itemType);
foreach (var item in SplitRange(First(member.Name), itemType))
{
collectionBuilder.Append(item);
}
member.Setter(instance, collectionBuilder.Complete());
}
else
{
member.Setter(instance, First(member.Name));
}
break; break;
default: // array, or first default: // array, or first
if (TypeHelpers.IsArray(member.ClrType)) if (TypeHelpers.IsArray(member.ClrType))
@ -526,5 +553,34 @@ namespace ZeroLevel.Services.Config
}); });
return instance; return instance;
} }
private static IEnumerable<object> SplitRange(string line, Type elementType)
{
if (string.IsNullOrWhiteSpace(line)) yield return StringToTypeConverter.TryConvert(line, elementType);
foreach (var part in line.Split(','))
{
if (part.IndexOf('-') >= 0)
{
var lr = part.Split('-');
if (lr.Length == 2)
{
long left = (long)Convert.ChangeType(StringToTypeConverter.TryConvert(lr[0], elementType), typeof(long));
long right = (long)Convert.ChangeType(StringToTypeConverter.TryConvert(lr[1], elementType), typeof(long));
for (; left <= right; left++)
{
yield return Convert.ChangeType(left, elementType);
}
}
else
{
// incorrect string
yield break;
}
}
else
{
yield return StringToTypeConverter.TryConvert(part, elementType);
}
}
}
} }
} }

@ -2,6 +2,8 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using ZeroLevel.Services.ObjectMapping;
using ZeroLevel.Services.Reflection;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Services.Config namespace ZeroLevel.Services.Config
@ -260,7 +262,16 @@ namespace ZeroLevel.Services.Config
public T Bind<T>() public T Bind<T>()
{ {
return default; var mapper = TypeMapper.Create<T>(true);
var instance = Default.Bind<T>();
mapper.TraversalMembers(member =>
{
if (ContainsSection(member.Name))
{
member.Setter(instance, GetSection(member.Name).Bind(member.ClrType));
}
});
return instance;
} }
} }
} }

@ -143,5 +143,6 @@ namespace ZeroLevel
void CopyTo(IConfiguration config); void CopyTo(IConfiguration config);
T Bind<T>(); T Bind<T>();
object Bind(Type type);
} }
} }

@ -39,15 +39,9 @@ Append DumpStorage (fast temporary journal)</PackageReleaseNotes>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Buffers" Version="4.5.0" /> <PackageReference Include="System.Buffers" Version="4.5.0" />
<PackageReference Include="System.Memory" Version="4.5.3" /> <PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.6.0" /> <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="4.6.0" /> <PackageReference Include="System.ServiceProcess.ServiceController" Version="4.7.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" /> <PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Win32.Registry">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.win32.registry\4.5.0\ref\netstandard2.0\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save

Powered by TurnKey Linux.