コード例 #1
0
        public ChoYamlRecordConfiguration Map <T, TField>(Expression <Func <T, TField> > field, Action <ChoYamlRecordFieldConfigurationMap> mapper)
        {
            var subType = field.GetReflectedType();
            var fn      = field.GetMemberName();
            var pd      = field.GetPropertyDescriptor();
            var fqm     = field.GetFullyQualifiedMemberName();

            ChoYamlRecordFieldConfiguration cf = GetFieldConfiguration(fn, pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().FirstOrDefault(), pd.Attributes.OfType <Attribute>().ToArray(),
                                                                       pd, fqm, subType == typeof(T) ? null : subType);

            mapper?.Invoke(new ChoYamlRecordFieldConfigurationMap(cf));
            return(this);
        }
コード例 #2
0
ファイル: ChoYamlWriter.cs プロジェクト: evolvencemsm/ChoETL
        public void Write(DataTable dt)
        {
            ChoGuard.ArgumentNotNull(dt, "DataTable");
            if (Configuration.UseYamlSerialization)
            {
                _writer.TraceSwitch = TraceSwitch;
                _writer.WriteTo(_textWriter.Value, dt.ConvertToEnumerable()).Loop();
                return;
            }

            DataTable schemaTable = dt;
            dynamic   expando     = new ExpandoObject();
            var       expandoDic  = (IDictionary <string, object>)expando;

            if (Configuration.YamlRecordFieldConfigurations.IsNullOrEmpty())
            {
                string colName     = null;
                Type   colType     = null;
                int    startIndex  = 0;
                int    fieldLength = 0;
                foreach (DataColumn col in schemaTable.Columns)
                {
                    colName = col.ColumnName;
                    colType = col.DataType;
                    //if (!colType.IsSimple()) continue;

                    var obj = new ChoYamlRecordFieldConfiguration(colName, yamlPath: null);
                    Configuration.YamlRecordFieldConfigurations.Add(obj);
                    startIndex += fieldLength;
                }
            }
            Configuration.RootName = dt.TableName.IsNullOrWhiteSpace() ? null : dt.TableName;

            List <T> list = new List <T>();

            foreach (DataRow row in dt.Rows)
            {
                expandoDic.Clear();

                foreach (var fc in Configuration.YamlRecordFieldConfigurations)
                {
                    expandoDic.Add(fc.Name, row[fc.Name] == DBNull.Value ? null : row[fc.Name]);
                }
                list.Add(expando);
            }

            Write(list.ToArray());
        }
コード例 #3
0
        public ChoYamlReader <T> WithFields(params string[] fieldsNames)
        {
            string fnTrim = null;

            if (!fieldsNames.IsNullOrEmpty())
            {
                PropertyDescriptor pd = null;
                ChoYamlRecordFieldConfiguration fc = null;
                foreach (string fn in fieldsNames)
                {
                    if (fn.IsNullOrEmpty())
                    {
                        continue;
                    }
                    ClearFieldsIf();

                    fnTrim = fn.NTrim();
                    if (Configuration.YamlRecordFieldConfigurations.Any(o => o.Name == fnTrim))
                    {
                        fc = Configuration.YamlRecordFieldConfigurations.Where(o => o.Name == fnTrim).First();
                        Configuration.YamlRecordFieldConfigurations.Remove(Configuration.YamlRecordFieldConfigurations.Where(o => o.Name == fnTrim).First());
                    }
                    else
                    {
                        pd = ChoTypeDescriptor.GetProperty(typeof(T), fn);
                    }

                    var nfc = new ChoYamlRecordFieldConfiguration(fnTrim, (string)null);
                    nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd;
                    nfc.DeclaringMember    = fc != null ? fc.DeclaringMember : null;
                    if (pd != null)
                    {
                        if (nfc.FieldType == null)
                        {
                            nfc.FieldType = pd.PropertyType;
                        }
                    }

                    Configuration.YamlRecordFieldConfigurations.Add(nfc);
                }
            }

            return(this);
        }
コード例 #4
0
        internal ChoYamlRecordFieldConfiguration GetFieldConfiguration(string propertyName, ChoYamlRecordFieldAttribute attr = null, Attribute[] otherAttrs = null,
                                                                       PropertyDescriptor pd = null, string fqm = null, Type subType = null)
        {
            if (subType != null)
            {
                MapRecordFieldsForType(subType);
                var fc = new ChoYamlRecordFieldConfiguration(propertyName, attr, otherAttrs);
                AddFieldForType(subType, fc);

                return(fc);
            }
            else
            {
                if (!YamlRecordFieldConfigurations.Any(fc => fc.Name == propertyName))
                {
                    YamlRecordFieldConfigurations.Add(new ChoYamlRecordFieldConfiguration(propertyName, attr, otherAttrs));
                }

                return(YamlRecordFieldConfigurations.First(fc => fc.Name == propertyName));
            }
        }
コード例 #5
0
        internal void AddFieldForType(Type rt, ChoYamlRecordFieldConfiguration rc)
        {
            if (rt == null || rc == null)
            {
                return;
            }

            if (!YamlRecordFieldConfigurationsForType.ContainsKey(rt))
            {
                YamlRecordFieldConfigurationsForType.Add(rt, new Dictionary <string, ChoYamlRecordFieldConfiguration>(StringComparer.InvariantCultureIgnoreCase));
            }

            if (YamlRecordFieldConfigurationsForType[rt].ContainsKey(rc.Name))
            {
                YamlRecordFieldConfigurationsForType[rt][rc.Name] = rc;
            }
            else
            {
                YamlRecordFieldConfigurationsForType[rt].Add(rc.Name, rc);
            }
        }
コード例 #6
0
        public void Write(DataTable dt)
        {
            ChoGuard.ArgumentNotNull(dt, "DataTable");

            DataTable schemaTable = dt;
            dynamic   expando     = new ExpandoObject();
            var       expandoDic  = (IDictionary <string, object>)expando;

            if (Configuration.YamlRecordFieldConfigurations.IsNullOrEmpty())
            {
                string colName     = null;
                Type   colType     = null;
                int    startIndex  = 0;
                int    fieldLength = 0;
                foreach (DataColumn col in schemaTable.Columns)
                {
                    colName = col.ColumnName;
                    colType = col.DataType;
                    //if (!colType.IsSimple()) continue;

                    var obj = new ChoYamlRecordFieldConfiguration(colName, yamlPath: null);
                    Configuration.YamlRecordFieldConfigurations.Add(obj);
                    startIndex += fieldLength;
                }
            }
            Configuration.RootName = dt.TableName.IsNullOrWhiteSpace() ? null : dt.TableName;

            foreach (DataRow row in dt.Rows)
            {
                expandoDic.Clear();

                foreach (var fc in Configuration.YamlRecordFieldConfigurations)
                {
                    expandoDic.Add(fc.Name, row[fc.Name] == DBNull.Value ? null : row[fc.Name]);
                }

                Write(expando);
            }
        }
コード例 #7
0
        public void Write(IDataReader dr)
        {
            ChoGuard.ArgumentNotNull(dr, "DataReader");

            DataTable schemaTable = dr.GetSchemaTable();
            dynamic   expando     = new ExpandoObject();
            var       expandoDic  = (IDictionary <string, object>)expando;

            //int ordinal = 0;
            if (Configuration.YamlRecordFieldConfigurations.IsNullOrEmpty())
            {
                string colName     = null;
                Type   colType     = null;
                int    startIndex  = 0;
                int    fieldLength = 0;
                foreach (DataRow row in schemaTable.Rows)
                {
                    colName = row["ColumnName"].CastTo <string>();
                    colType = row["DataType"] as Type;
                    //if (!colType.IsSimple()) continue;

                    var obj = new ChoYamlRecordFieldConfiguration(colName, yamlPath: null);
                    Configuration.YamlRecordFieldConfigurations.Add(obj);
                    startIndex += fieldLength;
                }
            }

            while (dr.Read())
            {
                expandoDic.Clear();

                foreach (var fc in Configuration.YamlRecordFieldConfigurations)
                {
                    expandoDic.Add(fc.Name, dr[fc.Name]);
                }

                Write(expando);
            }
        }
コード例 #8
0
ファイル: ChoYamlWriter.cs プロジェクト: evolvencemsm/ChoETL
        private IEnumerable <T> FromDataReader(IDataReader dr)
        {
            DataTable schemaTable = dr.GetSchemaTable();
            dynamic   expando     = new ExpandoObject();
            var       expandoDic  = (IDictionary <string, object>)expando;

            //int ordinal = 0;
            if (Configuration.YamlRecordFieldConfigurations.IsNullOrEmpty())
            {
                string colName     = null;
                Type   colType     = null;
                int    startIndex  = 0;
                int    fieldLength = 0;
                foreach (DataRow row in schemaTable.Rows)
                {
                    colName = row["ColumnName"].CastTo <string>();
                    colType = row["DataType"] as Type;
                    //if (!colType.IsSimple()) continue;

                    var obj = new ChoYamlRecordFieldConfiguration(colName, yamlPath: null);
                    Configuration.YamlRecordFieldConfigurations.Add(obj);
                    startIndex += fieldLength;
                }
            }

            var ordinals = Configuration.YamlRecordFieldConfigurations.ToDictionary(c => c.Name, c => dr.HasColumn(c.Name) ? dr.GetOrdinal(c.Name) : -1);

            while (dr.Read())
            {
                expandoDic.Clear();

                foreach (var fc in ordinals)
                {
                    expandoDic.Add(fc.Key, fc.Value == -1 ? null : dr[fc.Value]);
                }

                yield return(expando);
            }
        }
コード例 #9
0
        internal void WithField(string name, string YamlPath           = null, Type fieldType = null, ChoFieldValueTrimOption fieldValueTrimOption = ChoFieldValueTrimOption.Trim, string fieldName = null, Func <object, object> valueConverter = null,
                                Func <object, object> itemConverter    = null,
                                Func <object, object> customSerializer = null,
                                object defaultValue = null, object fallbackValue = null, string fullyQualifiedMemberName = null,
                                string formatText   = null, bool isArray         = true, string nullValue = null, Type recordType = null,
                                Type subRecordType  = null, Func <IDictionary <string, object>, Type> fieldTypeSelector = null)
        {
            ChoGuard.ArgumentNotNull(recordType, nameof(recordType));

            if (!name.IsNullOrEmpty())
            {
                if (subRecordType != null)
                {
                    MapRecordFieldsForType(subRecordType);
                }

                string fnTrim = name.NTrim();
                ChoYamlRecordFieldConfiguration fc = null;
                PropertyDescriptor pd = null;
                if (YamlRecordFieldConfigurations.Any(o => o.Name == fnTrim))
                {
                    fc = YamlRecordFieldConfigurations.Where(o => o.Name == fnTrim).First();
                    YamlRecordFieldConfigurations.Remove(fc);
                }
                else if (subRecordType != null)
                {
                    pd = ChoTypeDescriptor.GetNestedProperty(subRecordType, fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName);
                }
                else
                {
                    pd = ChoTypeDescriptor.GetNestedProperty(recordType, fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName);
                }

                var nfc = new ChoYamlRecordFieldConfiguration(fnTrim, YamlPath)
                {
                    FieldType            = fieldType,
                    FieldValueTrimOption = fieldValueTrimOption,
                    FieldName            = fieldName.IsNullOrWhiteSpace() ? name : fieldName,
                    ValueConverter       = valueConverter,
                    CustomSerializer     = customSerializer,
                    DefaultValue         = defaultValue,
                    FallbackValue        = fallbackValue,
                    FormatText           = formatText,
                    ItemConverter        = itemConverter,
                    IsArray           = isArray,
                    NullValue         = nullValue,
                    FieldTypeSelector = fieldTypeSelector,
                };
                if (fullyQualifiedMemberName.IsNullOrWhiteSpace())
                {
                    nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd;
                    nfc.DeclaringMember    = fc != null ? fc.DeclaringMember : fullyQualifiedMemberName;
                }
                else
                {
                    if (subRecordType == null)
                    {
                        pd = ChoTypeDescriptor.GetNestedProperty(recordType, fullyQualifiedMemberName);
                    }
                    else
                    {
                        pd = ChoTypeDescriptor.GetNestedProperty(subRecordType, fullyQualifiedMemberName);
                    }

                    nfc.PropertyDescriptor = pd;
                    nfc.DeclaringMember    = fullyQualifiedMemberName;
                }
                if (pd != null)
                {
                    if (nfc.FieldType == null)
                    {
                        nfc.FieldType = pd.PropertyType;
                    }
                }

                if (subRecordType == null)
                {
                    YamlRecordFieldConfigurations.Add(nfc);
                }
                else
                {
                    AddFieldForType(subRecordType, nfc);
                }
            }
        }
コード例 #10
0
        public override void Validate(object state)
        {
            if (RecordType != null)
            {
                Init(RecordType);
            }

            base.Validate(state);

            string[] fieldNames = null;
            IDictionary <string, object> yamlNode = null;

            if (state is Tuple <long, IDictionary <string, object> > )
            {
                yamlNode = ((Tuple <long, IDictionary <string, object> >)state).Item2;
            }
            else
            {
                fieldNames = state as string[];
            }

            if (AutoDiscoverColumns &&
                YamlRecordFieldConfigurations.Count == 0)
            {
                if (RecordType != null && !IsDynamicObject && /*&& RecordType != typeof(ExpandoObject)*/
                    ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()).Any())
                {
                    MapRecordFields(RecordType);
                }
                else if (yamlNode != null)
                {
                    Dictionary <string, ChoYamlRecordFieldConfiguration> dict = new Dictionary <string, ChoYamlRecordFieldConfiguration>(StringComparer.CurrentCultureIgnoreCase);
                    foreach (var entry in yamlNode)
                    {
                        if (!dict.ContainsKey(entry.Key))
                        {
                            dict.Add(entry.Key, new ChoYamlRecordFieldConfiguration(entry.Key, (string)null));
                        }
                        else
                        {
                            throw new ChoRecordConfigurationException("Duplicate field(s) [Name(s): {0}] found.".FormatString(entry.Key));
                        }
                    }

                    foreach (ChoYamlRecordFieldConfiguration obj in dict.Values)
                    {
                        YamlRecordFieldConfigurations.Add(obj);
                    }
                }
                else if (!fieldNames.IsNullOrEmpty())
                {
                    foreach (string fn in fieldNames)
                    {
                        if (IgnoredFields.Contains(fn))
                        {
                            continue;
                        }

                        var obj = new ChoYamlRecordFieldConfiguration(fn, (string)null);
                        YamlRecordFieldConfigurations.Add(obj);
                    }
                }
            }
            else
            {
                foreach (var fc in YamlRecordFieldConfigurations)
                {
                    fc.ComplexYamlPathUsed = !(fc.YamlPath.IsNullOrWhiteSpace() || String.Compare(fc.FieldName, fc.YamlPath, true) == 0);
                }
            }

            if (YamlRecordFieldConfigurations.Count <= 0)
            {
                throw new ChoRecordConfigurationException("No record fields specified.");
            }

            //Validate each record field
            foreach (var fieldConfig in YamlRecordFieldConfigurations)
            {
                fieldConfig.Validate(this);
            }

            //Check field position for duplicate
            string[] dupFields = YamlRecordFieldConfigurations.GroupBy(i => i.Name)
                                 .Where(g => g.Count() > 1)
                                 .Select(g => g.Key).ToArray();

            if (dupFields.Length > 0)
            {
                throw new ChoRecordConfigurationException("Duplicate field(s) [Name(s): {0}] found.".FormatString(String.Join(",", dupFields)));
            }

            PIDict = new Dictionary <string, System.Reflection.PropertyInfo>();
            PDDict = new Dictionary <string, PropertyDescriptor>();
            foreach (var fc in YamlRecordFieldConfigurations)
            {
                var pd1 = fc.DeclaringMember.IsNullOrWhiteSpace() ? ChoTypeDescriptor.GetProperty(RecordType, fc.Name)
                    : ChoTypeDescriptor.GetProperty(RecordType, fc.DeclaringMember);
                if (pd1 != null)
                {
                    fc.PropertyDescriptor = pd1;
                }

                if (fc.PropertyDescriptor == null)
                {
                    fc.PropertyDescriptor = TypeDescriptor.GetProperties(RecordType).AsTypedEnumerable <PropertyDescriptor>().Where(pd => pd.Name == fc.Name).FirstOrDefault();
                }
                if (fc.PropertyDescriptor == null)
                {
                    continue;
                }

                PIDict.Add(fc.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name));
                PDDict.Add(fc.Name, fc.PropertyDescriptor);
            }

            RecordFieldConfigurationsDict = YamlRecordFieldConfigurations.Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name);

            LoadNCacheMembers(YamlRecordFieldConfigurations);
        }
コード例 #11
0
        private Type DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false,
                                          List <ChoYamlRecordFieldConfiguration> recordFieldConfigurations = null, bool isTop = false)
        {
            if (recordType == null)
            {
                return(recordType);
            }
            if (!recordType.IsDynamicType())
            {
                Type pt = null;
                if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()).Any())
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        pt = pd.PropertyType.GetUnderlyingType();
                        bool optIn1 = ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()).Any();
                        if (optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport)
                        {
                            DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1, recordFieldConfigurations, false);
                        }
                        else if (pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any())
                        {
                            var obj = new ChoYamlRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().First(), pd.Attributes.OfType <Attribute>().ToArray());
                            obj.FieldType          = pt;
                            obj.PropertyDescriptor = pd;
                            obj.DeclaringMember    = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name);
                            if (recordFieldConfigurations != null)
                            {
                                if (!recordFieldConfigurations.Any(c => c.Name == pd.Name))
                                {
                                    recordFieldConfigurations.Add(obj);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (isTop)
                    {
                        if (typeof(IList).IsAssignableFrom(recordType))
                        {
                            throw new ChoParserException("Record type not supported.");
                        }
                        else if (typeof(IDictionary <string, object>).IsAssignableFrom(recordType))
                        {
                            recordType = typeof(ExpandoObject);
                            return(recordType);
                        }
                        else if (typeof(IDictionary).IsAssignableFrom(recordType))
                        {
                            recordType = typeof(ExpandoObject);
                            return(recordType);
                        }
                    }

                    if (recordType.IsSimple())
                    {
                        var obj = new ChoYamlRecordFieldConfiguration("Value", "$.Value");
                        obj.FieldType = recordType;

                        recordFieldConfigurations.Add(obj);
                        return(recordType);
                    }

                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        ChoYamlIgnoreAttribute jiAttr = pd.Attributes.OfType <ChoYamlIgnoreAttribute>().FirstOrDefault();
                        if (jiAttr != null)
                        {
                            continue;
                        }

                        pt = pd.PropertyType.GetUnderlyingType();
                        if (pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport)
                        {
                            DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn, recordFieldConfigurations, false);
                        }
                        else
                        {
                            var obj = new ChoYamlRecordFieldConfiguration(pd.Name, (string)null);
                            obj.FieldType          = pt;
                            obj.PropertyDescriptor = pd;
                            obj.DeclaringMember    = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name);
                            StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault();
                            if (slAttr != null && slAttr.MaximumLength > 0)
                            {
                                obj.Size = slAttr.MaximumLength;
                            }
                            //ChoUseYamlSerializationAttribute sAttr = pd.Attributes.OfType<ChoUseYamlSerializationAttribute>().FirstOrDefault();
                            //if (sAttr != null)
                            //    obj.UseYamlSerialization = sAttr.Flag;
                            ChoYamlPathAttribute jpAttr = pd.Attributes.OfType <ChoYamlPathAttribute>().FirstOrDefault();
                            if (jpAttr != null)
                            {
                                obj.YamlPath = jpAttr.YamlPath;
                            }

                            ChoYamPropertyAttribute jAttr = pd.Attributes.OfType <ChoYamPropertyAttribute>().FirstOrDefault();
                            if (jAttr != null && !jAttr.PropertyName.IsNullOrWhiteSpace())
                            {
                                obj.FieldName = jAttr.PropertyName;
                            }
                            else
                            {
                                DisplayNameAttribute dnAttr = pd.Attributes.OfType <DisplayNameAttribute>().FirstOrDefault();
                                if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace())
                                {
                                    obj.FieldName = dnAttr.DisplayName.Trim();
                                }
                                else
                                {
                                    DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault();
                                    if (dpAttr != null)
                                    {
                                        if (!dpAttr.ShortName.IsNullOrWhiteSpace())
                                        {
                                            obj.FieldName = dpAttr.ShortName;
                                        }
                                        else if (!dpAttr.Name.IsNullOrWhiteSpace())
                                        {
                                            obj.FieldName = dpAttr.Name;
                                        }

                                        obj.Order = dpAttr.Order;
                                    }
                                    else
                                    {
                                        ColumnAttribute clAttr = pd.Attributes.OfType <ColumnAttribute>().FirstOrDefault();
                                        if (clAttr != null)
                                        {
                                            obj.Order = clAttr.Order;
                                            if (!clAttr.Name.IsNullOrWhiteSpace())
                                            {
                                                obj.FieldName = clAttr.Name;
                                            }
                                        }
                                    }
                                }
                            }
                            DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault();
                            if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace())
                            {
                                obj.FormatText = dfAttr.DataFormatString;
                            }
                            if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace())
                            {
                                obj.NullValue = dfAttr.NullDisplayText;
                            }

                            if (recordFieldConfigurations != null)
                            {
                                if (!recordFieldConfigurations.Any(c => c.Name == pd.Name))
                                {
                                    recordFieldConfigurations.Add(obj);
                                }
                            }
                        }
                    }
                }
            }
            return(recordType);
        }
コード例 #12
0
        private bool ToText(long index, object rec, out string recText)
        {
            if (typeof(IChoScalarObject).IsAssignableFrom(Configuration.RecordType))
            {
                rec = ChoActivator.CreateInstance(Configuration.RecordType, rec);
            }

            if (!Configuration.IsDynamicObject)
            {
                if (rec.ToTextIfCustomSerialization(out recText))
                {
                    return(true);
                }

                //Check if KVP object
                if (rec.GetType().IsKeyValueType())
                {
                    recText = SerializeObject(rec);
                    return(true);
                }
            }

            recText = null;
            if (Configuration.UseYamlSerialization)
            {
                recText = new Serializer().Serialize(rec, rec.GetType());
                return(true);
            }
            StringBuilder msg        = new StringBuilder();
            object        fieldValue = null;
            string        fieldText  = null;
            ChoYamlRecordFieldConfiguration fieldConfig = null;
            string fieldName = null;

            if (Configuration.ColumnCountStrict)
            {
                CheckColumnsStrict(rec);
            }

            //bool firstColumn = true;
            PropertyInfo pi      = null;
            bool         isFirst = true;
            object       rootRec = rec;

            Dictionary <string, object> output = new Dictionary <string, object>();

            foreach (KeyValuePair <string, ChoYamlRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict.OrderBy(kvp => kvp.Value.Order))
            {
                //if (Configuration.IsDynamicObject)
                //{
                if (Configuration.IgnoredFields.Contains(kvp.Key))
                {
                    continue;
                }
                //}

                fieldConfig = kvp.Value;
                fieldName   = fieldConfig.FieldName;
                fieldValue  = null;
                fieldText   = String.Empty;
                if (Configuration.PIDict != null)
                {
                    Configuration.PIDict.TryGetValue(kvp.Key, out pi);
                }
                rec = GetDeclaringRecord(kvp.Value.DeclaringMember, rootRec);

                if (Configuration.ThrowAndStopOnMissingField)
                {
                    if (Configuration.IsDynamicObject)
                    {
                        var dict = rec.ToDynamicObject() as IDictionary <string, Object>;
                        if (!dict.ContainsKey(kvp.Key))
                        {
                            throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' Yaml node.".FormatString(fieldConfig.FieldName));
                        }
                    }
                    else
                    {
                        if (pi == null)
                        {
                            if (!RecordType.IsSimple())
                            {
                                throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' Yaml node.".FormatString(fieldConfig.FieldName));
                            }
                        }
                    }
                }

                try
                {
                    if (Configuration.IsDynamicObject)
                    {
                        IDictionary <string, Object> dict = rec.ToDynamicObject() as IDictionary <string, Object>;
                        fieldValue = dict[kvp.Key]; // dict.GetValue(kvp.Key, Configuration.FileHeaderConfiguration.IgnoreCase, Configuration.Culture);
                        if (rec is ChoDynamicObject)
                        {
                        }

                        if (kvp.Value.FieldType == null)
                        {
                            if (rec is ChoDynamicObject)
                            {
                                var dobj = rec as ChoDynamicObject;
                                kvp.Value.FieldType = dobj.GetMemberType(kvp.Key);
                            }
                            if (kvp.Value.FieldType == null)
                            {
                                if (ElementType == null)
                                {
                                    kvp.Value.FieldType = typeof(object);
                                }
                                else
                                {
                                    kvp.Value.FieldType = ElementType;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (pi != null)
                        {
                            fieldValue = ChoType.GetPropertyValue(rec, pi);
                            if (kvp.Value.FieldType == null)
                            {
                                kvp.Value.FieldType = pi.PropertyType;
                            }
                        }
                        else
                        {
                            kvp.Value.FieldType = typeof(string);
                        }
                    }

                    //Discover default value, use it if null
                    //if (fieldValue == null)
                    //{
                    //    if (fieldConfig.IsDefaultValueSpecified)
                    //        fieldValue = fieldConfig.DefaultValue;
                    //}
                    bool ignoreFieldValue = fieldValue.IgnoreFieldValue(fieldConfig.IgnoreFieldValueMode);
                    if (ignoreFieldValue)
                    {
                        fieldValue = fieldConfig.IsDefaultValueSpecified ? fieldConfig.DefaultValue : null;
                    }

                    if (!RaiseBeforeRecordFieldWrite(rec, index, kvp.Key, ref fieldValue))
                    {
                        return(false);
                    }

                    if (fieldConfig.ValueConverter != null)
                    {
                        fieldValue = fieldConfig.ValueConverter(fieldValue);
                    }
                    else if (RecordType.IsSimple())
                    {
                        fieldValue = new List <object> {
                            rec
                        }
                    }
                    ;
                    else
                    {
                        rec.GetNConvertMemberValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue, true);
                    }

                    if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.MemberLevel)
                    {
                        rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue);
                    }

                    if (!RaiseAfterRecordFieldWrite(rec, index, kvp.Key, fieldValue))
                    {
                        return(false);
                    }
                }
                catch (ChoParserException)
                {
                    throw;
                }
                catch (ChoMissingRecordFieldException)
                {
                    if (Configuration.ThrowAndStopOnMissingField)
                    {
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    ChoETLFramework.HandleException(ref ex);

                    if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop)
                    {
                        throw;
                    }

                    try
                    {
                        if (Configuration.IsDynamicObject)
                        {
                            var dict = rec.ToDynamicObject() as IDictionary <string, Object>;

                            if (dict.GetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue))
                            {
                                dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue);
                            }
                            else if (dict.GetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue))
                            {
                                dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue);
                            }
                            else
                            {
                                var ex1 = new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex);
                                fieldValue = null;
                                throw ex1;
                            }
                        }
                        else if (pi != null)
                        {
                            if (rec.GetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue))
                            {
                                rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode);
                            }
                            else if (rec.GetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue))
                            {
                                rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue);
                            }
                            else
                            {
                                var ex1 = new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex);
                                fieldValue = null;
                                throw ex1;
                            }
                        }
                        else
                        {
                            var ex1 = new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex);
                            fieldValue = null;
                            throw ex1;
                        }
                    }
                    catch (Exception innerEx)
                    {
                        if (ex == innerEx.InnerException)
                        {
                            if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                            {
                                continue;
                            }
                            else
                            {
                                if (!RaiseRecordFieldWriteError(rec, index, kvp.Key, ref fieldValue, ex))
                                {
                                    throw new ChoWriterException($"Failed to write '{fieldValue}' value of '{kvp.Key}' member.", ex);
                                }
                            }
                        }
                        else
                        {
                            throw new ChoWriterException("Failed to use '{0}' fallback value for '{1}' member.".FormatString(fieldValue, kvp.Key), innerEx);
                        }
                    }
                }

                bool isSimple = true;

                if (fieldConfig.CustomSerializer != null)
                {
                    fieldText = fieldConfig.CustomSerializer(fieldValue) as string;
                }
                else if (RaiseRecordFieldSerialize(rec, index, kvp.Key, ref fieldValue))
                {
                    fieldText = fieldValue as string;
                }
                else if (fieldConfig.PropCustomSerializer != null)
                {
                    fieldText = ChoCustomSerializer.Serialize(fieldValue, typeof(string), fieldConfig.PropCustomSerializer, fieldConfig.PropCustomSerializerParams, Configuration.Culture, fieldConfig.Name) as string;
                }
                else
                {
                    Type ft = fieldValue == null ? typeof(object) : fieldValue.GetType();

                    //if (fieldConfig.IgnoreFieldValue(fieldValue))
                    //    fieldText = null;

                    bool ignoreFieldValue = fieldValue.IgnoreFieldValue(fieldConfig.IgnoreFieldValueMode);
                    if (ignoreFieldValue)
                    {
                        fieldText = null;
                    }
                    else if (fieldValue == null)
                    {
                        //if (fieldConfig.FieldType == null || fieldConfig.FieldType == typeof(object))
                        //{
                        //    if (fieldConfig.NullValue == null)
                        //        fieldText = !fieldConfig.IsArray ? "null" : "[]";
                        //    else
                        //        fieldText = fieldConfig.NullValue;
                        //}
                        if (Configuration.NullValueHandling == ChoNullValueHandling.Ignore)
                        {
                            fieldText = null;
                        }
                        else if (Configuration.NullValueHandling == ChoNullValueHandling.Default)
                        {
                            fieldText = ChoActivator.CreateInstance(fieldConfig.FieldType).ToNString();
                        }
                        else if (Configuration.NullValueHandling == ChoNullValueHandling.Empty && fieldConfig.FieldType == typeof(string))
                        {
                            fieldText = String.Empty;
                        }
                        else
                        {
                            if (fieldConfig.NullValue == null)
                            {
                            }
                            else
                            {
                                fieldText = fieldConfig.NullValue;
                            }
                        }
                    }
                    else if (ft == typeof(string) || ft == typeof(char))
                    {
                        fieldText = NormalizeFieldValue(kvp.Key, fieldValue.ToString(), kvp.Value.Size, kvp.Value.Truncate, false, GetFieldValueJustification(kvp.Value.FieldValueJustification, kvp.Value.FieldType),
                                                        GetFillChar(kvp.Value.FillChar, kvp.Value.FieldType), false, kvp.Value.GetFieldValueTrimOption(kvp.Value.FieldType, Configuration.FieldValueTrimOption));
                    }
                    else if (ft == typeof(DateTime) || ft == typeof(TimeSpan))
                    {
                        fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string;
                    }
                    else if (ft.IsEnum)
                    {
                        fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string;
                    }
                    else if (ft == typeof(ChoCurrency))
                    {
                        fieldText = fieldValue.ToString();
                    }
                    else if (ft == typeof(bool))
                    {
                        fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string;
                    }
                    else if (ft.IsNumeric())
                    {
                        fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string;
                    }
                    else
                    {
                        isSimple = false;
                    }
                }

                object objValue = null;
                if (isSimple)
                {
                    objValue = fieldText;
                }
                else
                {
                    Writer.ContractResolverState = new ChoContractResolverState
                    {
                        Name        = kvp.Key,
                        Index       = index,
                        Record      = rec,
                        FieldConfig = kvp.Value
                    };
                    var json = JsonConvert.SerializeObject(fieldValue, Configuration.JsonSerializerSettings);

                    if (RecordType.IsSimple())
                    {
                        objValue = JsonConvert.DeserializeObject <IList <object> >(json);
                    }
                    else
                    {
                        objValue = JsonConvert.DeserializeObject <IDictionary <string, object> >(json);
                    }
                }

                if (!RecordType.IsSimple())
                {
                    output.Add(fieldName, objValue);
                }
                else
                {
                    fieldValue = objValue;
                }
            }

            if (!RecordType.IsSimple())
            {
                recText = Configuration.YamlSerializer.Serialize(output, output.GetType());
            }
            else
            {
                recText = Configuration.YamlSerializer.Serialize(fieldValue, fieldValue.GetType());
            }

            return(true);
        }
コード例 #13
0
 internal ChoYamlRecordFieldConfigurationMap(ChoYamlRecordFieldConfiguration config)
 {
     _config = config;
 }