【调整】cfg bean的field字段的index属性移到type中,xml及__bean__.xlsx中不再接受单独的index定义

main
walon 2021-10-24 11:37:54 +08:00
parent 81909562a0
commit 08720156e8
5 changed files with 43 additions and 62 deletions

View File

@ -85,13 +85,6 @@ namespace Luban.Job.Cfg.DataSources.Excel
public override Record ReadOne(TBean type) public override Record ReadOne(TBean type)
{ {
//var datas = ReadMulti(type);
//switch (datas.Count)
//{
// case 1: return datas[0];
// case 0: throw new Exception($"单例表不能为空必须包含且只包含1个记录");
// default: throw new Exception($"单例表必须恰好包含1个记录. 但当前记录数为:{datas.Count}");
//}
throw new Exception($"excel不支持单例读取模式"); throw new Exception($"excel不支持单例读取模式");
} }
} }

View File

@ -341,18 +341,11 @@ namespace Luban.Job.Cfg.Defs
switch (attrName) switch (attrName)
{ {
case "index": case "index":
{
cf.Index = attrValue;
break;
}
case "ref": case "ref":
case "path": case "path":
case "range": case "range":
{ {
cf.Type = cf.Type + "&" + attrs[i]; cf.Type = cf.Type + "&" + attrs[i];
//var validator = new Validator() { Type = attrName, Rule = attrValue };
//cf.Validators.Add(validator);
//cf.ValueValidators.Add(validator);
break; break;
} }
case "group": case "group":
@ -568,7 +561,6 @@ namespace Luban.Job.Cfg.Defs
{ {
new CfgField() { Name = "name", Type = "string" }, new CfgField() { Name = "name", Type = "string" },
new CfgField() { Name = "type", Type = "string" }, new CfgField() { Name = "type", Type = "string" },
new CfgField() { Name = "index", Type = "string" },
new CfgField() { Name = "group", Type = "string" }, new CfgField() { Name = "group", Type = "string" },
new CfgField() { Name = "comment", Type = "string" }, new CfgField() { Name = "comment", Type = "string" },
new CfgField() { Name = "tags", Type = "string" }, new CfgField() { Name = "tags", Type = "string" },
@ -647,7 +639,6 @@ namespace Luban.Job.Cfg.Defs
file.ActualFile, file.ActualFile,
(b.GetField("name") as DString).Value.Trim(), (b.GetField("name") as DString).Value.Trim(),
(b.GetField("type") as DString).Value.Trim(), (b.GetField("type") as DString).Value.Trim(),
(b.GetField("index") as DString).Value.Trim(),
(b.GetField("group") as DString).Value, (b.GetField("group") as DString).Value,
(b.GetField("comment") as DString).Value.Trim(), (b.GetField("comment") as DString).Value.Trim(),
(b.GetField("tags") as DString).Value.Trim(), (b.GetField("tags") as DString).Value.Trim(),
@ -667,7 +658,6 @@ namespace Luban.Job.Cfg.Defs
private static readonly List<string> _fieldOptionalAttrs = new() private static readonly List<string> _fieldOptionalAttrs = new()
{ {
"index",
"ref", "ref",
"path", "path",
"group", "group",
@ -695,7 +685,6 @@ namespace Luban.Job.Cfg.Defs
return CreateField(defineFile, XmlUtil.GetRequiredAttribute(e, "name"), return CreateField(defineFile, XmlUtil.GetRequiredAttribute(e, "name"),
typeStr, typeStr,
XmlUtil.GetOptionalAttribute(e, "index"),
XmlUtil.GetOptionalAttribute(e, "group"), XmlUtil.GetOptionalAttribute(e, "group"),
XmlUtil.GetOptionalAttribute(e, "comment"), XmlUtil.GetOptionalAttribute(e, "comment"),
XmlUtil.GetOptionalAttribute(e, "tags"), XmlUtil.GetOptionalAttribute(e, "tags"),
@ -703,14 +692,13 @@ namespace Luban.Job.Cfg.Defs
); );
} }
private Field CreateField(string defileFile, string name, string type, string index, string group, private Field CreateField(string defileFile, string name, string type, string group,
string comment, string tags, string comment, string tags,
bool ignoreNameValidation) bool ignoreNameValidation)
{ {
var f = new CfgField() var f = new CfgField()
{ {
Name = name, Name = name,
Index = index,
Groups = CreateGroups(group), Groups = CreateGroups(group),
Comment = comment, Comment = comment,
Tags = tags, Tags = tags,

View File

@ -16,7 +16,7 @@ namespace Luban.Job.Cfg.Defs
{ {
public DefAssembly Assembly => (DefAssembly)HostType.AssemblyBase; public DefAssembly Assembly => (DefAssembly)HostType.AssemblyBase;
public string Index { get; } public string Index { get; private set; }
public List<string> Groups { get; } public List<string> Groups { get; }
@ -125,7 +125,6 @@ namespace Luban.Job.Cfg.Defs
public DefField(DefTypeBase host, CfgField f, int idOffset) : base(host, f, idOffset) public DefField(DefTypeBase host, CfgField f, int idOffset) : base(host, f, idOffset)
{ {
Index = f.Index;
this.Groups = f.Groups; this.Groups = f.Groups;
this.RawDefine = f; this.RawDefine = f;
} }
@ -220,7 +219,7 @@ namespace Luban.Job.Cfg.Defs
{ {
if (CType.Tags.TryGetValue("ref", out string refStr2)) if (CType.Tags.TryGetValue("ref", out string refStr2))
{ {
this.Validators.Add( this.Ref = (RefValidator)ValidatorFactory.Create("ref", refStr2)); this.Validators.Add(this.Ref = (RefValidator)ValidatorFactory.Create("ref", refStr2));
} }
if (CType.Tags.TryGetValue("path", out string pathStr2)) if (CType.Tags.TryGetValue("path", out string pathStr2))
@ -278,28 +277,6 @@ namespace Luban.Job.Cfg.Defs
break; break;
} }
} }
if (!string.IsNullOrEmpty(Index))
{
if ((CType is TArray tarray) && (tarray.ElementType is TBean b))
{
if ((IndexField = b.GetBeanAs<DefBean>().GetField(Index)) == null)
{
throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. index not exist");
}
}
else if ((CType is TList tlist) && (tlist.ElementType is TBean tb))
{
if ((IndexField = tb.GetBeanAs<DefBean>().GetField(Index)) == null)
{
throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. index not exist");
}
}
else
{
throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. only array:bean or list:bean support index");
}
}
} }
private void ValidateRef(RefValidator val, TType refVarType) private void ValidateRef(RefValidator val, TType refVarType)
@ -370,6 +347,30 @@ namespace Luban.Job.Cfg.Defs
} }
} }
} }
Index = CType.GetTag("index");
if (!string.IsNullOrEmpty(Index))
{
if ((CType is TArray tarray) && (tarray.ElementType is TBean b))
{
if ((IndexField = b.GetBeanAs<DefBean>().GetField(Index)) == null)
{
throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. index not exist");
}
}
else if ((CType is TList tlist) && (tlist.ElementType is TBean tb))
{
if ((IndexField = tb.GetBeanAs<DefBean>().GetField(Index)) == null)
{
throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. index not exist");
}
}
else
{
throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. only array:bean or list:bean support index");
}
}
} }
} }
} }

View File

@ -5,8 +5,6 @@ namespace Luban.Job.Cfg.RawDefs
{ {
public class CfgField : Field public class CfgField : Field
{ {
public string Index { get; set; }
public List<string> Groups { get; set; } = new List<string>(); public List<string> Groups { get; set; } = new List<string>();
} }
} }

View File

@ -124,8 +124,9 @@ namespace Luban.Job.Common.Defs
#endif #endif
if (sepIndex > 0) if (sepIndex > 0)
{ {
string containerType = type.Substring(0, sepIndex).Trim(); var (containerAndElementType, tags) = DefUtil.ParseType(type);
return CreateContainerType(module, containerType, type.Substring(sepIndex + 1, type.Length - sepIndex - 1).Trim()); string containerType = containerAndElementType.Substring(0, sepIndex).Trim();
return CreateContainerType(module, containerType, tags, containerAndElementType.Substring(sepIndex + 1, containerAndElementType.Length - sepIndex - 1).Trim());
} }
else else
{ {
@ -203,30 +204,30 @@ namespace Luban.Job.Common.Defs
} }
} }
protected TMap CreateMapType(string module, string keyValueType, bool isTreeMap) protected TMap CreateMapType(string module, Dictionary<string, string> tags, string keyValueType, bool isTreeMap)
{ {
string[] elementTypes = keyValueType.Split(',').Select(s => s.Trim()).ToArray(); string[] elementTypes = keyValueType.Split(',').Select(s => s.Trim()).ToArray();
if (elementTypes.Length != 2) if (elementTypes.Length != 2)
{ {
throw new ArgumentException($"invalid map element type:'{keyValueType}'"); throw new ArgumentException($"invalid map element type:'{keyValueType}'");
} }
return TMap.Create(false, null, CreateNotContainerType(module, elementTypes[0]), CreateNotContainerType(module, elementTypes[1]), isTreeMap); return TMap.Create(false, tags, CreateNotContainerType(module, elementTypes[0]), CreateNotContainerType(module, elementTypes[1]), isTreeMap);
} }
protected TType CreateContainerType(string module, string containerType, string elementType) protected TType CreateContainerType(string module, string containerType, Dictionary<string, string> containerTags, string elementType)
{ {
switch (containerType) switch (containerType)
{ {
case "array": return TArray.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType)); case "array": return TArray.Create(false, containerTags, CreateNotContainerType(module, elementType));
case "list": return TList.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), true); case "list": return TList.Create(false, containerTags, CreateNotContainerType(module, elementType), true);
case "linkedlist": return TList.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), false); case "linkedlist": return TList.Create(false, containerTags, CreateNotContainerType(module, elementType), false);
case "arraylist": return TList.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), true); case "arraylist": return TList.Create(false, containerTags, CreateNotContainerType(module, elementType), true);
case "set": return TSet.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), false); case "set": return TSet.Create(false, containerTags, CreateNotContainerType(module, elementType), false);
case "hashset": return TSet.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), false); case "hashset": return TSet.Create(false, containerTags, CreateNotContainerType(module, elementType), false);
case "treeset": return TSet.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), true); case "treeset": return TSet.Create(false, containerTags, CreateNotContainerType(module, elementType), true);
case "map": return CreateMapType(module, elementType, false); case "map": return CreateMapType(module, containerTags, elementType, false);
case "treemap": return CreateMapType(module, elementType, true); case "treemap": return CreateMapType(module, containerTags, elementType, true);
case "hashmap": return CreateMapType(module, elementType, false); case "hashmap": return CreateMapType(module, containerTags, elementType, false);
default: default:
{ {
throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'"); throw new ArgumentException($"invalid container type. module:'{module}' container:'{containerType}' element:'{elementType}'");