【重构】修复 ref 相关生成

main
walon 2021-10-15 18:14:49 +08:00
parent a7dcb7f64d
commit c530331f95
4 changed files with 157 additions and 127 deletions

View File

@ -24,6 +24,10 @@ namespace Luban.Job.Cfg.Defs
public RefValidator Ref { get; private set; } public RefValidator Ref { get; private set; }
public RefValidator KeyRef { get; private set; }
public RefValidator ValueRef { get; private set; }
// 如果ref了多个表不再生成 xxx_ref之类的字段也不会resolve // 如果ref了多个表不再生成 xxx_ref之类的字段也不会resolve
public bool GenRef => Ref != null && Ref.Tables.Count == 1; public bool GenRef => Ref != null && Ref.Tables.Count == 1;
@ -121,25 +125,66 @@ namespace Luban.Job.Cfg.Defs
public override void Compile() public override void Compile()
{ {
base.Compile(); base.Compile();
//foreach (var v in this.Validators)
//{
// v.Compile(this);
//}
//foreach (var v in this.KeyValidators) switch (this.CType)
//{ {
// v.Compile(this); case TArray ta:
//} {
if (ta.ElementType.Tags.TryGetValue("ref", out string refStr))
//foreach (var v in this.ValueValidators) {
//{ this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr);
// v.Compile(this); }
//} if (CType.Tags.TryGetValue("ref", out string refStr2))
{
//if (!string.IsNullOrWhiteSpace(this.DefaultValue)) this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr2);
//{ }
// this.DefalutDtypeValue = CType.Apply(StringDataCreator.Ins, this.DefaultValue); break;
//} }
case TList ta:
{
if (ta.ElementType.Tags.TryGetValue("ref", out string refStr))
{
this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr);
}
if (CType.Tags.TryGetValue("ref", out string refStr2))
{
this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr2);
}
break;
}
case TSet ta:
{
if (ta.ElementType.Tags.TryGetValue("ref", out string refStr))
{
this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr);
}
if (CType.Tags.TryGetValue("ref", out string refStr2))
{
this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr2);
}
break;
}
case TMap ta:
{
if (ta.KeyType.Tags.TryGetValue("ref", out string keyRefStr))
{
this.KeyRef = (RefValidator)ValidatorFactory.Create("ref", keyRefStr);
}
if (ta.ValueType.Tags.TryGetValue("ref", out string valueRefStr))
{
this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", valueRefStr);
}
break;
}
default:
{
if (CType.Tags.TryGetValue("ref", out string refStr2))
{
this.Ref = (RefValidator)ValidatorFactory.Create("ref", refStr2);
}
break;
}
}
switch (CType) switch (CType)
{ {
@ -210,87 +255,72 @@ namespace Luban.Job.Cfg.Defs
throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. only array:bean or list:bean support index"); throw new Exception($"type:'{HostType.FullName}' field:'{Name}' index:'{Index}'. only array:bean or list:bean support index");
} }
} }
}
//if (!CType.IsCollection && !(CType.IsBean)) private void ValidateRef(RefValidator val, TType refVarType)
//{ {
// this.Ref = (RefValidator)this.Validators.FirstOrDefault(v => v is RefValidator); val.Compile(this);
//} foreach (var table in val.Tables)
{
//if (!string.IsNullOrEmpty(this.RawDefine.Converter)) var cfgTable = Assembly.GetCfgTable(RefValidator.GetActualTableName(table));
//{ if (cfgTable == null)
// this.Remapper = AssemblyBase.GetDefTType(HostType.Namespace, this.RawDefine.Converter, this.IsNullable) as TEnum; {
// if (this.Remapper == null) throw new Exception($"type:'{HostType.FullName}' field:'{Name}' ref 引用的表:'{table}' 不存在");
// { }
// throw new Exception($"type:'{HostType.FullName}' field:'{Name}' converter:'{this.RawDefine.Converter}' not exists"); if (!cfgTable.NeedExport)
// } {
//} throw new Exception($"type:'{HostType.FullName}' field:'{Name}' ref 引用的表:'{table}' 没有导出");
}
//// 检查所引用的表是否导出了 if (!cfgTable.IsMapTable)
//if (NeedExport) {
//{ throw new Exception($"type:'{HostType.FullName}' field:'{Name}' ref 引用的表:'{table}'不是普通表,无法进行引用检查");
// var allValidators = new List<IValidator>(this.Validators); }
// allValidators.AddRange(this.KeyValidators); var keyType = cfgTable.KeyTType;
// allValidators.AddRange(this.ValueValidators); if (keyType.GetType() != refVarType.GetType())
{
// foreach (var val in allValidators) throw new Exception($"type:'{HostType.FullName}' field:'{Name}' 类型:'{refVarType}' 与 被引用的表:'{cfgTable.FullName}' key类型:'{keyType}' 不一致");
// { }
// if (val is RefValidator refValidator && !Assembly.GetCfgTable(refValidator.FirstTable).NeedExport) }
// {
// throw new Exception($"type:'{HostType.FullName}' field:'{Name}' ref 引用的表:'{refValidator.FirstTable}' 没有导出");
// }
// }
//}
} }
public override void PostCompile() public override void PostCompile()
{ {
base.PostCompile(); base.PostCompile();
// 检查 字段类型 与 所引用的表的key是否一致 if (Ref != null)
{
//foreach (var val in KeyValidators) ValidateRef(Ref, CType);
//{ }
// if (val is RefValidator refValidator) if (KeyRef != null)
// { {
// var cfgTable = Assembly.GetCfgTable(refValidator.FirstTable); ValidateRef(KeyRef, (CType as TMap).KeyType);
// if (CType is TMap mapType) }
// { if (ValueRef != null)
// if (mapType.KeyType.GetType() != cfgTable.KeyTType.GetType()) {
// { switch (this.CType)
// throw new Exception($"type:'{HostType.FullName}' field:'{Name}' key类型:'{mapType.KeyType.GetType()}' 与 被引用的表:'{cfgTable.FullName}' key类型:'{cfgTable.KeyTType.GetType()}' 不一致"); {
// } case TArray ta:
// } {
// else ValidateRef(ValueRef, ta.ElementType);
// { break;
// throw new Exception($"type:'{HostType.FullName}' field:'{Name}' 不是 map类型. 不能指定 key_validator 引用"); }
// } case TList ta:
// } {
//} ValidateRef(ValueRef, ta.ElementType);
break;
//var remainValidators = new List<IValidator>(this.Validators); }
//remainValidators.AddRange(this.ValueValidators); case TSet ta:
//foreach (var val in remainValidators) {
//{ ValidateRef(ValueRef, ta.ElementType);
// if (val is RefValidator refValidator) break;
// { }
// var cfgTable = Assembly.GetCfgTable(refValidator.FirstTable); case TMap ta:
// TType valueType; {
// switch (CType) ValidateRef(ValueRef, ta.ValueType);
// { break;
// case TArray ta: valueType = ta.ElementType; break; }
// case TList tl: valueType = tl.ElementType; break; }
// case TSet ts: valueType = ts.ElementType; break; }
// case TMap tm: valueType = tm.ValueType; break;
// default: valueType = CType; break;
// }
// if (valueType.GetType() != cfgTable.KeyTType.GetType())
// {
// throw new Exception($"type:'{HostType.FullName}' field:'{Name}' 类型:'{valueType.GetType()}' 与 被引用的表:'{cfgTable.FullName}' key类型:'{cfgTable.KeyTType.GetType()}' 不一致");
// }
// }
//}
} }
} }
} }

View File

@ -4,29 +4,29 @@ using System.Linq;
namespace Luban.Job.Cfg.Validators namespace Luban.Job.Cfg.Validators
{ {
//static class ValidatorFactory static class ValidatorFactory
//{ {
// private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger();
// public static IValidator Create(Validator validator) public static IValidator Create(string type, string rule)
// { {
// s_logger.Debug("== create validator {type}:{rule}", validator.Type, validator.Rule); s_logger.Debug("== create validator {type}:{rule}", type, rule);
// switch (validator.Type) switch (type)
// { {
// case RefValidator.NAME: case RefValidator.NAME:
// { {
// return new RefValidator(validator.Rule.Split(',').ToList()); return new RefValidator(rule.Split(',').ToList());
// } }
// case PathValidator.NAME: case PathValidator.NAME:
// { {
// return new PathValidator(validator.Rule);//.Split(',').ToList()); return new PathValidator(rule);//.Split(',').ToList());
// } }
// case RangeValidator.NAME: case RangeValidator.NAME:
// { {
// return new RangeValidator(validator.Rule); return new RangeValidator(rule);
// } }
// default: default:
// throw new NotSupportedException("unknown validator type:" + validator.Type); throw new NotSupportedException("unknown validator type:" + type);
// } }
// } }
//} }
} }

View File

@ -217,13 +217,13 @@ namespace Luban.Job.Common.Defs
{ {
switch (containerType) switch (containerType)
{ {
case "array": return TArray.Create(false, null, CreateNotContainerType(module, elementType)); case "array": return TArray.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType));
case "list": return TList.Create(false, null, CreateNotContainerType(module, elementType), true); case "list": return TList.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), true);
case "linkedlist": return TList.Create(false, null, CreateNotContainerType(module, elementType), false); case "linkedlist": return TList.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), false);
case "arraylist": return TList.Create(false, null, CreateNotContainerType(module, elementType), true); case "arraylist": return TList.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), true);
case "set": return TSet.Create(false, null, CreateNotContainerType(module, elementType), false); case "set": return TSet.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), false);
case "hashset": return TSet.Create(false, null, CreateNotContainerType(module, elementType), false); case "hashset": return TSet.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), false);
case "treeset": return TSet.Create(false, null, CreateNotContainerType(module, elementType), true); case "treeset": return TSet.Create(false, new Dictionary<string, string>(), CreateNotContainerType(module, elementType), true);
case "map": return CreateMapType(module, elementType, false); case "map": return CreateMapType(module, elementType, false);
case "treemap": return CreateMapType(module, elementType, true); case "treemap": return CreateMapType(module, elementType, true);
case "hashmap": return CreateMapType(module, elementType, false); case "hashmap": return CreateMapType(module, elementType, false);

View File

@ -11,11 +11,11 @@ namespace Luban.Job.Common.Utils
public static Dictionary<string, string> ParseAttrs(string tags) public static Dictionary<string, string> ParseAttrs(string tags)
{ {
var am = new Dictionary<string, string>();
if (string.IsNullOrWhiteSpace(tags)) if (string.IsNullOrWhiteSpace(tags))
{ {
return null; return am;
} }
var am = new Dictionary<string, string>();
foreach (var pair in tags.Split(s_attrSep)) foreach (var pair in tags.Split(s_attrSep))
{ {
int sepIndex = pair.IndexOfAny(s_attrKeyValueSep); int sepIndex = pair.IndexOfAny(s_attrKeyValueSep);
@ -40,7 +40,7 @@ namespace Luban.Job.Common.Utils
int sepIndex = s.IndexOfAny(s_attrSep); int sepIndex = s.IndexOfAny(s_attrSep);
if (sepIndex < 0) if (sepIndex < 0)
{ {
return (s, null); return (s, new Dictionary<string, string>());
} }
else else
{ {