From bcc7bc8b3735156a2a42805b130645d18afbcb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carson=20-=20=E5=AE=9D=E9=B1=BC?= <44496710+kteong1012@users.noreply.github.com> Date: Thu, 20 Oct 2022 09:04:42 +0800 Subject: [PATCH] =?UTF-8?q?[new]=E6=94=AF=E6=8C=81$type=20+=20=E6=B5=81?= =?UTF-8?q?=E5=BC=8F$value=E7=9A=84=E5=A1=AB=E5=86=99=E6=96=B9=E5=BC=8F=20?= =?UTF-8?q?(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [new]支持$type + 流式$value的填写方式 Co-authored-by: Tianbao Lin --- .../Source/DataCreators/SheetDataCreator.cs | 57 ++++++++++++++++++- .../Source/DataSources/Excel/TitleRow.cs | 1 - src/Luban.Job.Cfg/Source/Defs/DefBean.cs | 1 + 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs b/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs index 95d3450..be5ce86 100644 --- a/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs +++ b/src/Luban.Job.Cfg/Source/DataCreators/SheetDataCreator.cs @@ -450,6 +450,7 @@ namespace Luban.Job.Cfg.DataCreators } else if (row.Fields != null) { + sep += type.GetBeanAs().Sep; var originBean = (DefBean)type.Bean; if (originBean.IsAbstractType) { @@ -458,7 +459,7 @@ namespace Luban.Job.Cfg.DataCreators { throw new Exception($"type:'{originBean.FullName}' 是多态类型,需要定义'{DefBean.EXCEL_TYPE_NAME_KEY}'列来指定具体子类型"); } - + TitleRow valueTitle = row.GetSubTitleNamedRow(DefBean.EXCEL_VALUE_NAME_KEY); string subType = typeTitle.Current?.ToString()?.Trim(); if (subType == null || subType == DefBean.BEAN_NULL_STR) { @@ -469,7 +470,35 @@ namespace Luban.Job.Cfg.DataCreators return null; } DefBean implType = DataUtil.GetImplTypeByNameOrAlias(originBean, subType); - return new DBean(type, implType, CreateBeanFields(implType, sheet, row)); + if (valueTitle == null) + { + return new DBean(type, implType, CreateBeanFields(implType, sheet, row)); + } + else + { + if (valueTitle.Row != null) + { + var s = valueTitle.AsStream(sep); + if (type.IsNullable && s.TryReadEOF()) + { + return null; + } + return new DBean(type, implType, CreateBeanFields(implType, s)); + } + else if (valueTitle.Rows != null) + { + var s = valueTitle.AsMultiRowConcatStream(sep); + if (type.IsNullable && s.TryReadEOF()) + { + return null; + } + return new DBean(type, implType, CreateBeanFields(implType, s)); + } + else + { + throw new Exception(); + } + } } else { @@ -646,5 +675,29 @@ namespace Luban.Job.Cfg.DataCreators throw new Exception(); } } + + private List CreateBeanFields(DefBean bean, ExcelStream stream) + { + var list = new List(); + foreach (DefField f in bean.HierarchyFields) + { + try + { + list.Add(f.CType.Apply(ExcelStreamDataCreator.Ins, stream)); + } + catch (DataCreateException dce) + { + dce.Push(bean, f); + throw; + } + catch (Exception e) + { + var dce = new DataCreateException(e, stream.LastReadDataInfo); + dce.Push(bean, f); + throw dce; + } + } + return list; + } } } diff --git a/src/Luban.Job.Cfg/Source/DataSources/Excel/TitleRow.cs b/src/Luban.Job.Cfg/Source/DataSources/Excel/TitleRow.cs index 05fadb8..d7f0449 100644 --- a/src/Luban.Job.Cfg/Source/DataSources/Excel/TitleRow.cs +++ b/src/Luban.Job.Cfg/Source/DataSources/Excel/TitleRow.cs @@ -105,7 +105,6 @@ namespace Luban.Job.Cfg.DataSources.Excel } } - public bool HasSubFields => Fields != null || Elements != null; public TitleRow(Title selfTitle, List row) diff --git a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs index cb7a91f..6b19972 100644 --- a/src/Luban.Job.Cfg/Source/Defs/DefBean.cs +++ b/src/Luban.Job.Cfg/Source/Defs/DefBean.cs @@ -23,6 +23,7 @@ namespace Luban.Job.Cfg.Defs public const string LUA_TYPE_NAME_KEY = "_type_"; public const string EXCEL_TYPE_NAME_KEY = "$type"; + public const string EXCEL_VALUE_NAME_KEY = "$value"; public string JsonTypeNameKey => JSON_TYPE_NAME_KEY;