mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 11:14:30 +08:00
fix(generator): 修复日志生成器中的诊断和分类逻辑
- 使用预定义的 Diagnostics.MustBePartial 替代手动创建 DiagnosticDescriptor - 优化构造函数参数检查逻辑,添加对 null 值和无效类型的处理 - 增加三种参数情况的处理:null 值、有效字符串、无效类型 - 添加错误情况下的回退机制,使用默认分类值 - 改进代码注释和错误处理的健壮性
This commit is contained in:
parent
d6b97fc0d1
commit
73c474c37a
@ -57,8 +57,7 @@ namespace GFramework.Generator.generator.logging
|
||||
{
|
||||
// 如果 Diagnostics 类有问题,这里可能会崩,先注释掉或确保该类存在
|
||||
spc.ReportDiagnostic(Diagnostic.Create(
|
||||
new DiagnosticDescriptor("GEN001", "Class must be partial", "Class '{0}' must be partial",
|
||||
"Usage", DiagnosticSeverity.Error, true),
|
||||
Diagnostics.MustBePartial,
|
||||
classDecl.Identifier.GetLocation(),
|
||||
classSymbol.Name));
|
||||
|
||||
@ -102,16 +101,34 @@ namespace GFramework.Generator.generator.logging
|
||||
var className = classSymbol.Name;
|
||||
|
||||
// === 解析 Category ===
|
||||
string category = className; // 默认值
|
||||
string category = className; // 默认使用类名
|
||||
|
||||
// 检查构造函数参数 (第一个参数)
|
||||
// 检查是否有构造函数参数
|
||||
if (attr.ConstructorArguments.Length > 0)
|
||||
{
|
||||
var argValue = attr.ConstructorArguments[0].Value;
|
||||
if (argValue is string s && !string.IsNullOrWhiteSpace(s))
|
||||
|
||||
// 情况 1: 参数存在,但值为 null (例如 [Log] 且构造函数有默认值 null)
|
||||
if (argValue is null)
|
||||
{
|
||||
category = className;
|
||||
}
|
||||
// 情况 2: 参数存在,且是有效的字符串 (例如 [Log("MyCategory")])
|
||||
else if (argValue is string s && !string.IsNullOrWhiteSpace(s))
|
||||
{
|
||||
category = s;
|
||||
}
|
||||
// 情况 3: 参数存在,但类型不对 (防御性编程)
|
||||
else
|
||||
{
|
||||
// 这里可以选择报错,或者回退到默认值。
|
||||
// 为了让用户知道写错了,保留报错逻辑是合理的。
|
||||
// 注意:这里需要你有 ReportDiagnostic 的逻辑,如果没有,为了不中断生成,建议回退到默认值。
|
||||
|
||||
// 如果你在 Generate 方法里无法轻松调用 ReportDiagnostic,
|
||||
// 建议直接忽略错误使用默认值,或者生成一段 #error 代码。
|
||||
category = $"{className}_InvalidArg";
|
||||
}
|
||||
}
|
||||
|
||||
// === 解析 Named Arguments (更加安全的获取方式) ===
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user