【优化】cfg 从excel中读取可空变量值时,如果单元格为空,则取null,而不是false,0之类。

【优化】cfg excel格式支持空单元格表示vector{2,3,4}类型的默认值
main
walon 2021-08-25 13:43:58 +08:00
parent 216bdee29d
commit 36be33dea8
6 changed files with 48 additions and 33 deletions

View File

@ -44,16 +44,17 @@ namespace Luban.Job.Cfg.DataCreators
private bool CheckNull(bool nullable, object o)
{
if (o is string s && s == "null")
return nullable && (o == null || (o is string s && s == "null"));
}
private bool CheckIsDefault(bool namedMode, object value)
{
if (namedMode)
{
if (nullable)
if (value == null || (value is string s && string.IsNullOrEmpty(s)))
{
return true;
}
else
{
throw new InvalidExcelDataException($"单元格没有填有效数据");
}
}
return false;
}
@ -75,18 +76,6 @@ namespace Luban.Job.Cfg.DataCreators
}
}
private bool CheckIsDefault(bool namedMode, object value)
{
if (namedMode)
{
if (value == null || (value is string s && string.IsNullOrEmpty(s)))
{
return true;
}
}
return false;
}
public DType Accept(TBool type, object converter, ExcelStream x, DefAssembly ass)
{
if (x.NamedMode && x.IncludeNullAndEmptySize != 1)
@ -338,7 +327,11 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TEnum type, object converter, ExcelStream x, DefAssembly ass)
{
var d = x.Read();
var d = x.Read(x.NamedMode);
if (CheckNull(type.IsNullable, d))
{
return null;
}
if (CheckNull(type.IsNullable, d))
{
return null;
@ -541,31 +534,55 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TVector2 type, object converter, ExcelStream x, DefAssembly ass)
{
var d = x.Read();
if (x.NamedMode && x.IncludeNullAndEmptySize != 1)
{
throw new InvalidExcelDataException("在标题头对应模式下必须正好占据1个单元格");
}
var d = x.Read(x.NamedMode);
if (CheckNull(type.IsNullable, d))
{
return null;
}
if (CheckIsDefault(x.NamedMode, d))
{
return DVector2.Default;
}
return DataUtil.CreateVector(type, d.ToString());
}
public DType Accept(TVector3 type, object converter, ExcelStream x, DefAssembly ass)
{
var d = x.Read();
if (x.NamedMode && x.IncludeNullAndEmptySize != 1)
{
throw new InvalidExcelDataException("在标题头对应模式下必须正好占据1个单元格");
}
var d = x.Read(x.NamedMode);
if (CheckNull(type.IsNullable, d))
{
return null;
}
if (CheckIsDefault(x.NamedMode, d))
{
return DVector3.Default;
}
return DataUtil.CreateVector(type, d.ToString());
}
public DType Accept(TVector4 type, object converter, ExcelStream x, DefAssembly ass)
{
var d = x.Read();
if (x.NamedMode && x.IncludeNullAndEmptySize != 1)
{
throw new InvalidExcelDataException("在标题头对应模式下必须正好占据1个单元格");
}
var d = x.Read(x.NamedMode);
if (CheckNull(type.IsNullable, d))
{
return null;
}
if (CheckIsDefault(x.NamedMode, d))
{
return DVector4.Default;
}
return DataUtil.CreateVector(type, d.ToString());
}

View File

@ -120,14 +120,6 @@ namespace Luban.Job.Cfg.DataCreators
try
{
list.Add(f.CType.Apply(this, row.GetSubTitleNamedRow(fname), f.IsMultiRow, f.IsNullable));
//if (f.IsMultiRow)
//{
// list.Add(f.CType.Apply(this, row.GetSubTitleNamedRowOfMultiRows(fname), f.IsMultiRow, f.IsNullable));
//}
//else
//{
// list.Add(f.CType.Apply(this, row.GetSubTitleNamedRow(fname), f.IsMultiRow /* 肯定是 false */, f.IsNullable));
//}
}
catch (DataCreateException dce)
{

View File

@ -5,6 +5,8 @@ namespace Luban.Job.Cfg.Datas
{
public class DVector2 : DType<Vector2>
{
public static DVector2 Default { get; } = new DVector2(default);
public DVector2(Vector2 x) : base(x)
{
}

View File

@ -5,6 +5,8 @@ namespace Luban.Job.Cfg.Datas
{
public class DVector3 : DType<Vector3>
{
public static DVector3 Default { get; } = new DVector3(default);
public DVector3(Vector3 x) : base(x)
{
}

View File

@ -5,6 +5,8 @@ namespace Luban.Job.Cfg.Datas
{
public class DVector4 : DType<Vector4>
{
public static DVector4 Default { get; } = new DVector4(default);
public DVector4(Vector4 x) : base(x)
{
}

View File

@ -40,17 +40,17 @@ namespace Luban.Job.Cfg.TypeVisitors
public override bool Accept(TVector2 type)
{
return true;
return false;
}
public override bool Accept(TVector3 type)
{
return true;
return false;
}
public override bool Accept(TVector4 type)
{
return true;
return false;
}
}
}