-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from zhouyuguangsc/v3.0.0
update version to 3.0.0
- Loading branch information
Showing
304 changed files
with
2,397 additions
and
121,858 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Nebula.Graph; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NebulaNet | ||
{ | ||
public static class EnumerableExtensions | ||
{ | ||
public static async Task<T[]> ToArrayAsync<T>(this Task<ExecutionResponse> executionTask) | ||
{ | ||
var executionResponse = await executionTask; | ||
if (executionResponse.Data == null) | ||
return default; | ||
if (executionResponse.Data.Rows.Count != 1) | ||
return default;//不能解析多行 | ||
|
||
return (T[])executionResponse.Data.Rows[0].Values[0].Mapping(typeof(T[])); | ||
} | ||
public static async Task<IList<T>> ToListAsync<T>(this Task<ExecutionResponse> executionTask) | ||
{ | ||
var executionResponse = await executionTask; | ||
if (executionResponse.Data == null) | ||
return default; | ||
|
||
//获取列名 | ||
var columnNames = executionResponse.Data.Column_names | ||
.Select(x => Encoding.UTF8.GetString(x).ToLower()).ToArray(); | ||
|
||
//查找可用属性和数据索引 | ||
var indexAndProps = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)//BindingFlags.IgnoreCase | ||
.Select(x => new { Index = Array.IndexOf(columnNames, x.Name.ToLower()), Prop = x }) | ||
.Where(x => x.Index >= 0); | ||
|
||
//映射对象 | ||
var result = new List<T>(); | ||
foreach (var row in executionResponse.Data.Rows) | ||
{ | ||
var o = Activator.CreateInstance<T>(); | ||
foreach (var item in indexAndProps) | ||
{ | ||
item.Prop.SetValue(o, row.Values[item.Index].Mapping(item.Prop.PropertyType)); | ||
} | ||
result.Add(o); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.Extensions.ObjectPool; | ||
using NebulaNet; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class NebulaServiceExtension | ||
{ | ||
public static IServiceCollection AddNebulaGraph(this IServiceCollection services,Action<NebulaConfig> configProvider) | ||
{ | ||
var config = new NebulaConfig(configProvider); | ||
|
||
services.AddSingleton<ObjectPool<NebulaConnection>>(serviceProvider => | ||
{ | ||
var objectPoolProvider = new DefaultObjectPoolProvider(); | ||
objectPoolProvider.MaximumRetained = 30; | ||
return objectPoolProvider.Create(new NebulaConnPoolPolicy(config)); | ||
}); | ||
services.AddSingleton<ObjectPool<SessionId>>(serviceProvider => | ||
{ | ||
var objectPoolProvider=new DefaultObjectPoolProvider(); | ||
objectPoolProvider.MaximumRetained = 30; | ||
return objectPoolProvider.Create(new NebulaSessionIdPoolPolicy()); | ||
}); | ||
services.AddSingleton(serviceProvider => | ||
{ | ||
var connPool = serviceProvider.GetRequiredService<ObjectPool<NebulaConnection>>(); | ||
var sessionIdPool = serviceProvider.GetRequiredService<ObjectPool<SessionId>>(); | ||
return new NebulaPool(connPool, sessionIdPool, config); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
using Nebula.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace NebulaNet | ||
{ | ||
internal static class ValueMappingExtensions | ||
{ | ||
internal static object Mapping(this @Value value, Type targetType) | ||
{ | ||
Type mapType = null; | ||
switch (targetType.Name) | ||
{ | ||
case "Bool": | ||
return value.BVal; | ||
case "Int64": | ||
return value.IVal; | ||
case "Double": | ||
return value.FVal; | ||
case "String": | ||
return Encoding.UTF8.GetString(value.SVal); | ||
case "Nebula.Common.Date": | ||
return value.DVal; | ||
case "Nebula.Common.Time": | ||
return value.TVal; | ||
case "DateTime": | ||
return value.DtVal; | ||
case "Vertex": | ||
return new NotImplementedException(); //value.VVal; | ||
case "Edge": | ||
return new NotImplementedException(); // value.EVal; | ||
case "Path": | ||
return new NotImplementedException(); // value.PVal; | ||
case "Dictionary": | ||
return new NotImplementedException(); //value.MVal; | ||
case "HashSet": | ||
return new NotImplementedException(); //value.UVal; | ||
case "DataSet": | ||
return new NotImplementedException(); //value.GVal; | ||
case "Geography": | ||
return new NotImplementedException(); //value.GgVal; | ||
case "Duration": | ||
return new NotImplementedException(); //value.DuVal; | ||
case "List`1"://List<T> | ||
mapType = targetType.GetGenericArguments()[0]; | ||
if (mapType.Name== "Int64") | ||
{ | ||
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<long>().ToList(); | ||
} | ||
if (mapType.Name == "String") | ||
{ | ||
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<string>().ToList(); | ||
} | ||
return new TypeLoadException(); | ||
default: | ||
if (targetType.IsArray)//数组 | ||
{ | ||
mapType = targetType.GetElementType(); | ||
if (mapType.Name == "Int64") | ||
{ | ||
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<long>().ToArray(); | ||
} | ||
if (mapType.Name == "String") | ||
{ | ||
return value.LVal.Values.Select(x => x.Mapping(mapType)).Cast<string>().ToArray(); | ||
} | ||
return new TypeLoadException(); | ||
} | ||
return new TypeLoadException(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 将一个对象转换为指定类型 | ||
/// </summary> | ||
/// <param name="obj">待转换的对象</param> | ||
/// <param name="type">目标类型</param> | ||
/// <returns>转换后的对象</returns> | ||
private static object ConvertToObject(object obj, Type type) | ||
{ | ||
if (type == null) return obj; | ||
if (obj == null) return type.IsValueType ? Activator.CreateInstance(type) : null; | ||
|
||
Type underlyingType = Nullable.GetUnderlyingType(type); | ||
// 如果待转换对象的类型与目标类型兼容,则无需转换 | ||
if (type.IsInstanceOfType(obj)) | ||
{ | ||
return obj; | ||
} | ||
// 如果待转换的对象的基类型为枚举 | ||
else if ((underlyingType ?? type).IsEnum) | ||
{ | ||
// 如果目标类型为可空枚举,并且待转换对象为null 则直接返回null值 | ||
if (underlyingType != null && string.IsNullOrEmpty(obj.ToString())) | ||
{ | ||
return null; | ||
} | ||
else | ||
{ | ||
return Enum.Parse(underlyingType ?? type, obj.ToString()); | ||
} | ||
} | ||
// 如果目标类型的基类型实现了IConvertible,则直接转换 | ||
else if (typeof(IConvertible).IsAssignableFrom(underlyingType ?? type)) | ||
{ | ||
try | ||
{ | ||
return Convert.ChangeType(obj, underlyingType ?? type, null); | ||
} | ||
catch | ||
{ | ||
return underlyingType == null ? Activator.CreateInstance(type) : null; | ||
} | ||
} | ||
else | ||
{ | ||
TypeConverter converter = TypeDescriptor.GetConverter(type); | ||
if (converter.CanConvertFrom(obj.GetType())) | ||
{ | ||
return converter.ConvertFrom(obj); | ||
} | ||
ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes); | ||
if (constructor != null) | ||
{ | ||
object o = constructor.Invoke(null); | ||
PropertyInfo[] propertys = type.GetProperties(); | ||
Type oldType = obj.GetType(); | ||
foreach (PropertyInfo property in propertys) | ||
{ | ||
PropertyInfo p = oldType.GetProperty(property.Name); | ||
if (property.CanWrite && p != null && p.CanRead) | ||
{ | ||
property.SetValue(o, ConvertToObject(p.GetValue(obj, null), property.PropertyType), null); | ||
} | ||
} | ||
return o; | ||
} | ||
} | ||
return obj; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.