public static List <Record> LoadCfgRecords(TBean recordType, string originFile, string sheetName, byte[] content, bool multiRecord)
        {
            // (md5,sheet,multiRecord,exportTestData) -> (valuetype, List<(datas)>)
            var dataSource = DataSourceFactory.Create(originFile, sheetName, new MemoryStream(content));

            try
            {
                if (multiRecord)
                {
                    return(dataSource.ReadMulti(recordType));
                }
                else
                {
                    Record record = dataSource.ReadOne(recordType);
                    return(record != null ? new List <Record> {
                        record
                    } : new List <Record>());
                }
            }
            catch (DataCreateException dce)
            {
                if (string.IsNullOrWhiteSpace(dce.OriginDataLocation))
                {
                    dce.OriginDataLocation = originFile;
                }
                throw;
            }
            catch (Exception e)
            {
                throw new Exception($"配置文件:{originFile} 生成失败.", e);
            }
        }
        public override void Accept(TBean type, DType x)
        {
            var beanData  = (DBean)x;
            var defFields = ((DefBean)type.Bean.AssemblyBase.GetDefType(beanData.ImplType.FullName)).HierarchyFields;// beanData.ImplType.HierarchyFields;
            int i         = 0;

            foreach (var fieldValue in beanData.Fields)
            {
                var defField = (DefField)defFields[i++];
                _path.Push(defField.Name);

                var fieldType = defField.CType;

                if (fieldType.Processors.Count > 0)
                {
                    foreach (var p in fieldType.Processors)
                    {
                        if (p is IValidator val)
                        {
                            val.Validate(Ctx, fieldType, fieldValue);
                        }
                    }
                }
                if (fieldValue != null)
                {
                    fieldType.Apply(this, fieldValue);
                }
                _path.Pop();
            }
        }
        public override List <Record> ReadMulti(TBean type)
        {
            var datas = new List <Record>();

            foreach (var sheet in _sheets)
            {
                try
                {
                    foreach (var r in sheet.GetRows())
                    {
                        TitleRow row    = r.Row;
                        string   tagStr = r.Tag;
                        if (DataUtil.IsIgnoreTag(tagStr))
                        {
                            continue;
                        }
                        var data = (DBean)type.Apply(SheetDataCreator.Ins, sheet, row);
                        datas.Add(new Record(data, sheet.RawUrl, DataUtil.ParseTags(tagStr)));
                    }
                }
                catch (DataCreateException dce)
                {
                    dce.OriginDataLocation = sheet.RawUrl;
                    throw;
                }
                catch (Exception e)
                {
                    throw new Exception($"sheet:{sheet.Name}", e);
                }
            }
            return(datas);
        }
Beispiel #4
0
 public static string GetBeanSep(TBean type)
 {
     if (type.Tags != null && type.Tags.TryGetValue("sep", out var s) && !string.IsNullOrWhiteSpace(s))
     {
         return(s);
     }
     return(((DefBean)type.Bean).Sep);
 }
Beispiel #5
0
        public List <DType> ReadMulti(TBean type, bool enableMultiRowRecord)
        {
            var datas = new List <DType>();

            for (DType data; (data = ReadOne(type, enableMultiRowRecord)) != null;)
            {
                datas.Add(data);
            }
            return(datas);
        }
Beispiel #6
0
        public static List <ResourceInfo> ExportResourceList(List <Record> records)
        {
            var resList = new List <ResourceInfo>();

            foreach (Record res in records)
            {
                ResourceExportor.Ins.Accept(res.Data, TBean.Create(false, res.Data.Type, null), resList);
            }
            return(resList);
        }
 public override string Accept(TBean type, string bufVarName, string fieldName)
 {
     if (type.Bean.IsAbstractType)
     {
         return($"{fieldName} = {type.Bean.FullName}.deserializeFrom({bufVarName})");
     }
     else
     {
         return($"{fieldName} = new {type.Bean.FullName}(); {fieldName}.deserialize({bufVarName})");
     }
 }
 public string Accept(TBean type, string bufName, string fieldName)
 {
     if (type.IsDynamic)
     {
         return($"{fieldName} = {type.Bean.FullNameWithTopModule}.deserialize{type.Bean.Name}({bufName});");
     }
     else
     {
         return($"{fieldName} = new {type.Bean.FullNameWithTopModule}({bufName});");
     }
 }
Beispiel #9
0
 public string Accept(TBean type, string fieldName, string logType)
 {
     if (type.Bean.IsAbstractType)
     {
         return($"{fieldName} = default;");
     }
     else
     {
         return($"{fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}();");
     }
 }
Beispiel #10
0
 public string Accept(TBean type, string json, string field)
 {
     if (type.Bean.IsAbstractType)
     {
         return($"if(!JsonUtil::WriteDynamicBean({json}, {field})) {{ return false; }}");
     }
     else
     {
         return($"if(!JsonUtil::WriteBean({json}, {field})) {{ return false; }}");
     }
 }
Beispiel #11
0
        public override List <DType> ReadMulti(TBean type)
        {
            var records = new List <DType>();

            foreach (LuaTable t in _dataTable.Values.Values)
            {
                records.Add(type.Apply(LuaDataCreator.Ins, t, (DefAssembly)type.Bean.AssemblyBase));
            }

            return(records);
        }
Beispiel #12
0
 public string Accept(TBean type, string jsonVarName, string fieldName)
 {
     if (type.Bean.IsAbstractType)
     {
         return($"{fieldName} = {type.Bean.FullName}.deserialize({jsonVarName});");
     }
     else
     {
         return($"{fieldName} = new {type.Bean.FullName}({jsonVarName});");
     }
 }
Beispiel #13
0
 public string Accept(TBean type, string json, string field)
 {
     if (type.Bean.IsAbstractType)
     {
         return($"if(!JsonUtil::ReadDynamicBean({json}, \"{field}\", this->{field})){{ return false; }}");
     }
     else
     {
         return($"if(!JsonUtil::ReadBean({json}, \"{field}\", this->{field})) {{ return false; }}");
     }
 }
Beispiel #14
0
 public string Accept(TBean type, string jsonVarName, string fieldName)
 {
     if (type.Bean.IsAbstractType)
     {
         return($"{fieldName} = {type.Bean.FullName}.constructorFrom({jsonVarName})");
     }
     else
     {
         return($"{fieldName} = new {type.Bean.FullName}({jsonVarName})");
     }
 }
Beispiel #15
0
 public string Accept(TBean type, string jsonVarName, string fieldName)
 {
     if (type.Bean.IsAbstractType)
     {
         return($"{fieldName} = {type.Bean.PyFullName}.fromJson({jsonVarName})");
     }
     else
     {
         return($"{fieldName} = {type.Bean.PyFullName}({jsonVarName})");
     }
 }
Beispiel #16
0
        public DType Accept(TBean type, XElement x, DefAssembly ass)
        {
            var bean = (DefBean)type.Bean;

            DefBean implBean;

            if (bean.IsAbstractType)
            {
                string subType = x.Attribute(DefBean.TYPE_NAME_KEY)?.Value;
                if (string.IsNullOrWhiteSpace(subType))
                {
                    throw new Exception($"bean:'{bean.FullName}'是多态,需要指定{DefBean.TYPE_NAME_KEY}属性.\n xml:{x}");
                }
                implBean = DataUtil.GetImplTypeByNameOrAlias(bean, subType);
            }
            else
            {
                implBean = bean;
            }

            var fields = new List <DType>();

            foreach (DefField f in implBean.HierarchyFields)
            {
                var      feles = x.Elements(f.Name);
                XElement fele  = feles.FirstOrDefault();
                if (fele == null)
                {
                    if (f.CType.IsNullable)
                    {
                        fields.Add(null);
                        continue;
                    }
                    throw new Exception($"字段:{f.Name} 缺失");
                }
                try
                {
                    fields.Add(f.CType.Apply(this, fele, ass));
                }
                catch (DataCreateException dce)
                {
                    dce.Push(implBean, f);
                    throw;
                }
                catch (Exception e)
                {
                    var dce = new DataCreateException(e, "");
                    dce.Push(bean, f);
                    throw dce;
                }
            }
            return(new DBean(type, implBean, fields));
        }
Beispiel #17
0
        public DType Accept(TBean type, XElement x, DefAssembly ass)
        {
            var bean = (DefBean)type.Bean;

            DefBean implBean;

            if (bean.IsAbstractType)
            {
                string subType = x.Attribute(DefBean.TYPE_NAME_KEY)?.Value;
                if (string.IsNullOrWhiteSpace(subType))
                {
                    throw new Exception($"bean:{bean.FullName}是多态,需要指定{DefBean.TYPE_NAME_KEY}属性.\n xml:{x}");
                }
                var fullName = TypeUtil.MakeFullName(bean.Namespace, subType);
                var defType  = (DefBean)bean.GetNotAbstractChildType(subType);
                if (defType == null)
                {
                    throw new Exception($"type:{fullName} 不是合法类型");
                }
                //if (defType.IsAbstractType)
                //{
                //    throw new Exception($"type:{fullName} 是抽象类. 不能创建实例");
                //}
                implBean = defType;
            }
            else
            {
                implBean = bean;
            }

            var fields = new List <DType>();

            foreach (var field in implBean.HierarchyFields)
            {
                var      feles = x.Elements(field.Name);
                XElement fele  = feles.FirstOrDefault();
                if (fele == null)
                {
                    throw new Exception($"字段:{field.Name} 缺失");
                }

                try
                {
                    fields.Add(field.CType.Apply(this, fele, ass));
                }
                catch (Exception e)
                {
                    throw new Exception($"结构:{implBean.FullName} 字段:{field.Name} 读取失败 => {e.Message}", e);
                }
            }
            return(new DBean(bean, implBean, fields));
        }
Beispiel #18
0
        public override DType ReadOne(TBean type)
        {
            var datas = ReadMulti(type);

            switch (datas.Count)
            {
            case 1: return(datas[0]);

            case 0: throw new Exception($"单例表不能为空,必须包含且只包含1个记录");

            default: throw new Exception($"单例表必须恰好包含1个记录. 但当前记录数为:{datas.Count}");
            }
        }
Beispiel #19
0
        public string Accept(TBean type, string byteBufName, string fieldName)
        {
            var bean = type.Bean;

            if (bean.IsNotAbstractType)
            {
                return($"{byteBufName}.BeginWriteSegment(out var _state2_); {fieldName}.Serialize({byteBufName}); {byteBufName}.EndWriteSegment(_state2_);");
            }
            else
            {
                return($"{byteBufName}.BeginWriteSegment(out var _state2_); {bean.FullName}.Serialize{bean.Name}({byteBufName}, {fieldName});{byteBufName}.EndWriteSegment(_state2_);");
            }
        }
        protected Record ReadRecord(LuaTable table, TBean type)
        {
            string tagName = table.GetValue(TAG_KEY)?.ToString();

            if (DataUtil.IsIgnoreTag(tagName))
            {
                return(null);
            }
            var data = (DBean)type.Apply(LuaDataCreator.Ins, table, (DefAssembly)type.Bean.AssemblyBase);
            var tags = DataUtil.ParseTags(tagName);

            return(new Record(data, RawUrl, tags));
        }
        public string Accept(TBean type, string bufName, string fieldName)
        {
            var bean = type.Bean;

            if (bean.IsNotAbstractType)
            {
                return($"{bufName}.EnterSegment(out var _state2_); {fieldName} = new {type.Apply(DbCsDefineTypeVisitor.Ins)}(); {fieldName}.Deserialize({bufName}); {bufName}.LeaveSegment(_state2_);");
            }
            else
            {
                return($"{bufName}.EnterSegment(out var _state2_); {fieldName} = {bean.FullName}.Deserialize{bean.Name}({bufName}); {bufName}.LeaveSegment(_state2_);");
            }
        }
        public override Record ReadOne(TBean type)
        {
            string tagName = _doc.Element(TAG_KEY)?.Value;

            if (DataUtil.IsIgnoreTag(tagName))
            {
                return(null);
            }
            var data = (DBean)type.Apply(XmlDataCreator.Ins, _doc, (DefAssembly)type.Bean.AssemblyBase);
            var tags = DataUtil.ParseTags(tagName);

            return(new Record(data, RawUrl, tags));
        }
        public string Accept(TBean type, string bufName, string fieldName)
        {
            var bean = type.Bean;

            if (bean.IsNotAbstractType)
            {
                return($"{fieldName}.Serialize({bufName});");
            }
            else
            {
                return($"{bean.FullName}.Serialize{bean.Name}({bufName}, {fieldName});");
            }
        }
        public override List <Record> ReadMulti(TBean type)
        {
            var records = new List <Record>();

            foreach (var ele in _data.EnumerateArray())
            {
                Record rec = ReadRecord(ele, type);
                if (rec != null)
                {
                    records.Add(rec);
                }
            }
            return(records);
        }
        public override List <Record> ReadMulti(TBean type)
        {
            var records = new List <Record>();

            foreach (var ele in (YamlSequenceNode)_root)
            {
                var rec = ReadRecord(ele, type);
                if (rec != null)
                {
                    records.Add(rec);
                }
            }
            return(records);
        }
        public override List <Record> ReadMulti(TBean type)
        {
            var records = new List <Record>();

            foreach (LuaTable t in _dataTable.Values.Values)
            {
                Record r = ReadRecord(t, type);
                if (r != null)
                {
                    records.Add(r);
                }
            }

            return(records);
        }
Beispiel #27
0
        public DType Accept(TBean type, JsonElement x, DefAssembly ass)
        {
            var bean = (DefBean)type.Bean;

            DefBean implBean;

            if (bean.IsAbstractType)
            {
                if (!x.TryGetProperty(DefBean.TYPE_NAME_KEY, out var typeNameProp))
                {
                    throw new Exception($"结构:{bean.FullName} 是多态类型,必须用 {DefBean.TYPE_NAME_KEY} 字段指定 子类名");
                }
                string subType  = typeNameProp.GetString();
                var    fullName = TypeUtil.MakeFullName(bean.Namespace, subType);
                var    defType  = (DefBean)bean.GetNotAbstractChildType(subType);
                //if (defType.IsAbstractType)
                //{
                //    throw new Exception($"type:{fullName} 是抽象类. 不能创建实例");
                //}
                implBean = defType ?? throw new Exception($"type:{fullName} 不是合法类型");
            }
            else
            {
                implBean = bean;
            }

            var fields = new List <DType>();

            foreach (var field in implBean.HierarchyFields)
            {
                if (x.TryGetProperty(field.Name, out var ele))
                {
                    try
                    {
                        fields.Add(field.CType.Apply(this, ele, ass));
                    }
                    catch (Exception e)
                    {
                        throw new Exception($"结构:{implBean.FullName} 字段:{field.Name} 读取失败 => {e.Message}", e);
                    }
                }
                else
                {
                    throw new Exception($"结构:{implBean.FullName} 字段:{field.Name} 缺失");
                }
            }
            return(new DBean(bean, implBean, fields));
        }
Beispiel #28
0
        public override List <DType> ReadMulti(TBean type)
        {
            var datas = new List <DType>();

            foreach (var sheet in _sheets)
            {
                try
                {
                    datas.AddRange(sheet.ReadMulti(type, ((DefBean)type.Bean).IsMultiRow));
                }
                catch (Exception e)
                {
                    throw new Exception($"sheet:{sheet.Name} ==> {e.Message} {e.StackTrace}", e);
                }
            }
            return(datas);
        }
        //public static ExcelStream SepIfNeed(TType type, ExcelStream stream)
        //{
        //    string sep = DataUtil.GetSep(type);
        //    if (!string.IsNullOrEmpty(sep))
        //    {
        //        return new ExcelStream(stream.ReadCell(), sep);
        //    }
        //    else
        //    {
        //        return stream;
        //    }
        //}

        public DType Accept(TBean type, ExcelStream x)
        {
            var originBean = (DefBean)type.Bean;

            if (!string.IsNullOrEmpty(originBean.Sep))
            {
                x = new ExcelStream(x.ReadCell(), originBean.Sep);
            }
            else
            {
                x = TrySep(type, x);
            }

            if (originBean.IsAbstractType)
            {
                string subType = x.Read().ToString();
                if (subType.ToLower().Trim() == DefBean.BEAN_NULL_STR)
                {
                    if (!type.IsNullable)
                    {
                        throw new InvalidExcelDataException($"type:{type.Bean.FullName}不是可空类型. 不能为空");
                    }
                    return(null);
                }
                DefBean implType = DataUtil.GetImplTypeByNameOrAlias(originBean, subType);
                return(new DBean(type, implType, CreateBeanFields(implType, x)));
            }
            else
            {
                if (type.IsNullable)
                {
                    string subType = x.Read().ToString().Trim();
                    if (subType == DefBean.BEAN_NULL_STR)
                    {
                        return(null);
                    }
                    else if (subType != DefBean.BEAN_NOT_NULL_STR && subType != originBean.Name)
                    {
                        throw new Exception($"type:'{type.Bean.FullName}' 可空标识:'{subType}' 不合法(只能为{DefBean.BEAN_NOT_NULL_STR}或{DefBean.BEAN_NULL_STR}或{originBean.Name})");
                    }
                }
                return(new DBean(type, originBean, CreateBeanFields(originBean, x)));
            }
        }
Beispiel #30
0
 public DType ReadOne(TBean type, bool enableMultiRowRecord)
 {
     if (!enableMultiRowRecord)
     {
         List <Cell> row = GetNextRecordRow();
         if (row == null)
         {
             return(null);
         }
         return(ExcelNamedRowDataCreator.Ins.ReadExcel(new NamedRow(_rootTitle, row), type));
     }
     else
     {
         List <List <Cell> > rows = GetNextRecordRows();
         if (rows == null)
         {
             return(null);
         }
         return(ExcelNamedRowDataCreator.Ins.ReadExcel(new NamedRow(_rootTitle, rows), type));
     }
 }