复杂类型增量源生成器

This commit is contained in:
ChenYi 2025-05-07 17:20:10 +08:00
parent 6b012d9303
commit ff517664fe
3 changed files with 113 additions and 19 deletions

View File

@ -162,13 +162,19 @@ namespace JiShe.CollectBus.IncrementalGenerator
code.AppendLine($"namespace {classSymbol.ContainingNamespace.ToDisplayString()};");
code.AppendLine();
// 处理泛型类型参数
// 处理泛型类型名称
var accessibility = classSymbol.DeclaredAccessibility switch
{
Accessibility.Public => "public",
_ => "internal"
};
var genericParams = classSymbol.IsGenericType
? $"<{string.Join(", ", classSymbol.TypeParameters.Select(t => t.Name))}>"
: "";
code.AppendLine(
$"public sealed class {classSymbol.Name}Accessor{genericParams} " +
$"{accessibility} sealed class {classSymbol.Name}Accessor{genericParams} " + // 保留泛型参数
$": ISourceEntityAccessor<{classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>");
code.AppendLine("{");
@ -306,26 +312,32 @@ namespace JiShe.CollectBus.IncrementalGenerator
public static class SourceEntityAccessorFactory
{
private static readonly ConcurrentDictionary<Type, object> _accessors = new();
public static ISourceEntityAccessor<T> GetAccessor<T>()
{
return (ISourceEntityAccessor<T>)_accessors.GetOrAdd(typeof(T), t =>
{
var typeName = $"{t.Namespace}.{t.Name}Accessor";
if (t.IsGenericType)
{
var genericArgs = t.GetGenericArguments();
var genericDef = t.GetGenericTypeDefinition();
typeName = $"{genericDef.Namespace}.{genericDef.Name}Accessor`{genericArgs.Length}";
}
var type = Type.GetType(typeName)
?? Assembly.GetAssembly(t)?.GetType(typeName)
// 获取泛型类型定义信息(如果是泛型类型)
var isGeneric = t.IsGenericType;
var genericTypeDef = isGeneric ? t.GetGenericTypeDefinition() : null;
var arity = isGeneric ? genericTypeDef!.GetGenericArguments().Length : 0;
// 构建访问器类名
var typeName = isGeneric
? $"{t.Namespace}.{genericTypeDef!.Name.Split('`')[0]}Accessor`{arity}"
: $"{t.Namespace}.{t.Name}Accessor";
// 尝试从当前程序集加载
var accessorType = Assembly.GetAssembly(t)!.GetType(typeName)
?? throw new InvalidOperationException($"Accessor type {typeName} not found");
return t.IsGenericType
? Activator.CreateInstance(type.MakeGenericType(t.GetGenericArguments()))
: Activator.CreateInstance(type);
// 处理泛型参数
if (isGeneric && accessorType.IsGenericTypeDefinition)
{
accessorType = accessorType.MakeGenericType(t.GetGenericArguments());
}
return Activator.CreateInstance(accessorType)!;
});
}
}

View File

@ -0,0 +1,77 @@
// <auto-generated/>
#nullable enable
using System;
using System.Collections.Generic;
using JiShe.CollectBus.Analyzers.Shared;
namespace JiShe.CollectBus.IoTDB.Model;
public sealed class TableModelSingleMeasuringEntityAccessor3<T> : ISourceEntityAccessor<global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T>>
{
public static string GetSingleColumn_Item1(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> obj) => obj.SingleColumn.Item1;
public static void SetSingleColumn_Item1(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> obj, string value) => obj.SingleColumn = (value, obj.SingleColumn.Item2);
public static T GetSingleColumn_Item2(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> obj) => obj.SingleColumn.Item2;
public static void SetSingleColumn_Item2(global::JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> obj, T value) => obj.SingleColumn = (obj.SingleColumn.Item1, value);
public static string GetSystemName(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.SystemName;
public static void SetSystemName(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.SystemName = value;
public static string GetProjectId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.ProjectId;
public static void SetProjectId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.ProjectId = value;
public static string GetDeviceType(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.DeviceType;
public static void SetDeviceType(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.DeviceType = value;
public static string GetDeviceId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.DeviceId;
public static void SetDeviceId(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, string value) => obj.DeviceId = value;
public static long GetTimestamps(JiShe.CollectBus.IoTDB.Model.IoTEntity obj) => obj.Timestamps;
public static void SetTimestamps(JiShe.CollectBus.IoTDB.Model.IoTEntity obj, long value) => obj.Timestamps = value;
public List<string> PropertyList { get; } = new List<string>()
{
"SingleColumn.Item1","SingleColumn.Item2","SystemName","ProjectId","DeviceType","DeviceId","Timestamps" };
public object GetPropertyValue(JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> targetEntity, string propertyName)
{
return propertyName switch
{
"SingleColumn.Item1" => GetSingleColumn_Item1(targetEntity),
"SingleColumn.Item2" => GetSingleColumn_Item2(targetEntity),
"SystemName" => GetSystemName(targetEntity),
"ProjectId" => GetProjectId(targetEntity),
"DeviceType" => GetDeviceType(targetEntity),
"DeviceId" => GetDeviceId(targetEntity),
"Timestamps" => GetTimestamps(targetEntity),
_ => throw new ArgumentException($"Unknown property: {propertyName}")
};
}
public void SetPropertyValue(JiShe.CollectBus.IoTDB.Model.TableModelSingleMeasuringEntity<T> targetEntity, string propertyName, object value)
{
switch (propertyName)
{
case "SingleColumn.Item1":
SetSingleColumn_Item1(
targetEntity, (string)value);
break;
case "SingleColumn.Item2":
SetSingleColumn_Item2(
targetEntity, (T)value);
break;
case "SystemName":
SetSystemName(
targetEntity, (string)value);
break;
case "ProjectId":
SetProjectId(
targetEntity, (string)value);
break;
case "DeviceType":
SetDeviceType(
targetEntity, (string)value);
break;
case "DeviceId":
SetDeviceId(
targetEntity, (string)value);
break;
case "Timestamps":
SetTimestamps(
targetEntity, (long)value);
break;
default:
throw new ArgumentException($"Unknown property: {propertyName}");
}
}
}

View File

@ -566,6 +566,8 @@ namespace JiShe.CollectBus.IoTDB.Provider
//metadata.ColumnNames.Insert(0, "Timestamps");
//metadata.DataTypes.Insert(0, TSDataType.TIMESTAMP);
var accessor = SourceEntityAccessorFactory.GetAccessor<T>();
while (dataSet.HasNext() && results.Count < pageSize)
{
var record = dataSet.Next();
@ -588,11 +590,14 @@ namespace JiShe.CollectBus.IoTDB.Provider
if (measurement.ToLower().EndsWith("time"))
{
typeof(T).GetProperty(measurement)?.SetValue(entity, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds));
//typeof(T).GetProperty(measurement)?.SetValue(entity, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds));
accessor.SetPropertyValue(entity, measurement, TimestampHelper.ConvertToDateTime(tempValue, TimestampUnit.Nanoseconds));
}
else
{
typeof(T).GetProperty(measurement)?.SetValue(entity, tempValue);
accessor.SetPropertyValue(entity, measurement, tempValue);
//typeof(T).GetProperty(measurement)?.SetValue(entity, tempValue);
}
}