【修复】修复加载定义出错时,打印错误日志无法定位错误的bug

main
walon 2021-09-01 11:28:28 +08:00
parent f0f39381c3
commit 75e73206f4
5 changed files with 67 additions and 76 deletions

View File

@ -67,7 +67,7 @@ namespace Luban.Job.Cfg.Defs
private static readonly List<string> _excelImportRequireAttrs = new List<string> { "name", "type" };
private void AddImportExcel(XElement e)
{
ValidAttrKeys(e, null, _excelImportRequireAttrs);
ValidAttrKeys(RootXml, e, null, _excelImportRequireAttrs);
var importName = XmlUtil.GetRequiredAttribute(e, "name");
if (string.IsNullOrWhiteSpace(importName))
{
@ -90,7 +90,7 @@ namespace Luban.Job.Cfg.Defs
private static readonly List<string> _branchRequireAttrs = new List<string> { "name" };
private void AddBranch(XElement e)
{
ValidAttrKeys(e, null, _branchRequireAttrs);
ValidAttrKeys(RootXml, e, null, _branchRequireAttrs);
var branchName = e.Attribute("name").Value;
if (string.IsNullOrWhiteSpace(branchName))
{
@ -108,7 +108,7 @@ namespace Luban.Job.Cfg.Defs
private void AddGroup(XElement e)
{
ValidAttrKeys(e, _groupOptionalAttrs, _groupRequireAttrs);
ValidAttrKeys(RootXml, e, _groupOptionalAttrs, _groupRequireAttrs);
List<string> groupNames = CreateGroups(e.Attribute("name").Value);
foreach (var g in groupNames)
@ -136,7 +136,7 @@ namespace Luban.Job.Cfg.Defs
}
}
private void FillValidators(string key, string attr, List<Validator> result)
private void FillValidators(string defineFile, string key, string attr, List<Validator> result)
{
if (!string.IsNullOrWhiteSpace(attr))
{
@ -145,7 +145,7 @@ namespace Luban.Job.Cfg.Defs
var sepIndex = validatorStr.IndexOf(':');
if (sepIndex < 0)
{
throw new Exception($"定义文件:{CurImportFile} 类型:'{CurDefine}' key:'{key}' attr:'{attr}' 不是合法的 validator 定义 (key1:value1#key2:value2 ...)");
throw new Exception($"定义文件:{defineFile} key:'{key}' attr:'{attr}' 不是合法的 validator 定义 (key1:value1#key2:value2 ...)");
}
result.Add(new Validator() { Type = validatorStr[..sepIndex], Rule = validatorStr[(sepIndex + 1)..] });
}
@ -162,7 +162,7 @@ namespace Luban.Job.Cfg.Defs
var refs = new List<string>();
s_logger.Trace("service name:{name} manager:{manager}", name, manager);
ValidAttrKeys(e, _serviceAttrs, _serviceAttrs);
ValidAttrKeys(RootXml, e, _serviceAttrs, _serviceAttrs);
foreach (XElement ele in e.Elements())
{
string tagName = ele.Name.LocalName;
@ -209,7 +209,7 @@ namespace Luban.Job.Cfg.Defs
return true;
}
private ETableMode ConvertMode(string tableName, string modeStr, string indexStr)
private ETableMode ConvertMode(string defineFile, string tableName, string modeStr, string indexStr)
{
ETableMode mode;
switch (modeStr)
@ -218,7 +218,7 @@ namespace Luban.Job.Cfg.Defs
{
if (!string.IsNullOrWhiteSpace(indexStr))
{
throw new Exception($"定义文件:{CurImportFile} table:'{tableName}' mode=one 是单例表不支持定义index属性");
throw new Exception($"定义文件:{defineFile} table:'{tableName}' mode=one 是单例表不支持定义index属性");
}
mode = ETableMode.ONE;
break;
@ -248,9 +248,9 @@ namespace Luban.Job.Cfg.Defs
private readonly List<string> _tableOptionalAttrs = new List<string> { "index", "mode", "group", "branch_input", "comment", "define_from_file" };
private readonly List<string> _tableRequireAttrs = new List<string> { "name", "value", "input" };
private void AddTable(XElement e)
private void AddTable(string defineFile, XElement e)
{
ValidAttrKeys(e, _tableOptionalAttrs, _tableRequireAttrs);
ValidAttrKeys(defineFile, e, _tableOptionalAttrs, _tableRequireAttrs);
string name = XmlUtil.GetRequiredAttribute(e, "name");
string module = CurNamespace;
string valueType = XmlUtil.GetRequiredAttribute(e, "value");
@ -262,7 +262,7 @@ namespace Luban.Job.Cfg.Defs
string branchInput = XmlUtil.GetOptionalAttribute(e, "branch_input");
string mode = XmlUtil.GetOptionalAttribute(e, "mode");
string tags = XmlUtil.GetOptionalAttribute(e, "tags");
AddTable(CurImportFile, name, module, valueType, index, mode, group, comment, defineFromFile, input, branchInput, tags);
AddTable(defineFile, name, module, valueType, index, mode, group, comment, defineFromFile, input, branchInput, tags);
}
private void AddTable(string defineFile, string name, string module, string valueType, string index, string mode, string group,
@ -277,7 +277,7 @@ namespace Luban.Job.Cfg.Defs
Index = index,
Groups = CreateGroups(group),
Comment = comment,
Mode = ConvertMode(name, mode, index),
Mode = ConvertMode(defineFile, name, mode, index),
Tags = tags,
};
@ -689,6 +689,7 @@ namespace Luban.Job.Cfg.Defs
Tags = tags,
Parent = "",
Fields = fields.Datas.Select(d => (DBean)d).Select(b => this.CreateField(
file.ActualFile,
(b.GetField("name") as DString).Value.Trim(),
(b.GetField("type") as DString).Value.Trim(),
(b.GetField("index") as DString).Value.Trim(),
@ -742,11 +743,11 @@ namespace Luban.Job.Cfg.Defs
private static readonly List<string> _fieldRequireAttrs = new List<string> { "name", "type" };
protected override Field CreateField(XElement e)
protected override Field CreateField(string defineFile, XElement e)
{
ValidAttrKeys(e, _fieldOptionalAttrs, _fieldRequireAttrs);
ValidAttrKeys(defineFile, e, _fieldOptionalAttrs, _fieldRequireAttrs);
return CreateField(XmlUtil.GetRequiredAttribute(e, "name"),
return CreateField(defineFile, XmlUtil.GetRequiredAttribute(e, "name"),
XmlUtil.GetRequiredAttribute(e, "type"),
XmlUtil.GetOptionalAttribute(e, "index"),
XmlUtil.GetOptionalAttribute(e, "sep"),
@ -767,7 +768,7 @@ namespace Luban.Job.Cfg.Defs
);
}
private Field CreateField(string name, string type, string index, string sep, bool isMultiRow, string group, string resource, string converter,
private Field CreateField(string defileFile, string name, string type, string index, string sep, bool isMultiRow, string group, string resource, string converter,
string comment, string refs, string path, string range, string keyValidator, string valueValidator, string validator, string tags,
bool ignoreNameValidation, bool isRowOrient)
{
@ -795,7 +796,7 @@ namespace Luban.Job.Cfg.Defs
}
else if (!ValidGroup(f.Groups, out var invalidGroup))
{
throw new Exception($"定义文件:{CurImportFile} field:'{name}' group:'{invalidGroup}' 不存在");
throw new Exception($"定义文件:{defileFile} field:'{name}' group:'{invalidGroup}' 不存在");
}
f.Type = type;
@ -804,18 +805,18 @@ namespace Luban.Job.Cfg.Defs
FillValueValidator(f, path, "path"); // (ue4|unity|normal|regex);xxx;xxx
FillValueValidator(f, range, "range");
FillValidators("key_validator", keyValidator, f.KeyValidators);
FillValidators("value_validator", valueValidator, f.ValueValidators);
FillValidators("validator", validator, f.Validators);
FillValidators(defileFile, "key_validator", keyValidator, f.KeyValidators);
FillValidators(defileFile, "value_validator", valueValidator, f.ValueValidators);
FillValidators(defileFile, "validator", validator, f.Validators);
return f;
}
private static readonly List<string> _beanOptinsAttrs = new List<string> { "value_type", "alias", "sep", "comment", "tags" };
private static readonly List<string> _beanRequireAttrs = new List<string> { "name" };
protected override void AddBean(XElement e, string parent)
protected override void AddBean(string defineFile, XElement e, string parent)
{
ValidAttrKeys(e, _beanOptinsAttrs, _beanRequireAttrs);
ValidAttrKeys(defineFile, e, _beanOptinsAttrs, _beanRequireAttrs);
var b = new CfgBean()
{
@ -841,9 +842,9 @@ namespace Luban.Job.Cfg.Defs
{
if (defineAnyChildBean)
{
throw new LoadDefException($"定义文件:{CurImportFile} 类型:{b.FullName} 的多态子bean必须在所有成员字段 <var> 之前定义");
throw new LoadDefException($"定义文件:{defineFile} 类型:{b.FullName} 的多态子bean必须在所有成员字段 <var> 之前定义");
}
b.Fields.Add(CreateField(fe)); ;
b.Fields.Add(CreateField(defineFile, fe)); ;
break;
}
case "bean":
@ -854,7 +855,7 @@ namespace Luban.Job.Cfg.Defs
}
default:
{
throw new LoadDefException($"定义文件:{CurImportFile} 类型:{b.FullName} 不支持 tag:{fe.Name}");
throw new LoadDefException($"定义文件:{defineFile} 类型:{b.FullName} 不支持 tag:{fe.Name}");
}
}
}
@ -864,7 +865,7 @@ namespace Luban.Job.Cfg.Defs
var fullname = b.FullName;
foreach (var cb in childBeans)
{
AddBean(cb, fullname);
AddBean(defineFile, cb, fullname);
}
}
}

View File

@ -11,6 +11,7 @@ namespace Luban.Job.Cfg.Utils
{
class DTypeTemplateExtends : TTypeTemplateExtends
{
public static bool IsSimpleLiteralData(DType type)
{
return type.Apply(IsSimpleLiteralDataVisitor.Ins);

View File

@ -41,11 +41,9 @@ namespace Luban.Job.Common.Defs
public bool IsBeanFieldMustDefineId { get; protected set; }
private readonly Dictionary<string, Action<XElement>> _rootDefineHandlers = new Dictionary<string, Action<XElement>>();
private readonly Dictionary<string, Action<XElement>> _moduleDefineHandlers = new Dictionary<string, Action<XElement>>();
private readonly Dictionary<string, Action<string, XElement>> _moduleDefineHandlers = new();
protected readonly Stack<string> _namespaceStack = new Stack<string>();
protected readonly Stack<string> _importFileStack = new Stack<string>();
protected readonly Stack<XElement> _defineStack = new Stack<XElement>();
protected string TopModule { get; private set; }
@ -65,6 +63,7 @@ namespace Luban.Job.Common.Defs
_moduleDefineHandlers.Add("bean", AddBean);
}
public string RootXml => _rootXml;
private string _rootXml;
public async Task LoadAsync(string rootXml)
@ -99,16 +98,12 @@ namespace Luban.Job.Common.Defs
_rootDefineHandlers.Add(name, handler);
}
protected void RegisterModuleDefineHandler(string name, Action<XElement> handler)
protected void RegisterModuleDefineHandler(string name, Action<string, XElement> handler)
{
_moduleDefineHandlers.Add(name, handler);
}
protected string CurNamespace => _namespaceStack.Peek();
protected string CurImportFile => _importFileStack.Peek();
protected XElement CurDefine => _defineStack.Peek();
protected string CurNamespace => _namespaceStack.Count > 0 ? _namespaceStack.Peek() : "";
#region root handler
@ -129,9 +124,7 @@ namespace Luban.Job.Common.Defs
if (fileOrDirContent.IsFile)
{
s_logger.Trace("== file:{file}", xmlFullPath);
_importFileStack.Push(xmlFullPath);
AddModule(XmlUtil.Open(xmlFullPath, await Agent.GetFromCacheOrReadAllBytesAsync(xmlFullPath, fileOrDirContent.Md5)));
_importFileStack.Pop();
AddModule(xmlFullPath, XmlUtil.Open(xmlFullPath, await Agent.GetFromCacheOrReadAllBytesAsync(xmlFullPath, fileOrDirContent.Md5)));
}
else
{
@ -147,9 +140,7 @@ namespace Luban.Job.Common.Defs
continue;
}
string subFullPath = subFileName;
_importFileStack.Push(subFullPath);
AddModule(XmlUtil.Open(subFullPath, await Agent.GetFromCacheOrReadAllBytesAsync(subFullPath, subFile.MD5)));
_importFileStack.Pop();
AddModule(subFullPath, XmlUtil.Open(subFullPath, await Agent.GetFromCacheOrReadAllBytesAsync(subFullPath, subFile.MD5)));
}
}
}
@ -159,7 +150,7 @@ namespace Luban.Job.Common.Defs
#region module handler
private void AddModule(XElement me)
private void AddModule(string defineFile, XElement me)
{
var name = XmlUtil.GetOptionalAttribute(me, "name");
//if (string.IsNullOrEmpty(name))
@ -177,18 +168,16 @@ namespace Luban.Job.Common.Defs
{
if (tagName != "module")
{
_defineStack.Push(e);
handler(e);
_defineStack.Pop();
handler(defineFile, e);
}
else
{
handler(e);
handler(defineFile, e);
}
}
else
{
throw new LoadDefException($"定义文件:{CurImportFile} module:{CurNamespace} 不支持 tag:{tagName}");
throw new LoadDefException($"定义文件:{defineFile} module:{CurNamespace} 不支持 tag:{tagName}");
}
}
_namespaceStack.Pop();
@ -197,9 +186,9 @@ namespace Luban.Job.Common.Defs
private static readonly List<string> _fieldRequireAttrs = new List<string> { "name", "type", };
private static readonly List<string> _fieldOptionalAttrs = new List<string> { "id", "comment", "tags" };
protected virtual Field CreateField(XElement e)
protected virtual Field CreateField(string defineFile, XElement e)
{
ValidAttrKeys(e, _fieldOptionalAttrs, _fieldRequireAttrs);
ValidAttrKeys(defineFile, e, _fieldOptionalAttrs, _fieldRequireAttrs);
var f = new Field()
{
Id = XmlUtil.GetOptionIntAttribute(e, "id"),
@ -211,9 +200,9 @@ namespace Luban.Job.Common.Defs
return f;
}
protected void AddBean(XElement e)
protected void AddBean(string defineFile, XElement e)
{
AddBean(e, "");
AddBean(defineFile, e, "");
}
private static readonly List<string> _beanOptinsAttrs1 = new List<string> { "compatible", "value_type", "comment", "tags" };
@ -222,15 +211,15 @@ namespace Luban.Job.Common.Defs
private static readonly List<string> _beanOptinsAttrs2 = new List<string> { "id", "compatible", "value_type", "comment", "tags" };
private static readonly List<string> _beanRequireAttrs2 = new List<string> { "name" };
protected virtual void AddBean(XElement e, string parent)
protected virtual void AddBean(string defineFile, XElement e, string parent)
{
if (IsBeanFieldMustDefineId)
{
ValidAttrKeys(e, _beanOptinsAttrs1, _beanRequireAttrs1);
ValidAttrKeys(defineFile, e, _beanOptinsAttrs1, _beanRequireAttrs1);
}
else
{
ValidAttrKeys(e, _beanOptinsAttrs2, _beanRequireAttrs2);
ValidAttrKeys(defineFile, e, _beanOptinsAttrs2, _beanRequireAttrs2);
}
var b = new Bean()
{
@ -254,9 +243,9 @@ namespace Luban.Job.Common.Defs
{
if (defineAnyChildBean)
{
throw new LoadDefException($"定义文件:{CurImportFile} 类型:{b.FullName} 的多态子bean必须在所有成员字段 <var> 之前定义");
throw new LoadDefException($"定义文件:{defineFile} 类型:{b.FullName} 的多态子bean必须在所有成员字段 <var> 之前定义");
}
b.Fields.Add(CreateField(fe)); ;
b.Fields.Add(CreateField(defineFile, fe)); ;
break;
}
case "bean":
@ -267,7 +256,7 @@ namespace Luban.Job.Common.Defs
}
default:
{
throw new LoadDefException($"定义文件:{CurImportFile} 类型:{b.FullName} 不支持 tag:{fe.Name}");
throw new LoadDefException($"定义文件:{defineFile} 类型:{b.FullName} 不支持 tag:{fe.Name}");
}
}
}
@ -277,7 +266,7 @@ namespace Luban.Job.Common.Defs
var fullname = b.FullName;
foreach (var cb in childBeans)
{
AddBean(cb, fullname);
AddBean(defineFile, cb, fullname);
}
}
@ -286,21 +275,21 @@ namespace Luban.Job.Common.Defs
return XmlUtil.GetRequiredAttribute(e, key);
}
protected void ValidAttrKeys(XElement e, List<string> optionKeys, List<string> requireKeys)
protected void ValidAttrKeys(string defineFile, XElement e, List<string> optionKeys, List<string> requireKeys)
{
foreach (var k in e.Attributes())
{
var name = k.Name.LocalName;
if (!requireKeys.Contains(name) && (optionKeys != null && !optionKeys.Contains(name)))
{
throw new LoadDefException($"定义文件:{CurImportFile} module:{CurNamespace} 定义:{e} 包含未知属性 attr:{name}");
throw new LoadDefException($"定义文件:{defineFile} module:{CurNamespace} 定义:{e} 包含未知属性 attr:{name}");
}
}
foreach (var k in requireKeys)
{
if (e.Attribute(k) == null)
{
throw new LoadDefException($"定义文件:{CurImportFile} module:{CurNamespace} 定义:{e} 缺失属性 attr:{k}");
throw new LoadDefException($"定义文件:{defineFile} module:{CurNamespace} 定义:{e} 缺失属性 attr:{k}");
}
}
}
@ -312,9 +301,9 @@ namespace Luban.Job.Common.Defs
private static readonly List<string> _constItemRequiredAttrs = new List<string> { "name", "type" };
private static readonly List<string> _constItemOptionalAttrs = new List<string> { "value", "comment" };
protected void AddConst(XElement e)
protected void AddConst(string defineFile, XElement e)
{
ValidAttrKeys(e, _constOptionalAttrs, _constRequiredAttrs);
ValidAttrKeys(defineFile, e, _constOptionalAttrs, _constRequiredAttrs);
var c = new Const()
{
Name = XmlUtil.GetRequiredAttribute(e, "name"),
@ -323,7 +312,7 @@ namespace Luban.Job.Common.Defs
};
foreach (XElement item in e.Elements())
{
ValidAttrKeys(item, _constItemOptionalAttrs, _constItemRequiredAttrs);
ValidAttrKeys(defineFile, item, _constItemOptionalAttrs, _constItemRequiredAttrs);
c.Items.Add(new ConstItem()
{
Name = XmlUtil.GetRequiredAttribute(item, "name"),
@ -343,9 +332,9 @@ namespace Luban.Job.Common.Defs
private static readonly List<string> _enumItemOptionalAttrs = new List<string> { "value", "alias", "comment", "tags" };
private static readonly List<string> _enumItemRequiredAttrs = new List<string> { "name" };
protected void AddEnum(XElement e)
protected void AddEnum(string defineFile, XElement e)
{
ValidAttrKeys(e, _enumOptionalAttrs, _enumRequiredAttrs);
ValidAttrKeys(defineFile, e, _enumOptionalAttrs, _enumRequiredAttrs);
var en = new PEnum()
{
Name = XmlUtil.GetRequiredAttribute(e, "name"),
@ -357,7 +346,7 @@ namespace Luban.Job.Common.Defs
foreach (XElement item in e.Elements())
{
ValidAttrKeys(item, _enumItemOptionalAttrs, _enumItemRequiredAttrs);
ValidAttrKeys(defineFile, item, _enumItemOptionalAttrs, _enumItemRequiredAttrs);
en.Items.Add(new EnumItem()
{
Name = XmlUtil.GetRequiredAttribute(item, "name"),

View File

@ -36,9 +36,9 @@ namespace Luban.Job.Db.Defs
private readonly List<string> _tableOptionalAttrs = new List<string> { "memory", "comment" };
private readonly List<string> _tableRequireAttrs = new List<string> { "name", "id", "key", "value" };
private void AddTable(XElement e)
private void AddTable(string defineFile, XElement e)
{
ValidAttrKeys(e, _tableOptionalAttrs, _tableRequireAttrs);
ValidAttrKeys(defineFile, e, _tableOptionalAttrs, _tableRequireAttrs);
var p = new Table()
{
Id = XmlUtil.GetRequiredIntAttribute(e, "id"),

View File

@ -38,9 +38,9 @@ namespace Luban.Job.Proto.Defs
private readonly List<string> rpcAttrs = new List<string> { "id" };
private readonly List<string> rpcRequiredAttrs = new List<string> { "name", "arg", "res" };
private void AddRpc(XElement e)
private void AddRpc(string defineFile, XElement e)
{
ValidAttrKeys(e, rpcAttrs, rpcRequiredAttrs);
ValidAttrKeys(defineFile, e, rpcAttrs, rpcRequiredAttrs);
var r = new PRpc()
{
Name = XmlUtil.GetRequiredAttribute(e, "name"),
@ -57,9 +57,9 @@ namespace Luban.Job.Proto.Defs
private readonly List<string> protoOptionalAttrs = new List<string> { "id", "comment" };
private readonly List<string> protoRequiredAttrs = new List<string> { "name" };
private void AddProto(XElement e)
private void AddProto(string defineFile, XElement e)
{
ValidAttrKeys(e, protoOptionalAttrs, protoRequiredAttrs);
ValidAttrKeys(defineFile, e, protoOptionalAttrs, protoRequiredAttrs);
var p = new PProto()
{
@ -75,12 +75,12 @@ namespace Luban.Job.Proto.Defs
{
case "var":
{
p.Fields.Add(CreateField(fe)); ;
p.Fields.Add(CreateField(defineFile, fe)); ;
break;
}
default:
{
throw new Exception($"定义文件:{CurImportFile} 不支持 tag:{fe.Name}");
throw new Exception($"定义文件:{defineFile} 不支持 tag:{fe.Name}");
}
}
@ -95,7 +95,7 @@ namespace Luban.Job.Proto.Defs
{
var name = XmlUtil.GetRequiredAttribute(e, "name");
s_logger.Trace("service {service}", name);
ValidAttrKeys(e, serviceAttrs, serviceAttrs);
ValidAttrKeys(RootXml, e, serviceAttrs, serviceAttrs);
foreach (XElement ele in e.Elements())
{
s_logger.Trace("service {service_name} node: {name} {value}", name, ele.Name, ele.Attribute("value")?.Value);