[new]支持$type + 流式$value的填写方式 (#29)

[new]支持$type + 流式$value的填写方式

Co-authored-by: Tianbao Lin <kteong1012@outlook.com>
main
Carson - 宝鱼 2022-10-20 09:04:42 +08:00 committed by GitHub
parent 165ad69a5d
commit bcc7bc8b37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 3 deletions

View File

@ -450,6 +450,7 @@ namespace Luban.Job.Cfg.DataCreators
}
else if (row.Fields != null)
{
sep += type.GetBeanAs<DefBean>().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<DType> CreateBeanFields(DefBean bean, ExcelStream stream)
{
var list = new List<DType>();
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;
}
}
}

View File

@ -105,7 +105,6 @@ namespace Luban.Job.Cfg.DataSources.Excel
}
}
public bool HasSubFields => Fields != null || Elements != null;
public TitleRow(Title selfTitle, List<Cell> row)

View File

@ -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;