【特性】excel title新增属性 non_empty,表示字段单元格值不能为空。 <name>&non_empty=1等效于 !<name>
【优化】excel中第一个字段默认为non_empty,避免了绝大多数情况下失误未填key时将空单元格当作key=默认值的问题main
parent
06467344a5
commit
029ac6ebd8
|
|
@ -29,6 +29,14 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
return o == null || (o is string s && s.Length == 0);
|
||||
}
|
||||
|
||||
private void ThrowIfNonEmpty(TitleRow row)
|
||||
{
|
||||
if (row.SelfTitle.NonEmpty)
|
||||
{
|
||||
throw new Exception($"字段不允许为空");
|
||||
}
|
||||
}
|
||||
|
||||
public DType Accept(TBool type, Sheet sheet, TitleRow row)
|
||||
{
|
||||
object x = row.Current;
|
||||
|
|
@ -38,6 +46,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DBool.ValueOf(false);
|
||||
}
|
||||
if (x is bool v)
|
||||
|
|
@ -70,6 +79,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DShort.Default;
|
||||
}
|
||||
return DShort.ValueOf(short.Parse(x.ToString()));
|
||||
|
|
@ -98,6 +108,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DInt.Default;
|
||||
}
|
||||
return DInt.ValueOf(int.Parse(x.ToString()));
|
||||
|
|
@ -112,6 +123,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DFint.Default;
|
||||
}
|
||||
return DFint.ValueOf(int.Parse(x.ToString()));
|
||||
|
|
@ -126,6 +138,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DLong.Default;
|
||||
}
|
||||
return DLong.ValueOf(long.Parse(x.ToString()));
|
||||
|
|
@ -140,6 +153,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DFlong.Default;
|
||||
}
|
||||
return DFlong.ValueOf(long.Parse(x.ToString()));
|
||||
|
|
@ -154,6 +168,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DFloat.Default;
|
||||
}
|
||||
return DFloat.ValueOf(float.Parse(x.ToString()));
|
||||
|
|
@ -168,6 +183,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DDouble.Default;
|
||||
}
|
||||
return DDouble.ValueOf(double.Parse(x.ToString()));
|
||||
|
|
@ -202,7 +218,12 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
|
||||
public DType Accept(TString type, Sheet sheet, TitleRow row)
|
||||
{
|
||||
var s = ParseString(row.Current);
|
||||
object x = row.Current;
|
||||
if (CheckDefault(x))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
}
|
||||
var s = ParseString(x);
|
||||
if (s == null)
|
||||
{
|
||||
if (type.IsNullable)
|
||||
|
|
@ -270,7 +291,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
var dce = new DataCreateException(e, $"列:{fname}");
|
||||
var dce = new DataCreateException(e, $"字段:{fname}");
|
||||
dce.Push(bean, f);
|
||||
throw dce;
|
||||
}
|
||||
|
|
@ -524,6 +545,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(d))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DVector2.Default;
|
||||
}
|
||||
return DataUtil.CreateVector(type, d.ToString());
|
||||
|
|
@ -538,6 +560,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(d))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DVector3.Default;
|
||||
}
|
||||
return DataUtil.CreateVector(type, d.ToString());
|
||||
|
|
@ -552,6 +575,7 @@ namespace Luban.Job.Cfg.DataCreators
|
|||
}
|
||||
if (CheckDefault(d))
|
||||
{
|
||||
ThrowIfNonEmpty(row);
|
||||
return DVector4.Default;
|
||||
}
|
||||
return DataUtil.CreateVector(type, d.ToString());
|
||||
|
|
|
|||
|
|
@ -146,15 +146,16 @@ namespace Luban.Job.Cfg.DataSources.Excel
|
|||
string titleName = attrs[0];
|
||||
var tags = new Dictionary<string, string>();
|
||||
// * 开头的表示是多行
|
||||
#if !LUBAN_LITE
|
||||
if (titleName.StartsWith('*'))
|
||||
#else
|
||||
if (titleName.StartsWith("*"))
|
||||
#endif
|
||||
{
|
||||
titleName = titleName.Substring(1);
|
||||
tags.Add("multi_rows", "1");
|
||||
}
|
||||
if (titleName.StartsWith("!"))
|
||||
{
|
||||
titleName = titleName.Substring(1);
|
||||
tags.Add("non_empty", "1");
|
||||
}
|
||||
foreach (var attrPair in attrs.Skip(1))
|
||||
{
|
||||
var pairs = attrPair.Split('=');
|
||||
|
|
|
|||
|
|
@ -30,6 +30,8 @@ namespace Luban.Job.Cfg.DataSources.Excel
|
|||
return string.IsNullOrEmpty(Sep) ? sep : Sep;
|
||||
}
|
||||
|
||||
public bool NonEmpty { get; private set; }
|
||||
|
||||
public string Default { get; private set; }
|
||||
|
||||
public bool SelfMultiRows { get; private set; }
|
||||
|
|
@ -63,10 +65,16 @@ namespace Luban.Job.Cfg.DataSources.Excel
|
|||
{
|
||||
SortSubTitles();
|
||||
Sep = Tags.TryGetValue("sep", out var v) && !string.IsNullOrWhiteSpace(v) ? v : null;
|
||||
NonEmpty = Tags.TryGetValue("non_empty", out var ne) && ne == "1";
|
||||
SelfMultiRows = Tags.TryGetValue("multi_rows", out var v2) && (v2 == "1" || v2 == "true");
|
||||
Default = Tags.TryGetValue("default", out var v3) ? v3 : null;
|
||||
if (SubTitleList.Count > 0)
|
||||
{
|
||||
if (Root)
|
||||
{
|
||||
// 第一个字段一般为key,为了避免失误将空单元格当作key=0的数据,默认非空
|
||||
SubTitleList[0].Tags.TryAdd("non_empty", "1");
|
||||
}
|
||||
foreach (var sub in SubTitleList)
|
||||
{
|
||||
sub.Init();
|
||||
|
|
|
|||
Loading…
Reference in New Issue