【重构】完成ref检查重构

main
walon 2021-10-15 18:22:50 +08:00
parent c530331f95
commit 2b03140c68
2 changed files with 160 additions and 151 deletions

View File

@ -117,165 +117,165 @@ namespace Luban.Job.Cfg.DataVisitors
{ {
return; return;
} }
//var defFields = record.ImplType.HierarchyFields; var defFields = record.ImplType.HierarchyFields;
//int i = 0; int i = 0;
//foreach (var fieldValue in record.Fields) foreach (var fieldValue in record.Fields)
//{ {
// var defField = (DefField)defFields[i++]; var defField = (DefField)defFields[i++];
// _path.Push(defField.Name); _path.Push(defField.Name);
// switch (defField.CType) switch (defField.CType)
// { {
// case TArray a: case TArray a:
// { {
// if (defField.ValueValidators.Count > 0) if (defField.ValueValidators.Count > 0)
// { {
// var arr = (DArray)fieldValue; var arr = (DArray)fieldValue;
// int index = 0; int index = 0;
// foreach (var value in arr.Datas) foreach (var value in arr.Datas)
// { {
// _path.Push(index++); _path.Push(index++);
// foreach (var v in defField.ValueValidators) foreach (var v in defField.ValueValidators)
// { {
// v.Validate(Ctx, value, defField.IsNullable); v.Validate(Ctx, value, defField.IsNullable);
// } }
// _path.Pop(); _path.Pop();
// } }
// } }
// if (a.ElementType is TBean) if (a.ElementType is TBean)
// { {
// var arr = (DArray)fieldValue; var arr = (DArray)fieldValue;
// int index = 0; int index = 0;
// foreach (var value in arr.Datas) foreach (var value in arr.Datas)
// { {
// _path.Push(index++); _path.Push(index++);
// Accept((DBean)value, assembly); Accept((DBean)value, assembly);
// _path.Pop(); _path.Pop();
// } }
// } }
// break; break;
// } }
// case TList b: case TList b:
// { {
// if (defField.ValueValidators.Count > 0) if (defField.ValueValidators.Count > 0)
// { {
// var arr = (DList)fieldValue; var arr = (DList)fieldValue;
// int index = 0; int index = 0;
// foreach (var value in arr.Datas) foreach (var value in arr.Datas)
// { {
// _path.Push(index++); _path.Push(index++);
// foreach (var v in defField.ValueValidators) foreach (var v in defField.ValueValidators)
// { {
// v.Validate(Ctx, value, false); v.Validate(Ctx, value, false);
// } }
// _path.Pop(); _path.Pop();
// } }
// } }
// if (b.ElementType is TBean tb) if (b.ElementType is TBean tb)
// { {
// var arr = (DList)fieldValue; var arr = (DList)fieldValue;
// int index = 0; int index = 0;
// foreach (var value in arr.Datas) foreach (var value in arr.Datas)
// { {
// _path.Push(index++); _path.Push(index++);
// Accept((DBean)value, assembly); Accept((DBean)value, assembly);
// _path.Pop(); _path.Pop();
// } }
// if (defField.IndexField != null) if (defField.IndexField != null)
// { {
// var indexSet = new HashSet<DType>(); var indexSet = new HashSet<DType>();
// if (!tb.GetBeanAs<DefBean>().TryGetField(defField.Index, out var _, out var indexOfIndexField)) if (!tb.GetBeanAs<DefBean>().TryGetField(defField.Index, out var _, out var indexOfIndexField))
// { {
// throw new Exception("impossible"); throw new Exception("impossible");
// } }
// foreach (var value in arr.Datas) foreach (var value in arr.Datas)
// { {
// _path.Push(index++); _path.Push(index++);
// DType indexValue = ((DBean)value).Fields[indexOfIndexField]; DType indexValue = ((DBean)value).Fields[indexOfIndexField];
// if (!indexSet.Add(indexValue)) if (!indexSet.Add(indexValue))
// { {
// throw new Exception($"'{TypeUtil.MakeFullName(_path)}' index:'{indexValue}' 重复"); throw new Exception($"'{TypeUtil.MakeFullName(_path)}' index:'{indexValue}' 重复");
// } }
// _path.Pop(); _path.Pop();
// } }
// } }
// } }
// break; break;
// } }
// case TSet c: case TSet c:
// { {
// if (defField.ValueValidators.Count > 0) if (defField.ValueValidators.Count > 0)
// { {
// var arr = (DSet)fieldValue; var arr = (DSet)fieldValue;
// foreach (var value in arr.Datas) foreach (var value in arr.Datas)
// { {
// foreach (var v in defField.ValueValidators) foreach (var v in defField.ValueValidators)
// { {
// v.Validate(Ctx, value, false); v.Validate(Ctx, value, false);
// } }
// } }
// } }
// break; break;
// } }
// case TMap m: case TMap m:
// { {
// DMap map = (DMap)fieldValue; DMap map = (DMap)fieldValue;
// if (defField.KeyValidators.Count > 0) if (defField.KeyValidators.Count > 0)
// { {
// foreach (var key in map.Datas.Keys) foreach (var key in map.Datas.Keys)
// { {
// _path.Push(key); _path.Push(key);
// foreach (var v in defField.KeyValidators) foreach (var v in defField.KeyValidators)
// { {
// v.Validate(Ctx, key, false); v.Validate(Ctx, key, false);
// } }
// _path.Pop(); _path.Pop();
// } }
// } }
// if (defField.ValueValidators.Count > 0) if (defField.ValueValidators.Count > 0)
// { {
// foreach (var value in map.Datas.Values) foreach (var value in map.Datas.Values)
// { {
// _path.Push(value); _path.Push(value);
// foreach (var v in defField.ValueValidators) foreach (var v in defField.ValueValidators)
// { {
// v.Validate(Ctx, value, false); v.Validate(Ctx, value, false);
// } }
// if (value is DBean dv) if (value is DBean dv)
// { {
// Accept(dv, assembly); Accept(dv, assembly);
// } }
// _path.Pop(); _path.Pop();
// } }
// } }
// break; break;
// } }
// case TBean n: case TBean n:
// { {
// Accept((DBean)fieldValue, assembly); Accept((DBean)fieldValue, assembly);
// break; break;
// } }
// default: default:
// { {
// if (defField.Validators.Count > 0) if (defField.Validators.Count > 0)
// { {
// foreach (var v in defField.Validators) foreach (var v in defField.Validators)
// { {
// v.Validate(Ctx, fieldValue, defField.IsNullable); v.Validate(Ctx, fieldValue, defField.IsNullable);
// } }
// } }
// break; break;
// } }
// } }
// _path.Pop(); _path.Pop();
//} }
} }
public void Accept(DArray type, DefAssembly x) public void Accept(DArray type, DefAssembly x)

View File

@ -28,6 +28,12 @@ namespace Luban.Job.Cfg.Defs
public RefValidator ValueRef { get; private set; } public RefValidator ValueRef { get; private set; }
public List<IValidator> Validators { get; } = new List<IValidator>();
public List<IValidator> KeyValidators { get; } = new List<IValidator>();
public List<IValidator> ValueValidators { get; } = new List<IValidator>();
// 如果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;
@ -289,14 +295,17 @@ namespace Luban.Job.Cfg.Defs
if (Ref != null) if (Ref != null)
{ {
Validators.Add(Ref);
ValidateRef(Ref, CType); ValidateRef(Ref, CType);
} }
if (KeyRef != null) if (KeyRef != null)
{ {
KeyValidators.Add(KeyRef);
ValidateRef(KeyRef, (CType as TMap).KeyType); ValidateRef(KeyRef, (CType as TMap).KeyType);
} }
if (ValueRef != null) if (ValueRef != null)
{ {
ValueValidators.Add(ValueRef);
switch (this.CType) switch (this.CType)
{ {
case TArray ta: case TArray ta: