/// <summary> /// 解析标量 /// </summary> /// <param name="type"></param> /// <param name="jsonValue"></param> /// <param name="report"></param> /// <returns></returns> private static object ParseScalar(string type, JsonParser.JsonValueContext jsonValue, ReportWrap report) { IToken token = jsonValue.boolValue ?? jsonValue.intValue ?? jsonValue.floatValue ?? jsonValue.strValue ?? null; if (token != null) { object result = BaseUtil.GetScalar(type, token.Text.Trim('"')); if (result != null) { return(result); } else { if (BaseUtil.IsInteger(type) || BaseUtil.IsFloat(type)) { report.OnError(string.Format("值\"{0}\"无法转换成{1},或超出{1}的精度范围.", token.Text.Trim('"'), type), token); } else { report.OnError(string.Format("值\"{0}\"无法转换成{1}.", token.Text.Trim('"'), type), token); } } } else { report.OnError(string.Format("\"{0}\"无法转换成{1}.", jsonValue.GetText(), type), jsonValue); } return(null); }
private static void ParseTable(Table table, CsvTable dataset, CSV csv) { var titleRowIndex = csv.titleRow - 1; var dataBeginRowIndex = csv.dataBeginRow - 1; var columnName2ColumnIndex = new Dictionary <string, int>(); //验证标题行有效性 if (titleRowIndex < 0 || titleRowIndex >= dataset.Count) { LogError(csv.filePath, titleRowIndex, -1, "标题行不存在!"); return; } //验证数据起始行有效性 if ((dataBeginRowIndex < 0 || dataBeginRowIndex >= dataset.Count)) { LogError(csv.filePath, dataBeginRowIndex, -1, "数据起始行不存在!"); return; } //检查标题行是否有重复的列名 CsvRow titleRow = dataset[titleRowIndex]; for (int i = 0; i < titleRow.Count; i++) { string colName = titleRow[i].text != null ? titleRow[i].text.Trim() : ""; if (string.IsNullOrEmpty(colName)) { continue; } if (columnName2ColumnIndex.ContainsKey(colName)) { LogError(csv.filePath, titleRowIndex, i, string.Format("标题行有重复列名:{0}!", colName)); } else { columnName2ColumnIndex.Add(colName, i); } } //检查标题行是否有有效数据 if (columnName2ColumnIndex.Count == 0) { LogError(csv.filePath, titleRowIndex, -1, "标题行没有有效的数据!"); return; } //检测数据表中是否缺少字段 foreach (var field in table.Fields) { var fieldName = field.DataField; if (!columnName2ColumnIndex.ContainsKey(fieldName)) { LogError(csv.filePath, titleRowIndex, -1, string.Format("标题行缺少名为\"{0}\"的列!", fieldName)); } } //找出所有带索引的列 var indexKeys = new HashSet <string>(); foreach (var index in table.Attributes.GetAttributes <Index>()) { foreach (var indexKey in index.IndexFields) { foreach (var field in table.Fields) { if (field.Name.Equals(indexKey)) { indexKeys.Add(field.DataField); } } } } //转换数据 for (int i = dataBeginRowIndex; i < dataset.Count; i++) { //忽略空行 if (dataset[i].isEmpty) { LogWarning(csv.filePath, i, -1, "整行内容为空,忽略!"); continue; } foreach (var field in table.Fields) { string fieldName = field.DataField; object fieldValue = null; object fieldDefaultValue = field.DefaultValue; if (!columnName2ColumnIndex.ContainsKey(fieldName) || (columnName2ColumnIndex[fieldName] < 0 || columnName2ColumnIndex[fieldName] >= dataset[i].Count)) { //缺少对应的数据列 | 列索引超出数据行总列数 if (BaseUtil.IsBaseType(field.Type)) { fieldValue = BaseUtil.GetDefaultValue(field.Type, fieldDefaultValue); } } else { bool isIndex = indexKeys.Contains(fieldName); bool isUnique = field.Attributes.GetAttribute <Unique>() != null; bool isNullable = field.Attributes.HasAttribute <Nullable>(); int columnIndex = columnName2ColumnIndex[fieldName]; CsvCol column = dataset[i][columnIndex]; string columnText = column.text; bool isEmpty = string.IsNullOrEmpty(columnText); if (BaseUtil.IsBaseType(field.Type)) { if (field.IsArray) { } else { object scalarValue = null; if (isEmpty) { if (!isNullable) { LogError(csv.filePath, i, columnIndex, string.Format("列{0}不允许为空!", fieldName)); } if (isUnique) { LogError(csv.filePath, i, columnIndex, string.Format("列{0}有唯一性约束,不允许为空!", fieldName)); } if (isIndex) { LogError(csv.filePath, i, columnIndex, string.Format("列{0}有索引,不允许为空!", fieldName)); } scalarValue = BaseUtil.GetDefaultValue(field.Type, fieldDefaultValue); } else { scalarValue = BaseUtil.GetScalar(field.Type, columnText); if (scalarValue == null) { LogError(csv.filePath, i, columnIndex, string.Format("'{0}'无法转换成一个'{1}'!", columnText, field.Type)); } } } } else if (field.TypeDefined is Model.Struct) { } else if (field.TypeDefined is Model.Enum) { } } } } }
/// <summary> /// 获取列表格式的结构 /// </summary> /// <param name="literal"></param> /// <param name="type"></param> /// <param name="text"></param> /// <param name="errors"></param> /// <returns></returns> private static object GetStruct(StructLiteral literal, Model.Struct type, string text, List <string> errors) { text = text.Trim(); //查找自定义的分割规则 var separator = ","; if (literal != null) { separator = literal.separator; if (text.StartsWith(literal.beginning)) { text = text.Substring(literal.beginning.Length); } if (text.EndsWith(literal.ending)) { text = text.Substring(0, text.Length - literal.ending.Length); } } //分割字符串 var texts = new string[] { }; if (!string.IsNullOrEmpty(text)) { texts = text.Split(new string[] { separator }, StringSplitOptions.None); } //解析字符串 var values = new Dictionary <string, object>(); for (int i = 0; i < texts.Length; i++) { var txt = texts[i].Trim(); if (i < type.Fields.Count) { var field = type.Fields[i]; var fieldType = field.Type; if (BaseUtil.IsInteger(fieldType) || BaseUtil.IsFloat(fieldType) || BaseUtil.IsBool(fieldType)) { object value = BaseUtil.GetScalar(fieldType, txt); if (value != null) { values.Add(field.Name, value); } else { errors.Add(string.Format("第{0}个元素{1}无法解析成{2}。", i, txt, fieldType)); } } else if (BaseUtil.IsString(fieldType)) { values.Add(field.Name, txt); } else if (field.TypeDefined is Model.Enum) { var value = BaseUtil.GetEnum(field.TypeDefined as Model.Enum, txt); if (value != null) { values.Add(field.Name, value); } else { errors.Add(string.Format("第{0}个元素{1}无法解析成{2}。", i, txt, fieldType)); } } else if (field.TypeDefined is Model.Struct) { var value = GetStruct(field.Attributes, field.TypeDefined as Model.Struct, txt, errors); if (value != null) { values.Add(field.Name, value); } } } else { errors.Add(string.Format("第{0}个元素将被忽略,因为{1}的字段数量只有{2}个。", i, type.Name, type.Fields.Count)); } } for (int i = texts.Length; i < type.Fields.Count; i++) { errors.Add(String.Format("字段{0}没有对应的数据项,因为数据只有{1}个元素。", type.Fields[i].Name, texts.Length)); } return(values); }