From 60bbcbbbf8518a99ee6cd3a65cd91b42e76148ca Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 25 Oct 2021 20:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E9=87=8D=E6=9E=84=E3=80=91validator?= =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/DataVisitors/ValidatorVisitor.cs | 408 ++++++++---------- src/Luban.Job.Cfg/Source/Defs/DefBean.cs | 36 -- src/Luban.Job.Cfg/Source/Defs/DefField.cs | 192 ++++----- .../TypeVisitors/DeepCompareTypeDefine.cs | 26 +- .../Source/Utils/DataLoaderUtil.cs | 7 - .../Source/Validators/IValidator.cs | 6 +- .../Source/Validators/PathValidator.cs | 5 +- .../Source/Validators/RangeValidator.cs | 5 +- .../Source/Validators/RefValidator.cs | 6 +- .../Source/Validators/ValidatorFactory.cs | 6 + src/Luban.Job.Cfg/Source/l10n/TextTable.cs | 1 - .../Source/Defs/DefAssemblyBase.cs | 6 +- .../Source/Defs/DefFieldBase.cs | 47 -- .../Source/Defs/IProcessor.cs | 12 + src/Luban.Job.Common/Source/Types/TBool.cs | 2 +- src/Luban.Job.Common/Source/Types/TType.cs | 3 + src/Luban.Job.Common/Source/Utils/DefUtil.cs | 27 +- src/Luban.Server/Luban.Server.csproj | 3 + 18 files changed, 316 insertions(+), 482 deletions(-) create mode 100644 src/Luban.Job.Common/Source/Defs/IProcessor.cs diff --git a/src/Luban.Job.Cfg/Source/DataVisitors/ValidatorVisitor.cs b/src/Luban.Job.Cfg/Source/DataVisitors/ValidatorVisitor.cs index 814fd70..e2a5d54 100644 --- a/src/Luban.Job.Cfg/Source/DataVisitors/ValidatorVisitor.cs +++ b/src/Luban.Job.Cfg/Source/DataVisitors/ValidatorVisitor.cs @@ -1,13 +1,15 @@ using Luban.Common.Utils; using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Defs; +using Luban.Job.Cfg.Validators; using Luban.Job.Common.Types; +using Luban.Job.Common.TypeVisitors; using System; using System.Collections.Generic; namespace Luban.Job.Cfg.DataVisitors { - public class ValidatorVisitor : IDataActionVisitor + public class ValidatorVisitor : ITypeActionVisitor { private readonly Stack _path = new Stack(); @@ -24,7 +26,6 @@ namespace Luban.Job.Cfg.DataVisitors public void ValidateTable(DefTable table, List records) { - DefAssembly ass = table.Assembly; var keyIndex = table.IndexFieldIdIndex; foreach (Record r in records) @@ -37,285 +38,212 @@ namespace Luban.Job.Cfg.DataVisitors { _path.Push(data.Fields[keyIndex]); } - Accept(data, ass); + if (table.ValueTType.Processors.Count > 0) + { + foreach (var p in table.ValueTType.Processors) + { + if (p is IValidator v) + { + v.Validate(Ctx, table.ValueTType, data); + } + } + } + table.ValueTType.Apply(this, data); } } - public void Accept(DBool type, DefAssembly x) + private void AcceptListLike(TType elementType, List eles) { - throw new NotImplementedException(); - } - - public void Accept(DByte type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DShort type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DFshort type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DInt type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DFint type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DLong type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DFlong type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DFloat type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DDouble type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DEnum type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DString type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DBytes type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DText type, DefAssembly x) - { - throw new NotImplementedException(); - } - - public void Accept(DBean record, DefAssembly assembly) - { - if (record == null) + if (elementType.Processors.Count > 0) { - return; + int index = 0; + foreach (var value in eles) + { + _path.Push(index++); + foreach (var v in elementType.Processors) + { + if (v is IValidator eleVal) + { + eleVal.Validate(Ctx, elementType, value); + if (value != null) + { + elementType.Apply(this, value); + } + } + } + _path.Pop(); + } } - var defFields = record.ImplType.HierarchyFields; + } + + public void Accept(TBool type, DType x) + { + + } + + public void Accept(TByte type, DType x) + { + + } + + public void Accept(TShort type, DType x) + { + + } + + public void Accept(TFshort type, DType x) + { + + } + + public void Accept(TInt type, DType x) + { + + } + + public void Accept(TFint type, DType x) + { + + } + + public void Accept(TLong type, DType x) + { + + } + + public void Accept(TFlong type, DType x) + { + + } + + public void Accept(TFloat type, DType x) + { + + } + + public void Accept(TDouble type, DType x) + { + + } + + public void Accept(TEnum type, DType x) + { + + } + + public void Accept(TString type, DType x) + { + + } + + public void Accept(TBytes type, DType x) + { + + } + + public void Accept(TText type, DType x) + { + + } + + public void Accept(TBean type, DType x) + { + var beanData = (DBean)x; + var defFields = ((DefBean)type.Bean.AssemblyBase.GetDefType(beanData.ImplType.FullName)).HierarchyFields;// beanData.ImplType.HierarchyFields; int i = 0; - foreach (var fieldValue in record.Fields) + foreach (var fieldValue in beanData.Fields) { var defField = (DefField)defFields[i++]; _path.Push(defField.Name); - switch (defField.CType) + + var fieldType = defField.CType; + + if (fieldType.Processors.Count > 0) { - case TArray a: + foreach (var p in fieldType.Processors) { - if (defField.ValueValidators.Count > 0) + if (p is IValidator val) { - var arr = (DArray)fieldValue; - int index = 0; - foreach (var value in arr.Datas) - { - _path.Push(index++); - foreach (var v in defField.ValueValidators) - { - v.Validate(Ctx, value, defField.IsNullable); - } - _path.Pop(); - } - + val.Validate(Ctx, fieldType, fieldValue); } - if (a.ElementType is TBean) - { - var arr = (DArray)fieldValue; - int index = 0; - foreach (var value in arr.Datas) - { - _path.Push(index++); - Accept((DBean)value, assembly); - _path.Pop(); - } - - } - break; - } - case TList b: - { - if (defField.ValueValidators.Count > 0) - { - var arr = (DList)fieldValue; - int index = 0; - foreach (var value in arr.Datas) - { - _path.Push(index++); - foreach (var v in defField.ValueValidators) - { - v.Validate(Ctx, value, false); - } - _path.Pop(); - } - - } - if (b.ElementType is TBean tb) - { - var arr = (DList)fieldValue; - int index = 0; - foreach (var value in arr.Datas) - { - _path.Push(index++); - Accept((DBean)value, assembly); - _path.Pop(); - } - - - if (defField.IndexField != null) - { - var indexSet = new HashSet(); - if (!tb.GetBeanAs().TryGetField(defField.Index, out var _, out var indexOfIndexField)) - { - throw new Exception("impossible"); - } - foreach (var value in arr.Datas) - { - _path.Push(index++); - DType indexValue = ((DBean)value).Fields[indexOfIndexField]; - if (!indexSet.Add(indexValue)) - { - throw new Exception($"'{TypeUtil.MakeFullName(_path)}' index:'{indexValue}' 重复"); - } - _path.Pop(); - } - } - } - break; - } - case TSet c: - { - if (defField.ValueValidators.Count > 0) - { - var arr = (DSet)fieldValue; - foreach (var value in arr.Datas) - { - foreach (var v in defField.ValueValidators) - { - v.Validate(Ctx, value, false); - } - } - - } - break; - } - - case TMap m: - { - DMap map = (DMap)fieldValue; - if (defField.KeyValidators.Count > 0) - { - foreach (var key in map.Datas.Keys) - { - _path.Push(key); - foreach (var v in defField.KeyValidators) - { - v.Validate(Ctx, key, false); - } - _path.Pop(); - } - } - if (defField.ValueValidators.Count > 0) - { - foreach (var value in map.Datas.Values) - { - _path.Push(value); - foreach (var v in defField.ValueValidators) - { - v.Validate(Ctx, value, false); - } - - if (value is DBean dv) - { - Accept(dv, assembly); - } - _path.Pop(); - } - } - break; - } - case TBean n: - { - Accept((DBean)fieldValue, assembly); - break; - } - default: - { - if (defField.Validators.Count > 0) - { - foreach (var v in defField.Validators) - { - v.Validate(Ctx, fieldValue, defField.IsNullable); - } - } - break; } } - _path.Pop(); + if (fieldValue != null) + { + fieldType.Apply(this, fieldValue); + } } } - public void Accept(DArray type, DefAssembly x) + public void Accept(TArray type, DType x) { - throw new NotImplementedException(); + AcceptListLike(type.ElementType, ((DArray)x).Datas); } - public void Accept(DList type, DefAssembly x) + public void Accept(TList type, DType x) { - throw new NotImplementedException(); + AcceptListLike(type.ElementType, ((DList)x).Datas); } - public void Accept(DSet type, DefAssembly x) + public void Accept(TSet type, DType x) { - throw new NotImplementedException(); + AcceptListLike(type.ElementType, ((DSet)x).Datas); } - public void Accept(DMap type, DefAssembly x) + public void Accept(TMap type, DType x) { - throw new NotImplementedException(); + var keyType = type.KeyType; + var valueType = type.ValueType; + if (keyType.Processors.Count > 0 || valueType.Processors.Count > 0) + { + foreach (var e in ((DMap)x).Datas) + { + _path.Push(e.Key); + if (keyType.Processors.Count > 0) + { + foreach (var v in keyType.Processors) + { + if (v is IValidator eleVal) + { + eleVal.Validate(Ctx, keyType, e.Key); + keyType.Apply(this, e.Key); + } + } + } + if (valueType.Processors.Count > 0) + { + foreach (var v in valueType.Processors) + { + if (v is IValidator eleVal) + { + eleVal.Validate(Ctx, valueType, e.Value); + valueType.Apply(this, e.Value); + } + } + } + _path.Pop(); + } + } } - public void Accept(DVector2 type, DefAssembly x) + public void Accept(TVector2 type, DType x) { - throw new NotImplementedException(); + } - public void Accept(DVector3 type, DefAssembly x) + public void Accept(TVector3 type, DType x) { - throw new NotImplementedException(); + } - public void Accept(DVector4 type, DefAssembly x) + public void Accept(TVector4 type, DType x) { - throw new NotImplementedException(); + } - public void Accept(DDateTime type, DefAssembly x) + public void Accept(TDateTime type, DType x) { - throw new NotImplementedException(); + } } } diff --git a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs index 5cdf2ca..cc128bd 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs @@ -70,49 +70,14 @@ namespace Luban.Job.Cfg.Defs { get { - //var imports = new HashSet(); - //if (ParentDefType != null) - //{ - // imports.Add(ParentDefType); - //} - //foreach (var f in Fields) - //{ - // f.CType.Apply(CollectEditorCppHeaderIncludeVisitor.Ins, imports); - //} - //imports.Remove(this); - //return string.Join('\n', imports.Select(im => $"#include \"{im.UeBpHeaderFileName}\"")); throw new NotImplementedException(); } } - //public string UeForwards - //{ - // get - // { - // var imports = new HashSet(); - // foreach (var f in Fields) - // { - // f.CType.Apply(CollectUeCppForwardDefineVisitor.Ins, imports); - // } - // return string.Join('\n', imports.Select(im => $"{im.CppNamespaceBegin} class {im.UeBpName}; {im.CppNamespaceEnd}")); - // } - //} - public string EditorCppIncludes { get { - //var imports = new HashSet(); - //if (ParentDefType != null) - //{ - // imports.Add(ParentDefType); - //} - //foreach (var f in Fields) - //{ - // f.CType.Apply(CollectEditorCppHeaderIncludeVisitor.Ins, imports); - //} - //imports.Remove(this); - //return string.Join('\n', imports.Select(im => $"#include \"{im.UeEditorHeaderFileName}\"")); throw new NotImplementedException(); } } @@ -126,7 +91,6 @@ namespace Luban.Job.Cfg.Defs { f.CType.Apply(CollectEditorCppForwardDefineVisitor.Ins, imports); } - //return string.Join('\n', imports.Select(im => $"{im.CppNamespaceBegin} struct {im.UeFname}; {im.CppNamespaceEnd}")); throw new NotImplementedException(); } } diff --git a/src/Luban.Job.Cfg/Source/Defs/DefField.cs b/src/Luban.Job.Cfg/Source/Defs/DefField.cs index 71c769c..272bcad 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefField.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefField.cs @@ -24,15 +24,15 @@ namespace Luban.Job.Cfg.Defs public RefValidator Ref { get; private set; } - public RefValidator KeyRef { get; private set; } + //public RefValidator KeyRef { get; private set; } - public RefValidator ValueRef { get; private set; } + //public RefValidator ValueRef { get; private set; } - public List Validators { get; } = new List(); + //public List Validators { get; } = new List(); - public List KeyValidators { get; } = new List(); + //public List KeyValidators { get; } = new List(); - public List ValueValidators { get; } = new List(); + //public List ValueValidators { get; } = new List(); // 如果ref了多个表,不再生成 xxx_ref之类的字段,也不会resolve public bool GenRef => Ref != null && Ref.Tables.Count == 1; @@ -126,102 +126,71 @@ namespace Luban.Job.Cfg.Defs this.RawDefine = f; } + + private void CompileValidatorsForType(TType type) + { + foreach (var valName in ValidatorFactory.ValidatorNames) + { + if (type.Tags != null && type.Tags.TryGetValue(valName, out var valValue)) + { + type.Processors.Add(ValidatorFactory.Create(valName, valValue)); + } + } + } + + private void CompileValidatorsForArrayLink(TType elementType) + { + CompileValidatorsForType(elementType); + + var valueRef = this.CType.Processors.Find(v => v is RefValidator); + if (valueRef != null) + { + this.CType.Processors.Remove(valueRef); + elementType.Processors.Add(valueRef); + } + var valuePath = this.CType.Processors.Find(v => v is PathValidator); + if (valuePath != null) + { + this.CType.Processors.Remove(valuePath); + elementType.Processors.Add(valuePath); + } + } + public override void Compile() { base.Compile(); + CompileValidatorsForType(CType); + switch (this.CType) { case TArray ta: { - if (ta.ElementType.Tags.TryGetValue("ref", out string refStr)) - { - this.ValueValidators.Add(this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr)); - } - if (CType.Tags.TryGetValue("ref", out string refStr2)) - { - this.ValueValidators.Add(this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr2)); - } - if (ta.ElementType.Tags.TryGetValue("path", out string PathStr)) - { - this.ValueValidators.Add(ValidatorFactory.Create("path", PathStr)); - } - if (CType.Tags.TryGetValue("path", out string pathStr2)) - { - this.ValueValidators.Add(ValidatorFactory.Create("path", pathStr2)); - } + CompileValidatorsForArrayLink(ta.ElementType); break; } case TList ta: { - if (ta.ElementType.Tags.TryGetValue("ref", out string refStr)) - { - this.ValueValidators.Add(this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr)); - } - if (CType.Tags.TryGetValue("ref", out string refStr2)) - { - this.ValueValidators.Add(this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr2)); - } - if (ta.ElementType.Tags.TryGetValue("path", out string PathStr)) - { - this.ValueValidators.Add(ValidatorFactory.Create("path", PathStr)); - } - if (CType.Tags.TryGetValue("path", out string pathStr2)) - { - this.ValueValidators.Add(ValidatorFactory.Create("path", pathStr2)); - } + CompileValidatorsForArrayLink(ta.ElementType); break; } case TSet ta: { - if (ta.ElementType.Tags.TryGetValue("ref", out string refStr)) - { - this.ValueValidators.Add(this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr)); - } - if (CType.Tags.TryGetValue("ref", out string refStr2)) - { - this.ValueValidators.Add(this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", refStr2)); - } - if (ta.ElementType.Tags.TryGetValue("path", out string PathStr)) - { - this.ValueValidators.Add(ValidatorFactory.Create("path", PathStr)); - } - if (CType.Tags.TryGetValue("path", out string pathStr2)) - { - this.ValueValidators.Add(ValidatorFactory.Create("path", pathStr2)); - } + CompileValidatorsForArrayLink(ta.ElementType); break; } case TMap ta: { - if (ta.KeyType.Tags.TryGetValue("ref", out string keyRefStr)) - { - this.KeyValidators.Add(this.KeyRef = (RefValidator)ValidatorFactory.Create("ref", keyRefStr)); - } - if (ta.ValueType.Tags.TryGetValue("ref", out string valueRefStr)) - { - this.ValueValidators.Add(this.ValueRef = (RefValidator)ValidatorFactory.Create("ref", valueRefStr)); - } - if (ta.KeyType.Tags.TryGetValue("path", out string PathStr)) - { - this.KeyValidators.Add(ValidatorFactory.Create("path", PathStr)); - } - if (ta.ValueType.Tags.TryGetValue("path", out string pathStr2)) - { - this.ValueValidators.Add(ValidatorFactory.Create("path", pathStr2)); - } + CompileValidatorsForType(ta.KeyType); + CompileValidatorsForType(ta.ValueType); break; } default: { - if (CType.Tags.TryGetValue("ref", out string refStr2)) + var selfRef = this.CType.Processors.Find(v => v is RefValidator); + if (selfRef != null) { - this.Validators.Add(this.Ref = (RefValidator)ValidatorFactory.Create("ref", refStr2)); - - } - if (CType.Tags.TryGetValue("path", out string pathStr2)) - { - this.Validators.Add(ValidatorFactory.Create("path", pathStr2)); + this.Ref = (RefValidator)selfRef; } break; } @@ -301,47 +270,50 @@ namespace Luban.Job.Cfg.Defs } } + private void CompileValidator(TType type) + { + foreach (var p in CType.Processors) + { + if (p is IValidator val) + { + val.Compile(this); + if (val is RefValidator refVal) + { + ValidateRef(refVal, type); + } + } + } + } + public override void PostCompile() { base.PostCompile(); - foreach (var val in KeyValidators.Concat(ValueValidators).Concat(Validators)) - { - val.Compile(this); - } + CompileValidator(CType); - if (Ref != null) + + switch (this.CType) { - ValidateRef(Ref, CType); - } - if (KeyRef != null) - { - ValidateRef(KeyRef, (CType as TMap).KeyType); - } - if (ValueRef != null) - { - switch (this.CType) + case TArray ta: { - case TArray ta: - { - ValidateRef(ValueRef, ta.ElementType); - break; - } - case TList ta: - { - ValidateRef(ValueRef, ta.ElementType); - break; - } - case TSet ta: - { - ValidateRef(ValueRef, ta.ElementType); - break; - } - case TMap ta: - { - ValidateRef(ValueRef, ta.ValueType); - break; - } + CompileValidator(ta.ElementType); + break; + } + case TList ta: + { + CompileValidator(ta.ElementType); + break; + } + case TSet ta: + { + CompileValidator(ta.ElementType); + break; + } + case TMap ta: + { + CompileValidator(ta.KeyType); + CompileValidator(ta.ValueType); + break; } } diff --git a/src/Luban.Job.Cfg/Source/TypeVisitors/DeepCompareTypeDefine.cs b/src/Luban.Job.Cfg/Source/TypeVisitors/DeepCompareTypeDefine.cs index 5801824..9e4145e 100644 --- a/src/Luban.Job.Cfg/Source/TypeVisitors/DeepCompareTypeDefine.cs +++ b/src/Luban.Job.Cfg/Source/TypeVisitors/DeepCompareTypeDefine.cs @@ -55,24 +55,6 @@ namespace Luban.Job.Cfg.TypeVisitors return false; } - //bool IsValidatorEquals(List vs1, List vs2) - //{ - // if (vs1.Count != vs2.Count) - // { - // return false; - // } - // for (int i = 0; i < vs1.Count; i++) - // { - // var v1 = vs1[i]; - // var v2 = vs2[i]; - // if (v1.Type != v2.Type || v1.Rule != v2.Rule) - // { - // return false; - // } - // } - // return true; - //} - if (ctx.TryGetValue(a, out var e)) { return e; @@ -103,15 +85,9 @@ namespace Luban.Job.Cfg.TypeVisitors if (f1.Name != f2.Name || f1.NeedExport != f2.NeedExport || f1.Index != f2.Index - // || f1.Sep != f2.Sep - //#if !LUBAN_LITE - // || f1.ResourceTag != f2.ResourceTag - //#endif || f1.CType.IsNullable != f2.CType.IsNullable || f1.CType.GetType() != f2.CType.GetType() - //|| !IsValidatorEquals(f1.RawDefine.Validators, f2.RawDefine.Validators) - //|| !IsValidatorEquals(f1.RawDefine.KeyValidators, f2.RawDefine.KeyValidators) - //|| !IsValidatorEquals(f1.RawDefine.ValueValidators, f2.RawDefine.ValueValidators) + //|| !IsProcessorEqual(f1.CType, f2.CType) ) { return setupNotEqual(); diff --git a/src/Luban.Job.Cfg/Source/Utils/DataLoaderUtil.cs b/src/Luban.Job.Cfg/Source/Utils/DataLoaderUtil.cs index e7be19b..6df56be 100644 --- a/src/Luban.Job.Cfg/Source/Utils/DataLoaderUtil.cs +++ b/src/Luban.Job.Cfg/Source/Utils/DataLoaderUtil.cs @@ -64,13 +64,6 @@ namespace Luban.Job.Cfg.Utils return allFiles; } - //private async Task> CollectInputFilesAsync(RemoteAgent agent, DefTable table, string dataDir) - //{ - // var collectTasks = new List>>(); - // foreach (var file in table.InputFiles) - // return CollectInputFilesAsync(agent, table.InputFiles, dataDir) - //} - public static async Task GenerateLoadRecordFromFileTasksAsync(IAgent agent, DefTable table, string dataDir, List inputFiles2, List>> tasks) { var inputFileInfos = await CollectInputFilesAsync(agent, inputFiles2, dataDir); diff --git a/src/Luban.Job.Cfg/Source/Validators/IValidator.cs b/src/Luban.Job.Cfg/Source/Validators/IValidator.cs index 021e667..460196c 100644 --- a/src/Luban.Job.Cfg/Source/Validators/IValidator.cs +++ b/src/Luban.Job.Cfg/Source/Validators/IValidator.cs @@ -1,12 +1,14 @@ using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Defs; +using Luban.Job.Common.Defs; +using Luban.Job.Common.Types; namespace Luban.Job.Cfg.Validators { - public interface IValidator + public interface IValidator : IProcessor { void Compile(DefField def); - void Validate(ValidatorContext ctx, DType data, bool nullable); + void Validate(ValidatorContext ctx, TType type, DType data); } } diff --git a/src/Luban.Job.Cfg/Source/Validators/PathValidator.cs b/src/Luban.Job.Cfg/Source/Validators/PathValidator.cs index 2a1f67c..b3cfebd 100644 --- a/src/Luban.Job.Cfg/Source/Validators/PathValidator.cs +++ b/src/Luban.Job.Cfg/Source/Validators/PathValidator.cs @@ -1,5 +1,6 @@ using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Defs; +using Luban.Job.Common.Types; using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -162,11 +163,11 @@ namespace Luban.Job.Cfg.Validators this.RawPattern = pathPattern; } - public void Validate(ValidatorContext ctx, DType data, bool nullable) + public void Validate(ValidatorContext ctx, TType type, DType data) { var assembly = ctx.Assembly; - if (nullable && data == null) + if (type.IsNullable && data == null) { return; } diff --git a/src/Luban.Job.Cfg/Source/Validators/RangeValidator.cs b/src/Luban.Job.Cfg/Source/Validators/RangeValidator.cs index 2888cf9..3732be5 100644 --- a/src/Luban.Job.Cfg/Source/Validators/RangeValidator.cs +++ b/src/Luban.Job.Cfg/Source/Validators/RangeValidator.cs @@ -1,5 +1,6 @@ using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Defs; +using Luban.Job.Common.Types; using System; namespace Luban.Job.Cfg.Validators @@ -143,7 +144,7 @@ namespace Luban.Job.Cfg.Validators public string Source => ValidatorContext.CurrentVisitor.CurrentValidateRecord.Source; - public void Validate(ValidatorContext ctx, DType data, bool nullable) + public void Validate(ValidatorContext ctx, TType type, DType data) { var assembly = ctx.Assembly; void LogError() @@ -151,7 +152,7 @@ namespace Luban.Job.Cfg.Validators assembly.Agent.Error("记录 {0}:{1} (来自文件:{2}) 不在范围:{3}内", ValidatorContext.CurrentRecordPath, data, Source, _str); } - if (nullable && data == null) + if (type.IsNullable && data == null) { return; } diff --git a/src/Luban.Job.Cfg/Source/Validators/RefValidator.cs b/src/Luban.Job.Cfg/Source/Validators/RefValidator.cs index b11b920..439f22d 100644 --- a/src/Luban.Job.Cfg/Source/Validators/RefValidator.cs +++ b/src/Luban.Job.Cfg/Source/Validators/RefValidator.cs @@ -1,8 +1,10 @@ using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.DataVisitors; using Luban.Job.Cfg.Defs; +using Luban.Job.Common.Types; using System; using System.Collections.Generic; +using System.Linq; namespace Luban.Job.Cfg.Validators { @@ -28,10 +30,10 @@ namespace Luban.Job.Cfg.Validators this.Tables = new List(tables); } - public void Validate(ValidatorContext ctx, DType key, bool nullable) + public void Validate(ValidatorContext ctx, TType type, DType key) { // 对于可空字段,跳过检查 - if (nullable && key == null) + if (type.IsNullable && key == null) { return; } diff --git a/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs b/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs index 3ae66ab..8a7437c 100644 --- a/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs +++ b/src/Luban.Job.Cfg/Source/Validators/ValidatorFactory.cs @@ -1,5 +1,6 @@ using Luban.Job.Cfg.RawDefs; using System; +using System.Collections.Generic; using System.Linq; namespace Luban.Job.Cfg.Validators @@ -7,6 +8,11 @@ namespace Luban.Job.Cfg.Validators static class ValidatorFactory { private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger(); + + private readonly static List s_validatorNames = new List() { RefValidator.NAME, PathValidator.NAME, RangeValidator.NAME }; + + public static List ValidatorNames => s_validatorNames; + public static IValidator Create(string type, string rule) { s_logger.Debug("== create validator {type}:{rule}", type, rule); diff --git a/src/Luban.Job.Cfg/Source/l10n/TextTable.cs b/src/Luban.Job.Cfg/Source/l10n/TextTable.cs index 75f7390..089cb96 100644 --- a/src/Luban.Job.Cfg/Source/l10n/TextTable.cs +++ b/src/Luban.Job.Cfg/Source/l10n/TextTable.cs @@ -38,7 +38,6 @@ namespace Luban.Job.Cfg.l10n Fields = new List { new CfgField() { Name = "key", Type = "string" }, - //new Common.RawDefs.Field() { Id = 1, Name = "origin_text", Type = "string" }, new CfgField() { Name = textValueFieldName, Type = "string" }, } }) diff --git a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs index efe9657..a816fd6 100644 --- a/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefAssemblyBase.cs @@ -129,11 +129,7 @@ namespace Luban.Job.Common.Defs public TType CreateType(string module, string type) { -#if LUBAN_LITE - int sepIndex = type.IndexOf(','); -#else - int sepIndex = type.IndexOf(',', System.StringComparison.Ordinal); -#endif + int sepIndex = DefUtil.IndexOfIncludeBrace(type, ','); if (sepIndex > 0) { var (containerAndElementType, tags) = DefUtil.ParseType(type); diff --git a/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs b/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs index 7dfe715..03195fd 100644 --- a/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs +++ b/src/Luban.Job.Common/Source/Defs/DefFieldBase.cs @@ -61,53 +61,6 @@ namespace Luban.Job.Common.Defs } } - //private string _lanStyleName; - //public string CsStyleName - //{ - // get - // { - // if (_lanStyleName == null) - // { - // _lanStyleName = TypeUtil.ToCsStyleName(Name); - // } - // return _lanStyleName; - // } - //} - - //public string JavaStyleName - //{ - // get - // { - // if (_lanStyleName == null) - // { - // _lanStyleName = TypeUtil.ToJavaStyleName(Name); - // } - // return _lanStyleName; - // } - //} - - //public string CppStyleName => JavaStyleName; - - //public string TsStyleName - //{ - // get - // { - // if (_lanStyleName == null) - // { - // _lanStyleName = TypeUtil.ToJavaStyleName(Name); - // } - // return _lanStyleName; - // } - //} - - //public string PyStyleName => Name; - - //public string GoStyleName => CsStyleName; - - //public string RustStyleName => Name != "type" ? Name : "r#" + Name; - - //public string GoStyleAssignName => CType.IsNullable ? "*" + CsStyleName : CsStyleName; - public string Type { get; } public TType CType { get; protected set; } diff --git a/src/Luban.Job.Common/Source/Defs/IProcessor.cs b/src/Luban.Job.Common/Source/Defs/IProcessor.cs new file mode 100644 index 0000000..822d6a9 --- /dev/null +++ b/src/Luban.Job.Common/Source/Defs/IProcessor.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Luban.Job.Common.Defs +{ + public interface IProcessor + { + } +} diff --git a/src/Luban.Job.Common/Source/Types/TBool.cs b/src/Luban.Job.Common/Source/Types/TBool.cs index d9957cc..f00e132 100644 --- a/src/Luban.Job.Common/Source/Types/TBool.cs +++ b/src/Luban.Job.Common/Source/Types/TBool.cs @@ -11,7 +11,7 @@ namespace Luban.Job.Common.Types public static TBool Create(bool isNullable, Dictionary tags) { - if (tags == null) + if (tags == null || tags.Count == 0) { return isNullable ? NullableIns : Ins; } diff --git a/src/Luban.Job.Common/Source/Types/TType.cs b/src/Luban.Job.Common/Source/Types/TType.cs index 6666bdd..370cb66 100644 --- a/src/Luban.Job.Common/Source/Types/TType.cs +++ b/src/Luban.Job.Common/Source/Types/TType.cs @@ -1,3 +1,4 @@ +using Luban.Job.Common.Defs; using Luban.Job.Common.TypeVisitors; using System.Collections.Generic; @@ -9,6 +10,8 @@ namespace Luban.Job.Common.Types public Dictionary Tags { get; } + public List Processors { get; } = new List(); + protected TType(bool isNullable, Dictionary tags) { IsNullable = isNullable; diff --git a/src/Luban.Job.Common/Source/Utils/DefUtil.cs b/src/Luban.Job.Common/Source/Utils/DefUtil.cs index affa8fe..abff816 100644 --- a/src/Luban.Job.Common/Source/Utils/DefUtil.cs +++ b/src/Luban.Job.Common/Source/Utils/DefUtil.cs @@ -35,6 +35,29 @@ namespace Luban.Job.Common.Utils return am; } + public static int IndexOfIncludeBrace(string s, char sep) + { + int braceDepth = 0; + for (int i = 0; i < s.Length; i++) + { + var c = s[i]; + if (c == '(' || c == '[' || c == '{') + { + ++braceDepth; + } + else if (c == ')' || c == ')' || c == '}') + { + --braceDepth; + } + + if (braceDepth == 0 && (c == sep)) + { + return i; + } + } + return -1; + } + public static (string, Dictionary) ParseType(string s) { int sepIndex = s.IndexOfAny(s_attrSep); @@ -48,11 +71,11 @@ namespace Luban.Job.Common.Utils for (int i = 0; i < s.Length; i++) { var c = s[i]; - if (c == '(') + if (c == '(' || c == '[' || c == '{') { ++braceDepth; } - else if (c == ')') + else if (c == ')' || c == ')' || c == '}') { --braceDepth; } diff --git a/src/Luban.Server/Luban.Server.csproj b/src/Luban.Server/Luban.Server.csproj index 474e170..0a3c7b9 100644 --- a/src/Luban.Server/Luban.Server.csproj +++ b/src/Luban.Server/Luban.Server.csproj @@ -17,8 +17,11 @@ + + +