【修复】修复读取深层次容器未使用sep的bug

main
walon 2022-01-19 21:16:49 +08:00
parent 36df70bffb
commit d40c016afa
2 changed files with 48 additions and 44 deletions

View File

@ -3,6 +3,7 @@ using Luban.Common.Utils;
using Luban.Job.Cfg.Datas; using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources.Excel; using Luban.Job.Cfg.DataSources.Excel;
using Luban.Job.Cfg.Defs; using Luban.Job.Cfg.Defs;
using Luban.Job.Cfg.TypeVisitors;
using Luban.Job.Cfg.Utils; using Luban.Job.Cfg.Utils;
using Luban.Job.Common.Types; using Luban.Job.Common.Types;
using Luban.Job.Common.TypeVisitors; using Luban.Job.Common.TypeVisitors;
@ -263,6 +264,8 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TText type, ExcelStream x) public DType Accept(TText type, ExcelStream x)
{ {
//x = SepIfNeed(type, x); //x = SepIfNeed(type, x);
x = TrySep(type, x);
string key = ParseString(x.Read()); string key = ParseString(x.Read());
if (key == null) if (key == null)
{ {
@ -372,12 +375,15 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TBean type, ExcelStream x) public DType Accept(TBean type, ExcelStream x)
{ {
var originX = x;
var originBean = (DefBean)type.Bean; var originBean = (DefBean)type.Bean;
if (!string.IsNullOrEmpty(originBean.Sep)) if (!string.IsNullOrEmpty(originBean.Sep))
{ {
x = new ExcelStream(x.ReadCell(), originBean.Sep); x = new ExcelStream(x.ReadCell(), originBean.Sep);
} }
else
{
x = TrySep(type, x);
}
if (originBean.IsAbstractType) if (originBean.IsAbstractType)
{ {
@ -416,50 +422,59 @@ namespace Luban.Job.Cfg.DataCreators
} }
} }
private static ExcelStream TrySep(TType type, ExcelStream stream)
{
string sep = type.GetTag("sep");
if (string.IsNullOrEmpty(sep) && type.ElementType != null && type.ElementType.Apply(IsNotSepTypeVisitor.Ins))
{
sep = DataUtil.SimpleContainerSep;
}
if (!string.IsNullOrEmpty(sep) && !stream.TryReadEOF())
{
stream = new ExcelStream(stream.ReadCell(), sep);
}
return stream;
}
// 容器类统统不支持 type.IsNullable // 容器类统统不支持 type.IsNullable
// 因为貌似没意义? // 因为貌似没意义?
public List<DType> ReadList(TType type, ExcelStream stream) public List<DType> ReadList(TType type, TType eleType, ExcelStream stream)
{ {
var datas = new List<DType>(); var datas = new List<DType>();
string elementSep = type.GetTag("sep"); stream = TrySep(type, stream);
while (!stream.TryReadEOF()) while (!stream.TryReadEOF())
{ {
if (string.IsNullOrEmpty(elementSep)) datas.Add(eleType.Apply(this, stream));
{
datas.Add(type.Apply(this, stream));
}
else
{
datas.Add(type.Apply(this, new ExcelStream(stream.ReadCell(), elementSep)));
}
} }
return datas; return datas;
} }
public DType Accept(TArray type, ExcelStream x) public DType Accept(TArray type, ExcelStream x)
{ {
return new DArray(type, ReadList(type.ElementType, x)); return new DArray(type, ReadList(type, type.ElementType, x));
} }
public DType Accept(TList type, ExcelStream x) public DType Accept(TList type, ExcelStream x)
{ {
return new DList(type, ReadList(type.ElementType, x)); return new DList(type, ReadList(type, type.ElementType, x));
} }
public DType Accept(TSet type, ExcelStream x) public DType Accept(TSet type, ExcelStream x)
{ {
return new DSet(type, ReadList(type.ElementType, x)); return new DSet(type, ReadList(type, type.ElementType, x));
} }
public DType Accept(TMap type, ExcelStream x) public DType Accept(TMap type, ExcelStream stream)
{ {
//x = SepIfNeed(type, x); //x = SepIfNeed(type, x);
stream = TrySep(type, stream);
var datas = new Dictionary<DType, DType>(); var datas = new Dictionary<DType, DType>();
while (!x.TryReadEOF()) while (!stream.TryReadEOF())
{ {
var key = type.KeyType.Apply(this, x); var key = type.KeyType.Apply(this, stream);
var value = type.ValueType.Apply(this, x); var value = type.ValueType.Apply(this, stream);
if (!datas.TryAdd(key, value)) if (!datas.TryAdd(key, value))
{ {
throw new InvalidExcelDataException($"map 的 key:{key} 重复"); throw new InvalidExcelDataException($"map 的 key:{key} 重复");

View File

@ -249,7 +249,10 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TText type, Sheet sheet, TitleRow row) public DType Accept(TText type, Sheet sheet, TitleRow row)
{ {
if (!type.HasTag("sep")) //var s = row.AsStream(row.SelfTitle.Sep);
//return type.Apply(ExcelStreamDataCreator.Ins, s);
if (string.IsNullOrEmpty(row.SelfTitle.SepOr(type.GetTag("sep"))))
{ {
if (row.CellCount != 2) if (row.CellCount != 2)
{ {
@ -267,7 +270,7 @@ namespace Luban.Job.Cfg.DataCreators
} }
else else
{ {
var s = row.AsStream(type.GetTag("sep")); var s = row.AsStream(row.SelfTitle.Sep);
return type.Apply(ExcelStreamDataCreator.Ins, s); return type.Apply(ExcelStreamDataCreator.Ins, s);
} }
} }
@ -364,12 +367,7 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TBean type, Sheet sheet, TitleRow row) public DType Accept(TBean type, Sheet sheet, TitleRow row)
{ {
string sep = "";// type.GetBeanAs<DefBean>().Sep; string sep = row.SelfTitle.Sep;// type.GetBeanAs<DefBean>().Sep;
if (string.IsNullOrWhiteSpace(sep))
{
sep = row.SelfTitle.SepOr(type.GetTag("sep"));
}
if (row.Row != null) if (row.Row != null)
{ {
@ -482,16 +480,16 @@ namespace Luban.Job.Cfg.DataCreators
return datas; return datas;
} }
private List<DType> ReadCollectionDatas(TType elementType, Sheet sheet, TitleRow row, string containerSep) private List<DType> ReadCollectionDatas(TType type, TType elementType, Sheet sheet, TitleRow row)
{ {
if (row.Row != null) if (row.Row != null)
{ {
var s = row.AsStream(string.IsNullOrEmpty(containerSep) ? DataUtil.GetCollectionElementTypeSep(elementType) : containerSep); var s = row.AsStream(row.SelfTitle.Sep);
return ExcelStreamDataCreator.Ins.ReadList(elementType, s); return ExcelStreamDataCreator.Ins.ReadList(type, elementType, s);
} }
else if (row.Rows != null) else if (row.Rows != null)
{ {
var s = row.AsMultiRowStream(string.IsNullOrEmpty(containerSep) ? DataUtil.GetCollectionElementTypeSep(elementType) : containerSep); var s = row.AsMultiRowStream(row.SelfTitle.Sep);
return ReadList(elementType, s); return ReadList(elementType, s);
} }
else if (row.Fields != null) else if (row.Fields != null)
@ -524,36 +522,27 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TArray type, Sheet sheet, TitleRow row) public DType Accept(TArray type, Sheet sheet, TitleRow row)
{ {
//string sep = DataUtil.GetSep(type); //string sep = DataUtil.GetSep(type);
return new DArray(type, ReadCollectionDatas(type.ElementType, sheet, row, row.SelfTitle.SepOr(type.GetTag("sep")))); return new DArray(type, ReadCollectionDatas(type, type.ElementType, sheet, row));
} }
public DType Accept(TList type, Sheet sheet, TitleRow row) public DType Accept(TList type, Sheet sheet, TitleRow row)
{ {
return new DList(type, ReadCollectionDatas(type.ElementType, sheet, row, row.SelfTitle.SepOr(type.GetTag("sep")))); return new DList(type, ReadCollectionDatas(type, type.ElementType, sheet, row));
} }
public DType Accept(TSet type, Sheet sheet, TitleRow row) public DType Accept(TSet type, Sheet sheet, TitleRow row)
{ {
return new DSet(type, ReadCollectionDatas(type.ElementType, sheet, row, row.SelfTitle.SepOr(type.GetTag("sep")))); return new DSet(type, ReadCollectionDatas(type, type.ElementType, sheet, row));
} }
public DType Accept(TMap type, Sheet sheet, TitleRow row) public DType Accept(TMap type, Sheet sheet, TitleRow row)
{ {
string sep = row.SelfTitle.SepOr(type.GetTag("sep")); string sep = row.SelfTitle.Sep;
if (row.Row != null) if (row.Row != null)
{ {
var s = row.AsStream(sep); var s = row.AsStream(sep);
var datas = new Dictionary<DType, DType>(); return type.Apply(ExcelStreamDataCreator.Ins, s);
while (!s.TryReadEOF())
{
var key = type.KeyType.Apply(ExcelStreamDataCreator.Ins, s);
var value = type.ValueType.Apply(ExcelStreamDataCreator.Ins, s);
datas.Add(key, value);
}
return new DMap(type, datas);
} }
else if (row.Rows != null) else if (row.Rows != null)
{ {