Beispiel #1
0
        //protected Type DiscoverFieldType(string value, ChoFileRecordConfiguration config)
        //{
        //    bool treatCurrencyAsDecimal = config.TreatCurrencyAsDecimal;
        //    long lresult = 0;
        //    double dresult = 0;
        //    DateTime dtresult;
        //    Decimal decResult = 0;
        //    ChoCurrency currResult = 0;
        //    Guid guidResult;

        //    if (value == null)
        //        return typeof(string);
        //    else if (long.TryParse(value, out lresult))
        //        return typeof(long);
        //    else if (double.TryParse(value, out dresult))
        //        return typeof(double);
        //    else if (RecordConfiguration.TypeConverterFormatSpec.DateTimeFormat.IsNullOrWhiteSpace()
        //        && ChoDateTime.TryParseExact(value, RecordConfiguration.TypeConverterFormatSpec.DateTimeFormat, CultureInfo.CurrentCulture, out dtResult))
        //        return typeof(DateTime);
        //    else if (DateTime.TryParse(value, out dtresult))
        //        return typeof(DateTime);
        //    else if (Guid.TryParse(value, out guidResult))
        //        return typeof(Guid);
        //    else if (!treatCurrencyAsDecimal && ChoCurrency.TryParse(value, out currResult))
        //        return typeof(ChoCurrency);
        //    else if (treatCurrencyAsDecimal && Decimal.TryParse(value, NumberStyles.Currency, CultureInfo.CurrentCulture, out decResult))
        //        return typeof(Decimal);
        //    else
        //        return typeof(string);
        //}

        protected object GetDeclaringRecord(string declaringMember, object rec, ChoFileRecordFieldConfiguration config = null)
        {
            if (rec == null)
            {
                return(null);
            }

            var obj = ChoType.GetDeclaringRecord(declaringMember, rec);

            if (obj == null)
            {
                return(null);
            }

            Type recordType = obj.GetType();

            if (config != null)
            {
                if (config.ArrayIndex != null &&
                    config.ArrayIndex.Value >= 0 &&
                    obj is IEnumerable &&
                    !(obj is ArrayList))
                {
                    var item = Enumerable.Skip(((IEnumerable)obj).Cast <object>(), config.ArrayIndex.Value).FirstOrDefault();

                    if (item == null)
                    {
                        Type itemType = obj.GetType().GetItemType();
                        item = ChoActivator.CreateInstance(itemType);
                    }

                    if (obj is Array)
                    {
                        if (config.ArrayIndex.Value < ((Array)obj).Length)
                        {
                            ((Array)obj).SetValue(item, config.ArrayIndex.Value);
                        }
                    }
                    else if (obj is IList)
                    {
                        ((IList)obj).Add(item);
                    }

                    return(item);
                }
            }

            return(obj);
        }
Beispiel #2
0
        /// <summary>
        /// Advances the <see cref="T:System.Data.IDataReader"/> to the next record.
        /// </summary>
        /// <returns>
        /// true if there are more rows; otherwise, false.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override bool Read()
        {
            bool returnValue = false;

            if (_isDeferred)
            {
                returnValue = _firstElementExists;
                _isDeferred = false;
            }
            else
            {
                returnValue = _enumerator.MoveNext();
            }
            _current = returnValue ? _enumerator.Current : _type.IsValueType ? ChoActivator.CreateInstance(_type) : null;
            return(returnValue);
        }
Beispiel #3
0
        protected virtual object Serialize(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            IList result = ChoActivator.CreateInstance(typeof(IList <>).MakeGenericType(targetType)) as IList;

            if (value != null && !value.GetType().IsCollectionType())
            {
                if (targetType == typeof(object) || targetType.IsSimple())
                {
                    foreach (var p in ChoTypeDescriptor.GetProperties(value.GetType()).Where(pd => ChoTypeDescriptor.GetPropetyAttribute <ChoIgnoreMemberAttribute>(pd) == null))
                    {
                        result.Add(ChoConvert.ConvertTo(ChoType.GetPropertyValue(value, p.Name), targetType, culture));
                    }
                }
            }

            return(result.OfType <object>().ToArray());
        }
        protected virtual void Init(Type recordType)
        {
            if (recordType == null)
            {
                return;
            }

            _recObject = new Lazy <object>(() => ChoActivator.CreateInstance(RecordType));
            ChoRecordObjectAttribute recObjAttr = ChoType.GetAttribute <ChoRecordObjectAttribute>(recordType);

            if (recObjAttr != null)
            {
                ErrorMode                  = recObjAttr.ErrorMode;
                IgnoreFieldValueMode       = recObjAttr.IgnoreFieldValueMode;
                ThrowAndStopOnMissingField = recObjAttr.ThrowAndStopOnMissingField;
                ObjectValidationMode       = recObjAttr.ObjectValidationMode;
            }
        }
Beispiel #5
0
        public static object ConvertToObject(this object source, Type type)
        {
            if (source == null)
            {
                return(source);
            }

            if (source is IDictionary <string, object> )
            {
                return(((IDictionary <string, object>)source).ToObject(type));
            }
            else
            {
                Type   sourceType = source.GetType();
                object target     = ChoActivator.CreateInstance(type);
                string key        = null;
                object value      = null;

                foreach (var p in ChoType.GetProperties(type))
                {
                    if (p.GetCustomAttribute <ChoIgnoreMemberAttribute>() != null)
                    {
                        continue;
                    }

                    key = p.Name;
                    var attr = p.GetCustomAttribute <ChoPropertyAttribute>();
                    if (attr != null && !attr.Name.IsNullOrWhiteSpace())
                    {
                        key = attr.Name.NTrim();
                    }

                    if (!ChoType.HasProperty(sourceType, key))
                    {
                        continue;
                    }
                    value = ChoType.GetPropertyValue(source, key);

                    p.SetValue(target, value.CastObjectTo(p.PropertyType));
                }

                return(target);
            }
        }
Beispiel #6
0
        protected virtual void Init(Type recordType)
        {
            if (recordType == null)
            {
                return;
            }

            var tc = recordType.GetCustomAttribute(typeof(ChoTypeConverterAttribute)) as ChoTypeConverterAttribute;

            if (tc != null)
            {
                var c = tc.CreateInstance();
                if (c is IChoValueConverter)
                {
                    ChoTypeConverter.Global.Add(recordType, c as IChoValueConverter);
                }
                else if (c is TypeConverter)
                {
                    ChoTypeConverter.Global.Add(recordType, c as TypeConverter);
                }
#if !NETSTANDARD2_0
                else if (c is IValueConverter)
                {
                    ChoTypeConverter.Global.Add(recordType, c as IValueConverter);
                }
#endif
            }

            var st = recordType.GetCustomAttribute(typeof(ChoSourceTypeAttribute)) as ChoSourceTypeAttribute;
            if (st != null)
            {
                SourceType = st.Type;
            }

            _recObject = new Lazy <object>(() => ChoActivator.CreateInstance(RecordType));
            ChoRecordObjectAttribute recObjAttr = ChoType.GetAttribute <ChoRecordObjectAttribute>(recordType);
            if (recObjAttr != null)
            {
                ErrorMode                  = recObjAttr.ErrorMode;
                IgnoreFieldValueMode       = recObjAttr.IgnoreFieldValueMode;
                ThrowAndStopOnMissingField = recObjAttr.ThrowAndStopOnMissingField;
                ObjectValidationMode       = recObjAttr.ObjectValidationMode;
            }
        }
        private List <JsonConverter> GetJSONConverters()
        {
            List <JsonConverter> converters = new List <JsonConverter>();

            converters.Add(new ExpandoObjectConverter());

            foreach (var kvp in NodeConvertersForType)
            {
                if (kvp.Value == null)
                {
                    continue;
                }
                converters.Add(ChoActivator.CreateInstance(typeof(ChoJSONNodeConverter <>).MakeGenericType(kvp.Key), kvp.Value) as JsonConverter);

                ChoTypeConverter.Global.Add(kvp.Key, ChoActivator.CreateInstance(typeof(ChoJSONTypeConverter <>).MakeGenericType(kvp.Key), kvp.Value) as IChoValueConverter);
            }

            return(converters);
        }
Beispiel #8
0
        protected void InitializeRecordConfiguration(ChoRecordConfiguration configuration)
        {
            if (configuration == null || configuration.IsDynamicObject || configuration.RecordType == null)
            {
                return;
            }

            if (!typeof(IChoNotifyRecordConfigurable).IsAssignableFrom(configuration.RecordMapType))
            {
                return;
            }

            var obj = ChoActivator.CreateInstance(configuration.RecordMapType) as IChoNotifyRecordConfigurable;

            if (obj != null)
            {
                obj.RecondConfigure(configuration);
            }
        }
Beispiel #9
0
        public static object CreateMoqInstance(Type objType, ChoIniFile iniFile = null)
        {
            //if (typeof(ChoRecord).IsAssignableFrom(objType))
            //    return CreateDynamicMoqInstance(objType, iniFile);

            object obj = ChoActivator.CreateInstanceAndInit(objType);

            foreach (KeyValuePair <MemberInfo, Attribute> kv in ChoUtility.DiscoverMembers(objType, typeof(ChoRandomAttribute)))
            {
                if (!(kv.Value is ChoRandomAttribute))
                {
                    continue;
                }

                ChoType.SetMemberValue(obj, kv.Key, ((ChoRandomAttribute)kv.Value).NextValue());
            }

            return(obj);
        }
Beispiel #10
0
        public virtual object CreateInstance()
        {
            if (ConverterType == null)
            {
                return(null);
            }

            //if (ChoGuard.IsArgumentNotNullOrEmpty(Parameters) && ChoType.HasConstructor(ConverterType, ParametersArray1))
            //    return ChoType.CreateInstance(ConverterType, ParametersArray1);
            //else if (ChoType.HasConstructor(ConverterType, new object[] { String.Empty }))
            //    return ChoType.CreateInstance(ConverterType, new object[] { ParametersArray1 != null && ParametersArray1.Length > 0 ? ParametersArray1[0] : String.Empty });
            //else
            if ((ParametersArray == null || ParametersArray.Length == 0) && (ParametersDict == null || ParametersDict.Count == 0))
            {
                return(ChoActivator.CreateInstance(ConverterType));
            }
            else
            {
                if (ParametersDict != null || ParametersDict.Count > 0)
                {
                    try
                    {
                        return(ChoActivator.CreateInstance(ConverterType, ParametersDict));
                    }
                    catch
                    {
                        return(ChoActivator.CreateInstance(ConverterType));
                    }
                }
                else
                {
                    try
                    {
                        return(ChoActivator.CreateInstance(ConverterType, ParametersArray));
                    }
                    catch
                    {
                        return(ChoActivator.CreateInstance(ConverterType));
                    }
                }
            }
        }
Beispiel #11
0
        public virtual object CreateInstance()
        {
            if (ConverterType == null)
            {
                return(null);
            }

            if (ChoGuard.IsArgumentNotNullOrEmpty(Parameters) && ChoType.HasConstructor(ConverterType, Parameters))
            {
                return(ChoType.CreateInstance(ConverterType, Parameters));
            }
            else if (ChoType.HasConstructor(ConverterType, new object[] { String.Empty }))
            {
                return(ChoType.CreateInstance(ConverterType, new object[] { Parameters != null && Parameters.Length > 0 ? Parameters[0] : String.Empty }));
            }
            else
            {
                return(ChoActivator.CreateInstance(ConverterType));
            }
        }
Beispiel #12
0
        public void ScanAndLoad()
        {
            var types = ChoType.GetAllTypes().Where(t => typeof(IComparer).IsAssignableFrom(t)).ToArray();

            foreach (Type compType in types)
            {
                try
                {
                    var comp = ChoActivator.CreateInstance(compType) as IComparer;
                    if (comp != null)
                    {
                        if (compType.IsGenericType)
                        {
                            Type type = comp.GetType().GetGenericArguments()[0];
                            Add(type, comp);
                        }
                        else
                        {
                            Type objType = compType.GetAllInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IComparer <>))
                                           .Select(i => i.GetGenericArguments()[0]).FirstOrDefault();

                            if (objType != null)
                            {
                                Add(objType, comp);
                            }
                            else
                            {
                                var attr = compType.GetCustomAttribute <ChoComparerObjectTypeAttribute>();
                                if (attr != null && attr.Type != null)
                                {
                                    Add(attr.Type, comp);
                                }
                            }
                        }
                    }
                }
                catch
                {
                }
            }
        }
Beispiel #13
0
        public object GetMetadataObject(object @this)
        {
            if (@this == null)
            {
                return(@this);
            }

            Type type = @this.GetType();

            if (_objectCache.ContainsKey(type))
            {
                return(_objectCache[type] != null ? _objectCache[type] : @this);
            }

            MetadataTypeAttribute attr = type.GetCustomAttribute <MetadataTypeAttribute>();

            if (attr == null || attr.MetadataClassType == null)
            {
                return(@this);
            }
            else
            {
                lock (_padLock)
                {
                    if (!_objectCache.ContainsKey(type))
                    {
                        object obj = null;

                        try
                        {
                            obj = ChoActivator.CreateInstanceAndInit(attr.MetadataClassType);
                        }
                        catch { }

                        _objectCache.Add(type, obj);
                    }

                    return(_objectCache[type] != null ? _objectCache[type] : @this);
                }
            }
        }
Beispiel #14
0
        public static object ChangeType <T>(object value, Type conversionType)
            where T : Attribute
        {
            if (value == null)
            {
                return(ChoActivator.CreateInstanceAndInit(conversionType));
            }

            if (conversionType.IsAssignableFrom(value.GetType()))
            {
                return(value);
            }

            object dest = null;

            if (value is IConvertible)
            {
                dest = Convert.ChangeType(value, conversionType);
            }
            else
            {
                dest = ChoActivator.CreateInstanceAndInit(conversionType);
                value.CloneTo <T>(dest);
            }

            if (dest != null && !dest.GetType().IsSimple())
            {
                ChoObjectValidationMode m = GetValidationMode(value);
                if (m == ChoObjectValidationMode.MemberLevel)
                {
                    ChoValidator.Validate(dest);
                }
                else if (m == ChoObjectValidationMode.ObjectLevel)
                {
                    ChoValidator.Validate(dest);
                }
            }
            return(dest);
        }
Beispiel #15
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (typeof(IList).IsAssignableFrom(objectType))
            {
                Type itemType = objectType.GetItemType();
                var  arr      = JArray.Load(reader);


                IList result = ChoActivator.CreateInstance(typeof(IList <>).MakeGenericType(itemType)) as IList;
                foreach (var jo in arr.OfType <JObject>())
                {
                    result.Add(jo.ToObjectEx(ResolveType(jo, itemType), serializer));
                }

                return(typeof(Array).IsAssignableFrom(objectType) ? result.ConvertToArray() : result);
            }
            else
            {
                var jo      = JObject.Load(reader);
                var newType = ResolveType(jo, objectType);
                return(jo.ToObjectEx(newType, serializer));
            }
        }
        internal ChoJSONRecordConfiguration(Type recordType) : base(recordType)
        {
            _JsonSerializer = new Lazy <JsonSerializer>(() =>
            {
                return(JsonSerializerSettings == null ? null : JsonSerializer.Create(JsonSerializerSettings));
            });
            _JSONConverters = new Lazy <List <JsonConverter> >(() =>
            {
                List <JsonConverter> converters = new List <JsonConverter>();
                converters.Add(new ExpandoObjectConverter());
                converters.Add(ChoDynamicObjectConverter.Instance);

                foreach (var kvp in NodeConvertersForType)
                {
                    if (kvp.Value == null)
                    {
                        continue;
                    }
                    converters.Add(ChoActivator.CreateInstance(typeof(ChoJSONNodeConverter <>).MakeGenericType(kvp.Key), kvp.Value) as JsonConverter);

                    ChoTypeConverter.Global.Add(kvp.Key, ChoActivator.CreateInstance(typeof(ChoJSONTypeConverter <>).MakeGenericType(kvp.Key), kvp.Value) as IChoValueConverter);
                }

                return(converters);
            });

            DefaultArrayHandling          = recordType == null || recordType.IsDynamicType() ? true : false;
            JSONRecordFieldConfigurations = new List <ChoJSONRecordFieldConfiguration>();
            WithJSONConverter(ChoDynamicObjectConverter.Instance);

            LineBreakChars = Environment.NewLine;
            Formatting     = Newtonsoft.Json.Formatting.Indented;
            if (recordType != null)
            {
                Init(recordType);
            }
        }
Beispiel #17
0
        public override object ReadJson(JsonReader reader, Type objectType,
                                        object existingValue, JsonSerializer serializer)
        {
            var dict = serializer.Deserialize <Dictionary <string, string> >(reader);
            var item = dict.First();

            var rec = ChoActivator.CreateInstance(objectType);

            if (typeof(IChoKeyValueType).IsAssignableFrom(objectType))
            {
                IChoKeyValueType kvp = rec as IChoKeyValueType;
                kvp.Key   = item.Key;
                kvp.Value = item.Value;
            }
            else
            {
                var kP = ChoTypeDescriptor.GetProperties <ChoKeyAttribute>(objectType).FirstOrDefault();
                var vP = ChoTypeDescriptor.GetProperties <ChoValueAttribute>(objectType).FirstOrDefault();

                ChoType.SetPropertyValue(rec, kP.Name, item.Key);
                ChoType.SetPropertyValue(rec, vP.Name, item.Value);
            }
            return(rec);
        }
        public override IEnumerable <object> AsEnumerable(object source, Func <object, bool?> filterFunc = null)
        {
            StreamReader sr = source as StreamReader;

            ChoGuard.ArgumentNotNull(sr, "StreamReader");

            sr.Seek(0, SeekOrigin.Begin);

            if (!RaiseBeginLoad(sr))
            {
                yield break;
            }

            string[] commentTokens = Configuration.Comments;

            using (ChoPeekEnumerator <Tuple <int, string> > e = new ChoPeekEnumerator <Tuple <int, string> >(
                       new ChoIndexedEnumerator <string>(sr.ReadLines(Configuration.EOLDelimiter, Configuration.QuoteChar)).ToEnumerable(),
                       (pair) =>
            {
                //bool isStateAvail = IsStateAvail();

                bool?skip = false;

                //if (isStateAvail)
                //{
                //    if (!IsStateMatches(item))
                //    {
                //        skip = filterFunc != null ? filterFunc(item) : false;
                //    }
                //    else
                //        skip = true;
                //}
                //else
                //    skip = filterFunc != null ? filterFunc(item) : false;

                if (skip == null)
                {
                    return(null);
                }

                //if (!(sr.BaseStream is MemoryStream))
                //{
                ChoETLFramework.WriteLog(Environment.NewLine);

                if (!skip.Value)
                {
                    ChoETLFramework.WriteLog("Loading line [{0}]...".FormatString(pair.Item1));
                }
                else
                {
                    ChoETLFramework.WriteLog("Skipping line [{0}]...".FormatString(pair.Item1));
                }
                //}

                if (skip.Value)
                {
                    return(skip);
                }

                //if (!(sr.BaseStream is MemoryStream))
                //    ChoETLFramework.WriteLog(ChoETLFramework.Switch.TraceVerbose, "Loading line [{0}]...".FormatString(item.Item1));

                //if (Task != null)
                //    return !IsStateNOTExistsOrNOTMatch(item);

                if (pair.Item2.IsNullOrWhiteSpace())
                {
                    if (!Configuration.IgnoreEmptyLine)
                    {
                        throw new ChoParserException("Empty line found at {0} location.".FormatString(e.Peek.Item1));
                    }
                    else
                    {
                        ChoETLFramework.WriteLog("Empty line found at [{0}]...".FormatString(pair.Item1));
                        return(true);
                    }
                }

                //LoadExcelSeparator if any
                if (pair.Item1 == 1 &&
                    !_excelSeparatorFound)
                {
                    ChoETLFramework.WriteLog("Inspecting for excel separator at [{0}]...".FormatString(pair.Item1));
                    bool retVal = LoadExcelSeperatorIfAny(pair);
                    _excelSeparatorFound = true;

                    if (Configuration.HasExcelSeparator.HasValue &&
                        Configuration.HasExcelSeparator.Value &&
                        !retVal)
                    {
                        throw new ChoParserException("Missing excel separator header line in the file.");
                    }

                    if (retVal)
                    {
                        return(true);
                    }
                }

                if (commentTokens == null)
                {
                    return(false);
                }
                else
                {
                    var x = (from comment in commentTokens
                             where !pair.Item2.IsNull() && pair.Item2.StartsWith(comment, true, Configuration.Culture)
                             select comment).FirstOrDefault();
                    if (x != null)
                    {
                        ChoETLFramework.WriteLog("Comment line found at [{0}]...".FormatString(pair.Item1));
                        return(true);
                    }
                }

                //LoadHeader if any
                if (Configuration.CSVFileHeaderConfiguration.HasHeaderRecord &&
                    !_headerFound)
                {
                    ChoETLFramework.WriteLog("Loading header line at [{0}]...".FormatString(pair.Item1));
                    LoadHeaderLine(pair);
                    _headerFound = true;
                    return(true);
                }

                return(false);
            }))
            {
                while (true)
                {
                    Tuple <int, string> pair = e.Peek;
                    if (pair == null)
                    {
                        RaiseEndLoad(sr);
                        yield break;
                    }

                    object rec = ChoActivator.CreateInstance(RecordType);
                    if (!LoadLine(pair, ref rec))
                    {
                        yield break;
                    }

                    //StoreState(e.Current, rec != null);

                    e.MoveNext();

                    if (rec == null)
                    {
                        continue;
                    }

                    yield return(rec);
                }
            }
        }
Beispiel #19
0
        private bool ToText(long index, object rec, out string recText)
        {
            if (typeof(IChoScalarObject).IsAssignableFrom(Configuration.RecordType))
            {
                rec = ChoActivator.CreateInstance(Configuration.RecordType, rec);
            }

            recText = null;
            StringBuilder msg        = new StringBuilder();
            object        fieldValue = null;
            string        fieldText  = null;
            ChoFixedLengthRecordFieldConfiguration fieldConfig = null;

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

            //bool firstColumn = true;
            PropertyInfo pi      = null;
            object       rootRec = rec;
            IDictionary <string, Object> dict = null;

            foreach (KeyValuePair <string, ChoFixedLengthRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict)
            {
                fieldConfig = kvp.Value;
                fieldValue  = null;
                fieldText   = String.Empty;
                if (Configuration.PIDict != null)
                {
                    Configuration.PIDict.TryGetValue(kvp.Key, out pi);
                }

                rec = GetDeclaringRecord(kvp.Value.DeclaringMember, rootRec);

                dict = rec.ToDynamicObject() as IDictionary <string, Object>;
                if (Configuration.IsDynamicObject)
                {
                    dict = dict.Flatten().ToDictionary();
                }

                if (Configuration.ThrowAndStopOnMissingField)
                {
                    if (Configuration.IsDynamicObject)
                    {
                        if (!dict.ContainsKey(kvp.Key))
                        {
                            throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' FixedLength column.".FormatString(fieldConfig.FieldName));
                        }
                    }
                    else
                    {
                        if (pi == null)
                        {
                            throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' FixedLength column.".FormatString(fieldConfig.FieldName));
                        }
                    }
                }

                try
                {
                    if (Configuration.IsDynamicObject)
                    {
                        fieldValue = dict[kvp.Key]; // dict.GetValue(kvp.Key, Configuration.FileHeaderConfiguration.IgnoreCase, Configuration.Culture);
                        if (kvp.Value.FieldType == null)
                        {
                            if (fieldValue == null)
                            {
                                kvp.Value.FieldType = typeof(string);
                            }
                            else
                            {
                                kvp.Value.FieldType = fieldValue.GetType();
                            }
                        }
                    }
                    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;
                        }
                    }

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

                    if (fieldConfig.ValueConverter != null)
                    {
                        fieldValue = fieldConfig.ValueConverter(fieldValue);
                    }
                    else
                    {
                        rec.GetNConvertMemberValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue);
                    }

                    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)
                        {
                            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, fieldText, 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);
                        }
                    }
                }

                if (fieldValue == null)
                {
                    fieldText = String.Empty;
                }
                else
                {
                    fieldText = fieldValue.ToString();
                }

                msg.Append(NormalizeFieldValue(kvp.Key, fieldText, kvp.Value.Size, kvp.Value.Truncate, kvp.Value.QuoteField,
                                               GetFieldValueJustification(kvp.Value.FieldValueJustification, kvp.Value.FieldType),
                                               GetFillChar(kvp.Value.FillChar, kvp.Value.FieldType), false, kvp.Value.NullValue,
                                               kvp.Value.GetFieldValueTrimOption(kvp.Value.FieldType)));
            }

            recText = msg.ToString();
            return(true);
        }
        private IEnumerable <object> AsEnumerable(object source, TraceSwitch traceSwitch, Func <object, bool?> filterFunc = null)
        {
            TraceSwitch = traceSwitch;

            TextReader sr = source as TextReader;

            ChoGuard.ArgumentNotNull(sr, "TextReader");

            if (sr is StreamReader)
            {
                ((StreamReader)sr).Seek(0, SeekOrigin.Begin);
            }

            if (Configuration.RecordSelector == null)
            {
                throw new ChoRecordConfigurationException("Missing record selector.");
            }

            if (!RaiseBeginLoad(sr))
            {
                yield break;
            }

            string[] commentTokens = Configuration.Comments;
            bool?    skip          = false;
            bool     _headerFound  = false;
            bool?    skipUntil     = true;
            bool?    doWhile       = true;

            using (ChoPeekEnumerator <Tuple <long, string> > e = new ChoPeekEnumerator <Tuple <long, string> >(
                       new ChoIndexedEnumerator <string>(sr.ReadLines(Configuration.EOLDelimiter, Configuration.QuoteChar, Configuration.MayContainEOLInData, Configuration.MaxLineSize)).ToEnumerable(),
                       (pair) =>
            {
                //bool isStateAvail = IsStateAvail();

                skip = false;
                if (skipUntil != null)
                {
                    if (skipUntil.Value)
                    {
                        skipUntil = RaiseSkipUntil(pair);
                        if (skipUntil == null)
                        {
                        }
                        else
                        {
                            skip = skipUntil.Value;
                        }
                    }
                }

                //if (isStateAvail)
                //{
                //    if (!IsStateMatches(item))
                //    {
                //        skip = filterFunc != null ? filterFunc(item) : false;
                //    }
                //    else
                //        skip = true;
                //}
                //else
                //    skip = filterFunc != null ? filterFunc(item) : false;

                if (skip == null)
                {
                    return(null);
                }


                if (TraceSwitch.TraceVerbose)
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, Environment.NewLine);

                    if (!skip.Value)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Loading line [{0}]...".FormatString(pair.Item1));
                    }
                    else
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping line [{0}]...".FormatString(pair.Item1));
                    }
                }

                if (skip.Value)
                {
                    return(skip);
                }

                //if (!(sr.BaseStream is MemoryStream))
                //    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, ChoETLFramework.Switch.TraceVerbose, "Loading line [{0}]...".FormatString(item.Item1));

                //if (Task != null)
                //    return !IsStateNOTExistsOrNOTMatch(item);

                if (pair.Item2.IsNullOrWhiteSpace())
                {
                    if (!Configuration.IgnoreEmptyLine)
                    {
                        throw new ChoParserException("Empty line found at [{0}] location.".FormatString(pair.Item1));
                    }
                    else
                    {
                        if (TraceSwitch.TraceVerbose)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Ignoring empty line found at [{0}].".FormatString(pair.Item1));
                        }
                        return(true);
                    }
                }

                if (commentTokens != null && commentTokens.Length > 0)
                {
                    foreach (string comment in commentTokens)
                    {
                        if (!pair.Item2.IsNull() && pair.Item2.StartsWith(comment, StringComparison.Ordinal))     //, true, Configuration.Culture))
                        {
                            if (TraceSwitch.TraceVerbose)
                            {
                                ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Comment line found at [{0}]...".FormatString(pair.Item1));
                            }
                            return(true);
                        }
                    }
                }

                if (!_configCheckDone)
                {
                    Configuration.Validate(pair);     // GetHeaders(pair.Item2));
                    _configCheckDone = true;
                }

                //Ignore Header if any
                if (Configuration.FileHeaderConfiguration.HasHeaderRecord &&
                    !_headerFound)
                {
                    if (TraceSwitch.TraceVerbose)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Ignoring header line at [{0}]...".FormatString(pair.Item1));
                    }
                    _headerFound = true;
                    return(true);
                }

                return(false);
            }))
            {
                while (true)
                {
                    Tuple <long, string> pair = e.Peek;
                    if (pair == null)
                    {
                        RaiseEndLoad(sr);
                        yield break;
                    }

                    Type recType = Configuration.RecordSelector(pair.Item2);
                    if (recType == null)
                    {
                        if (Configuration.IgnoreIfNoRecordParserExists)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, $"No record type found for [{pair.Item1}] line to parse.");
                        }
                        else
                        {
                            throw new ChoParserException($"No record type found for [{pair.Item1}] line to parse.");
                        }
                    }

                    object rec = ChoActivator.CreateInstance(recType);
                    if (!LoadLine(pair, ref rec))
                    {
                        yield break;
                    }

                    //StoreState(e.Current, rec != null);

                    e.MoveNext();

                    if (rec == null)
                    {
                        continue;
                    }

                    yield return(rec);

                    if (Configuration.NotifyAfter > 0 && pair.Item1 % Configuration.NotifyAfter == 0)
                    {
                        if (RaisedRowsLoaded(pair.Item1))
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested.");
                            yield break;
                        }
                    }

                    if (doWhile != null)
                    {
                        doWhile = RaiseDoWhile(pair);
                        if (doWhile != null && doWhile.Value)
                        {
                            break;
                        }
                    }
                }
            }
        }
Beispiel #21
0
        public static object[] GetTypeConvertersForType(Type objType)
        {
            if (objType == null)
            {
                return(null);
            }

            if (DoNotUseTypeConverterForTypes)
            {
                return(EmptyTypeConverters);
            }

            lock (_typeMemberTypeConverterCacheLockObject)
            {
                if (_typeTypeConverterCache.ContainsKey(objType))
                {
                    return(_typeTypeConverterCache[objType]);
                }
                else
                {
                    if (DoNotDiscoverTypeConverterForTypes)
                    {
                        return(EmptyTypeConverters);
                    }

                    if (!_typeTypeConverterCache.ContainsKey(objType))
                    {
                        Type type = objType;
                        if (ChoTypeConverter.Global.Contains(type))
                        {
                            _typeTypeConverterCache[type] = (from a1 in ChoTypeConverter.Global.GetAll()
                                                             where a1.Key == type
                                                             select a1.Value).ToArray();
                            return(_typeTypeConverterCache[type]);
                        }

                        Type[] types = ChoType.GetTypes(typeof(ChoTypeConverterAttribute)).Where(t => t.GetCustomAttribute <ChoTypeConverterAttribute>().ConverterType != null && t.GetCustomAttribute <ChoTypeConverterAttribute>().ConverterType.IsAssignableFrom(objType)).ToArray();

                        if (types != null)
                        {
                            int index1 = 0;
                            SortedList <int, object> queue1       = new SortedList <int, object>();
                            SortedList <int, object> paramsQueue1 = new SortedList <int, object>();

                            foreach (Type t in types)
                            {
                                queue1.Add(index1, ChoActivator.CreateInstance(t));
                                index1++;
                            }
                            _typeTypeConverterCache.Add(objType, queue1.Values.ToArray());
                            return(_typeTypeConverterCache[objType]);
                        }

                        TypeConverter converter = TypeDescriptor.GetConverter(objType);
                        if (converter != null)
                        {
                            _typeTypeConverterCache.Add(objType, new object[] { converter });
                        }
                        else
                        {
                            _typeTypeConverterCache.Add(objType, EmptyTypeConverters);
                        }

                        _typeTypeConverterParamsCache.Add(objType, EmptyParams);
                    }

                    return(_typeTypeConverterCache[objType]);
                }
            }
        }
Beispiel #22
0
 public static IEnumerable <T> Touch <T>(this IEnumerable <T> items) =>
 items == null || items.Count() == 0 ? Enumerable.Repeat(ChoActivator.CreateInstance <T>(), 1) : items;
Beispiel #23
0
        private IEnumerable <object> AsEnumerable(object source, TraceSwitch traceSwitch, Func <object, bool?> filterFunc = null)
        {
            TraceSwitch = traceSwitch;

            TextReader sr = source as TextReader;

            ChoGuard.ArgumentNotNull(sr, "TextReader");

            if (sr is StreamReader)
            {
                ((StreamReader)sr).Seek(0, SeekOrigin.Begin);
            }

            if (!RaiseBeginLoad(sr))
            {
                yield break;
            }

            string[]                     commentTokens      = Configuration.Comments;
            bool?                        skip               = false;
            bool                         isRecordStartFound = false;
            bool                         isRecordEndFound   = false;
            long                         seekOriginPos      = sr is StreamReader ? ((StreamReader)sr).BaseStream.Position : 0;
            List <string>                headers            = new List <string>();
            Tuple <long, string>         lastLine           = null;
            List <Tuple <long, string> > recLines           = new List <Tuple <long, string> >();
            long                         recNo              = 0;
            int  loopCount      = Configuration.AutoDiscoverColumns && Configuration.KVPRecordFieldConfigurations.Count == 0 ? 2 : 1;
            bool isHeaderFound  = loopCount == 1;
            bool IsHeaderLoaded = false;
            Tuple <long, string> pairIn;
            bool abortRequested = false;
            bool?skipUntil      = true;
            bool?doWhile        = true;

            for (int i = 0; i < loopCount; i++)
            {
                if (i == 1)
                {
                    if (sr is StreamReader)
                    {
                        ((StreamReader)sr).Seek(seekOriginPos, SeekOrigin.Begin);
                    }
                    TraceSwitch = traceSwitch;
                }
                else
                {
                    TraceSwitch = ChoETLFramework.TraceSwitchOff;
                }
                lastLine = null;
                recLines.Clear();
                isRecordEndFound   = false;
                isRecordStartFound = false;

                using (ChoPeekEnumerator <Tuple <long, string> > e = new ChoPeekEnumerator <Tuple <long, string> >(
                           new ChoIndexedEnumerator <string>(sr.ReadLines(Configuration.EOLDelimiter, Configuration.QuoteChar, Configuration.MayContainEOLInData)).ToEnumerable(),
                           (pair) =>
                {
                    //bool isStateAvail = IsStateAvail();
                    skip = false;

                    if (skipUntil != null)
                    {
                        if (skipUntil.Value)
                        {
                            skipUntil = RaiseSkipUntil(pair);
                            if (skipUntil == null)
                            {
                            }
                            else
                            {
                                if (skipUntil.Value)
                                {
                                    skip = skipUntil;
                                }
                                else
                                {
                                    skip = true;
                                }
                            }
                        }
                    }

                    //if (isStateAvail)
                    //{
                    //    if (!IsStateMatches(item))
                    //    {
                    //        skip = filterFunc != null ? filterFunc(item) : false;
                    //    }
                    //    else
                    //        skip = true;
                    //}
                    //else
                    //    skip = filterFunc != null ? filterFunc(item) : false;

                    if (skip == null)
                    {
                        return(null);
                    }


                    if (TraceSwitch.TraceVerbose)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, Environment.NewLine);

                        if (!skip.Value)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Loading line [{0}]...".FormatString(pair.Item1));
                        }
                        else
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping line [{0}]...".FormatString(pair.Item1));
                        }
                    }

                    if (skip.Value)
                    {
                        return(skip);
                    }

                    //if (!(sr.BaseStream is MemoryStream))
                    //    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, ChoETLFramework.Switch.TraceVerbose, "Loading line [{0}]...".FormatString(item.Item1));

                    //if (Task != null)
                    //    return !IsStateNOTExistsOrNOTMatch(item);

                    if (pair.Item2.IsNullOrWhiteSpace())
                    {
                        if (!Configuration.IgnoreEmptyLine)
                        {
                            throw new ChoParserException("Empty line found at [{0}] location.".FormatString(pair.Item1));
                        }
                        else
                        {
                            if (TraceSwitch.TraceVerbose)
                            {
                                ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Ignoring empty line found at [{0}].".FormatString(pair.Item1));
                            }
                            return(true);
                        }
                    }

                    if (commentTokens != null && commentTokens.Length > 0)
                    {
                        foreach (string comment in commentTokens)
                        {
                            if (!pair.Item2.IsNull() && pair.Item2.StartsWith(comment, StringComparison.Ordinal))     //, true, Configuration.Culture))
                            {
                                if (TraceSwitch.TraceVerbose)
                                {
                                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Comment line found at [{0}]...".FormatString(pair.Item1));
                                }
                                return(true);
                            }
                        }
                    }

                    if (!_configCheckDone)
                    {
                        Configuration.Validate(null);
                        var dict = Configuration.KVPRecordFieldConfigurations.ToDictionary(i1 => i1.Name, i1 => i1.FieldType == null ? null : i1.FieldType);
                        RaiseMembersDiscovered(dict);
                        Configuration.UpdateFieldTypesIfAny(dict);
                        _configCheckDone = true;
                    }

                    return(false);
                }))
                {
                    while (true)
                    {
                        pairIn = e.Peek;

                        if (!isRecordStartFound)
                        {
                            if (pairIn == null)
                            {
                                break;
                            }

                            lastLine = null;
                            recLines.Clear();
                            isRecordEndFound   = false;
                            isRecordStartFound = true;
                            if (!Configuration.RecordStart.IsNullOrWhiteSpace())
                            {
                                //Move to record start
                                while (!(Configuration.IsRecordStartMatch(e.Peek.Item2)))
                                {
                                    e.MoveNext();
                                    if (e.Peek == null)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (e.Peek != null)
                            {
                                ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Record start found at [{0}] line...".FormatString(e.Peek.Item1));
                            }

                            e.MoveNext();
                            continue;
                        }
                        else
                        {
                            string recordEnd = !Configuration.RecordEnd.IsNullOrWhiteSpace() ? Configuration.RecordEnd : Configuration.RecordStart;
                            if (!recordEnd.IsNullOrWhiteSpace())
                            {
                                if (e.Peek == null)
                                {
                                    if (Configuration.RecordEnd.IsNullOrWhiteSpace())
                                    {
                                        isRecordEndFound = true;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                else
                                {
                                    //Move to record start
                                    if (Configuration.IsRecordEndMatch(e.Peek.Item2))
                                    {
                                        isRecordEndFound   = true;
                                        isRecordStartFound = false;
                                        if (e.Peek != null)
                                        {
                                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Record end found at [{0}] line...".FormatString(e.Peek.Item1));
                                        }
                                    }
                                }
                            }
                            else if (e.Peek == null)
                            {
                                isRecordEndFound = true;
                            }

                            if (!isHeaderFound)
                            {
                                //if (isRecordEndFound && headers.Count == 0)
                                //{
                                //    //throw new ChoParserException("Unexpected EOF found.");
                                //}
                                if (!isRecordEndFound)
                                {
                                    e.MoveNext();
                                    if (e.Peek != null)
                                    {
                                        //If line empty or line continuation, skip
                                        if (pairIn.Item2.IsNullOrWhiteSpace() || IsLineContinuationCharFound(pairIn.Item2)) //.Item2[0] == ' ' || pairIn.Item2[0] == '\t')
                                        {
                                        }
                                        else
                                        {
                                            string header = ToKVP(pairIn.Item1, pairIn.Item2).Key;
                                            if (!header.IsNullOrWhiteSpace())
                                            {
                                                headers.Add(header);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    Configuration.Validate(headers.ToArray());
                                    isHeaderFound    = true;
                                    isRecordEndFound = false;
                                    IsHeaderLoaded   = true;
                                    break;
                                }
                            }
                            else
                            {
                                if (!IsHeaderLoaded)
                                {
                                    Configuration.Validate(new string[] { });
                                    IsHeaderLoaded = true;
                                }

                                if (isRecordEndFound && recLines.Count == 0)
                                {
                                    //throw new ChoParserException("Unexpected EOF found.");
                                }
                                else if (!isRecordEndFound)
                                {
                                    e.MoveNext();
                                    if (e.Peek != null)
                                    {
                                        //If line empty or line continuation, skip
                                        if (pairIn.Item2.IsNullOrWhiteSpace())
                                        {
                                            if (!Configuration.IgnoreEmptyLine)
                                            {
                                                throw new ChoParserException("Empty line found at [{0}] location.".FormatString(pairIn.Item1));
                                            }
                                            else
                                            {
                                                Tuple <long, string> t = new Tuple <long, string>(lastLine.Item1, lastLine.Item2 + Configuration.EOLDelimiter);
                                                recLines.RemoveAt(recLines.Count - 1);
                                                recLines.Add(t);
                                            }
                                        }
                                        else if (IsLineContinuationCharFound(pairIn.Item2)) //pairIn.Item2[0] == ' ' || pairIn.Item2[0] == '\t')
                                        {
                                            if (lastLine == null)
                                            {
                                                throw new ChoParserException("Unexpected line continuation found at {0} location.".FormatString(pairIn.Item1));
                                            }
                                            else
                                            {
                                                Tuple <long, string> t = new Tuple <long, string>(lastLine.Item1, lastLine.Item2 + Configuration.EOLDelimiter + pairIn.Item2);
                                                recLines.RemoveAt(recLines.Count - 1);
                                                recLines.Add(t);
                                            }
                                        }
                                        else
                                        {
                                            lastLine = pairIn;
                                            recLines.Add(pairIn);
                                        }
                                    }
                                }
                                else
                                {
                                    object rec = Configuration.IsDynamicObject ? new ChoDynamicObject(new Dictionary <string, object>(Configuration.FileHeaderConfiguration.StringComparer))
                                    {
                                        ThrowExceptionIfPropNotExists = true,
                                        AlternativeKeys = Configuration.AlternativeKeys
                                    } : ChoActivator.CreateInstance(RecordType);
                                    if (!LoadLines(new Tuple <long, List <Tuple <long, string> > >(++recNo, recLines), ref rec))
                                    {
                                        yield break;
                                    }

                                    isRecordStartFound = false;
                                    //StoreState(e.Current, rec != null);

                                    if (!Configuration.RecordEnd.IsNullOrWhiteSpace())
                                    {
                                        e.MoveNext();
                                    }

                                    if (rec == null)
                                    {
                                        continue;
                                    }

                                    yield return(rec);

                                    if (Configuration.NotifyAfter > 0 && recNo % Configuration.NotifyAfter == 0)
                                    {
                                        if (RaisedRowsLoaded(recNo))
                                        {
                                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested.");
                                            abortRequested = true;
                                            yield break;
                                        }
                                    }

                                    if (e.Peek == null)
                                    {
                                        break;
                                    }
                                }
                            }
                        }

                        if (doWhile != null)
                        {
                            doWhile = RaiseDoWhile(pairIn);
                            if (doWhile != null && doWhile.Value)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            if (!abortRequested)
            {
                RaisedRowsLoaded(recNo, true);
            }
            RaiseEndLoad(sr);
        }
Beispiel #24
0
 private void Initialize()
 {
     _pointer = 0;
     Array.Copy(_backupCache, _cache, _size);
     Task.Factory.StartNew(() => Parallel.For(0, _size, i => _backupCache[i] = ChoActivator.CreateInstance <T>()));
 }
Beispiel #25
0
        private IEnumerable <object> AsEnumerable(object source, TraceSwitch traceSwitch, Func <object, bool?> filterFunc = null)
        {
            TraceSwitch = traceSwitch;

            TextReader sr = source as TextReader;

            ChoGuard.ArgumentNotNull(sr, "TextReader");

            if (sr is StreamReader)
            {
                ((StreamReader)sr).Seek(0, SeekOrigin.Begin);
            }

            if (!RaiseBeginLoad(sr))
            {
                yield break;
            }

            string[]      commentTokens              = Configuration.Comments;
            bool?         skip                       = false;
            bool          abortRequested             = false;
            long          runningCount               = 0;
            long          recCount                   = 0;
            bool          headerLineLoaded           = false;
            List <object> buffer                     = new List <object>();
            IDictionary <string, Type> recFieldTypes = null;
            bool?skipUntil = true;
            bool?doWhile   = true;

            using (ChoPeekEnumerator <Tuple <long, string> > e = new ChoPeekEnumerator <Tuple <long, string> >(
                       new ChoIndexedEnumerator <string>(sr.ReadLines(Configuration.EOLDelimiter, Configuration.QuoteChar, false /*Configuration.MayContainEOLInData*/)).ToEnumerable(),
                       (pair) =>
            {
                //bool isStateAvail = IsStateAvail();
                skip = false;

                if (skipUntil != null)
                {
                    if (skipUntil.Value)
                    {
                        skipUntil = RaiseSkipUntil(pair);
                        if (skipUntil == null)
                        {
                        }
                        else
                        {
                            if (skipUntil.Value)
                            {
                                skip = skipUntil;
                            }
                            else
                            {
                                skip = true;
                            }
                        }
                    }
                }
                //if (isStateAvail)
                //{
                //    if (!IsStateMatches(item))
                //    {
                //        skip = filterFunc != null ? filterFunc(item) : false;
                //    }
                //    else
                //        skip = true;
                //}
                //else
                //    skip = filterFunc != null ? filterFunc(item) : false;

                if (skip == null)
                {
                    return(null);
                }


                if (TraceSwitch.TraceVerbose)
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, Environment.NewLine);

                    if (!skip.Value)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Loading line [{0}]...".FormatString(pair.Item1));
                    }
                    else
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping line [{0}]...".FormatString(pair.Item1));
                    }
                }

                if (skip.Value)
                {
                    return(skip);
                }

                //if (!(sr.BaseStream is MemoryStream))
                //    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, ChoETLFramework.Switch.TraceVerbose, "Loading line [{0}]...".FormatString(item.Item1));

                //if (Task != null)
                //    return !IsStateNOTExistsOrNOTMatch(item);

                if (pair.Item2.IsNullOrWhiteSpace())
                {
                    if (!Configuration.IgnoreEmptyLine)
                    {
                        throw new ChoParserException("Empty line found at [{0}] location.".FormatString(pair.Item1));
                    }
                    else
                    {
                        if (TraceSwitch.TraceVerbose)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Ignoring empty line found at [{0}].".FormatString(pair.Item1));
                        }
                        return(true);
                    }
                }

                if (commentTokens != null && commentTokens.Length > 0)
                {
                    foreach (string comment in commentTokens)
                    {
                        if (!pair.Item2.IsNull() && pair.Item2.StartsWith(comment, StringComparison.Ordinal))     //, true, Configuration.Culture))
                        {
                            if (TraceSwitch.TraceVerbose)
                            {
                                ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Comment line found at [{0}]...".FormatString(pair.Item1));
                            }
                            return(true);
                        }
                    }
                }

                if (Configuration.FileHeaderConfiguration.HeaderLineAt > 0)
                {
                    if (pair.Item1 < Configuration.FileHeaderConfiguration.HeaderLineAt)
                    {
                        if (TraceSwitch.TraceVerbose)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Header line at {1}. Skipping [{0}] line...".FormatString(pair.Item1, Configuration.FileHeaderConfiguration.HeaderLineAt));
                        }
                        return(true);
                    }
                }

                if (!_configCheckDone)
                {
                    if (Configuration.SupportsMultiRecordTypes && Configuration.RecordSelector != null && !Configuration.RecordTypeMapped)
                    {
                    }
                    else
                    {
                        Configuration.Validate(pair);                                 // GetHeaders(pair.Item2));
                    }
                    var dict = recFieldTypes = Configuration.FixedLengthRecordFieldConfigurations.ToDictionary(i => i.Name, i => i.FieldType == null ? null : i.FieldType);
                    RaiseMembersDiscovered(dict);
                    Configuration.UpdateFieldTypesIfAny(dict);
                    _configCheckDone = true;
                }

                //LoadHeader if any
                if ((Configuration.FileHeaderConfiguration.HasHeaderRecord ||
                     Configuration.FileHeaderConfiguration.HeaderLineAt > 0) &&
                    !_headerFound)
                {
                    if (Configuration.FileHeaderConfiguration.IgnoreHeader)
                    {
                        if (TraceSwitch.TraceVerbose)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Ignoring header line at [{0}]...".FormatString(pair.Item1));
                        }
                    }
                    else
                    {
                        if (TraceSwitch.TraceVerbose)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Loading header line at [{0}]...".FormatString(pair.Item1));
                        }

                        headerLineLoaded = true;
                        LoadHeaderLine(pair);
                    }
                    _headerFound = true;
                    return(true);
                }

                return(false);
            }))
            {
                while (true)
                {
                    recCount++;
                    Tuple <long, string> pair = e.Peek;
                    if (pair == null)
                    {
                        if (!abortRequested)
                        {
                            RaisedRowsLoaded(runningCount);
                        }

                        RaiseEndLoad(sr);
                        yield break;
                    }
                    runningCount = pair.Item1;

                    object rec = null;
                    if (Configuration.SupportsMultiRecordTypes && Configuration.RecordSelector != null)
                    {
                        Type recType = Configuration.RecordSelector(pair);
                        if (recType == null)
                        {
                            if (Configuration.IgnoreIfNoRecordTypeFound)
                            {
                                ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, $"No record type found for [{pair.Item1}] line to parse.");
                                e.MoveNext();
                                continue;
                            }
                            else
                            {
                                throw new ChoParserException($"No record type found for [{pair.Item1}] line to parse.");
                            }
                        }

                        if (!Configuration.RecordTypeMapped)
                        {
                            Configuration.MapRecordFields(recType);
                            Configuration.Validate(null);
                        }

                        rec = recType.IsDynamicType() ? new ChoDynamicObject(new Dictionary <string, object>(Configuration.FileHeaderConfiguration.StringComparer))
                        {
                            ThrowExceptionIfPropNotExists = true,
                            AlternativeKeys = Configuration.AlternativeKeys
                        } : ChoActivator.CreateInstance(recType);
                    }
                    else
                    {
                        rec = Configuration.IsDynamicObject ? new ChoDynamicObject(new Dictionary <string, object>(Configuration.FileHeaderConfiguration.StringComparer))
                        {
                            ThrowExceptionIfPropNotExists = true,
                            AlternativeKeys = Configuration.AlternativeKeys
                        } : ChoActivator.CreateInstance(RecordType);
                    }

                    if (!LoadLine(pair, ref rec))
                    {
                        yield break;
                    }

                    //StoreState(e.Current, rec != null);

                    e.MoveNext();

                    if (rec == null)
                    {
                        continue;
                    }

                    if (!Configuration.SupportsMultiRecordTypes && Configuration.IsDynamicObject)
                    {
                        if (Configuration.AreAllFieldTypesNull && Configuration.AutoDiscoverFieldTypes && Configuration.MaxScanRows > 0 && recCount <= Configuration.MaxScanRows)
                        {
                            buffer.Add(rec);
                            RaiseRecordFieldTypeAssessment(recFieldTypes, (IDictionary <string, object>)rec, recCount == Configuration.MaxScanRows);
                            if (recCount == Configuration.MaxScanRows || e.Peek == null)
                            {
                                Configuration.UpdateFieldTypesIfAny(recFieldTypes);
                                var dict = recFieldTypes = Configuration.FixedLengthRecordFieldConfigurations.ToDictionary(i => i.Name, i => i.FieldType == null ? null : i.FieldType);
                                RaiseMembersDiscovered(dict);

                                foreach (object rec1 in buffer)
                                {
                                    yield return(ConvertToNestedObjectIfApplicable(new ChoDynamicObject(MigrateToNewSchema(rec1 as IDictionary <string, object>, recFieldTypes)) as object, headerLineLoaded));
                                }
                            }
                        }
                        else
                        {
                            yield return(ConvertToNestedObjectIfApplicable(rec, headerLineLoaded));
                        }
                    }
                    else
                    {
                        yield return(rec);
                    }

                    if (Configuration.NotifyAfter > 0 && pair.Item1 % Configuration.NotifyAfter == 0)
                    {
                        if (RaisedRowsLoaded(pair.Item1))
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested.");
                            yield break;
                        }
                    }

                    if (doWhile != null)
                    {
                        doWhile = RaiseDoWhile(pair);
                        if (doWhile != null && doWhile.Value)
                        {
                            break;
                        }
                    }
                }
            }
        }
        private IEnumerable <object> AsEnumerable(object source, TraceSwitch traceSwitch, Func <object, bool?> filterFunc = null)
        {
            TraceSwitch = traceSwitch;

            StreamReader sr = source as StreamReader;

            ChoGuard.ArgumentNotNull(sr, "StreamReader");

            if (Configuration.RecordSelector == null)
            {
                throw new ChoRecordConfigurationException("Missing record selector.");
            }

            sr.Seek(0, SeekOrigin.Begin);

            if (!RaiseBeginLoad(sr))
            {
                yield break;
            }

            string[] commentTokens = Configuration.Comments;
            bool     _headerFound  = false;

            using (ChoPeekEnumerator <Tuple <int, string> > e = new ChoPeekEnumerator <Tuple <int, string> >(
                       new ChoIndexedEnumerator <string>(sr.ReadLines(Configuration.EOLDelimiter, Configuration.QuoteChar)).ToEnumerable(),
                       (pair) =>
            {
                //bool isStateAvail = IsStateAvail();

                bool?skip = false;

                //if (isStateAvail)
                //{
                //    if (!IsStateMatches(item))
                //    {
                //        skip = filterFunc != null ? filterFunc(item) : false;
                //    }
                //    else
                //        skip = true;
                //}
                //else
                //    skip = filterFunc != null ? filterFunc(item) : false;

                if (skip == null)
                {
                    return(null);
                }

                //if (!(sr.BaseStream is MemoryStream))
                //{
                ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, Environment.NewLine);

                if (!skip.Value)
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Loading line [{0}]...".FormatString(pair.Item1));
                }
                else
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping line [{0}]...".FormatString(pair.Item1));
                }
                //}

                if (skip.Value)
                {
                    return(skip);
                }

                //if (!(sr.BaseStream is MemoryStream))
                //    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, ChoETLFramework.Switch.TraceVerbose, "Loading line [{0}]...".FormatString(item.Item1));

                //if (Task != null)
                //    return !IsStateNOTExistsOrNOTMatch(item);

                if (pair.Item2.IsNullOrWhiteSpace())
                {
                    if (!Configuration.IgnoreEmptyLine)
                    {
                        throw new ChoParserException("Empty line found at {0} location.".FormatString(e.Peek.Item1));
                    }
                    else
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Empty line found at [{0}]...".FormatString(pair.Item1));
                        return(true);
                    }
                }

                if (commentTokens == null)
                {
                    return(false);
                }
                else
                {
                    var x = (from comment in commentTokens
                             where !pair.Item2.IsNull() && pair.Item2.StartsWith(comment, true, Configuration.Culture)
                             select comment).FirstOrDefault();
                    if (x != null)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Comment line found at [{0}]...".FormatString(pair.Item1));
                        return(true);
                    }
                }

                if (!_configCheckDone)
                {
                    Configuration.Validate(pair);     // GetHeaders(pair.Item2));
                    _configCheckDone = true;
                }

                //Ignore Header if any
                if (Configuration.FileHeaderConfiguration.HasHeaderRecord &&
                    !_headerFound)
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Ignoring header line at [{0}]...".FormatString(pair.Item1));
                    _headerFound = true;
                    return(true);
                }

                return(false);
            }))
            {
                while (true)
                {
                    Tuple <int, string> pair = e.Peek;
                    if (pair == null)
                    {
                        RaiseEndLoad(sr);
                        yield break;
                    }

                    Type recType = Configuration.RecordSelector(pair.Item2);
                    if (recType == null)
                    {
                        if (Configuration.IgnoreIfNoRecordParserExists)
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, $"No record type found for [{pair.Item1}] line to parse.");
                        }
                        else
                        {
                            throw new ChoParserException($"No record type found for [{pair.Item1}] line to parse.");
                        }
                    }

                    object rec = ChoActivator.CreateInstance(recType);
                    if (!LoadLine(pair, ref rec))
                    {
                        yield break;
                    }

                    //StoreState(e.Current, rec != null);

                    e.MoveNext();

                    if (rec == null)
                    {
                        continue;
                    }

                    yield return(rec);
                }
            }
        }
Beispiel #27
0
        private bool LoadNode(Tuple <long, IDictionary <string, object> > pair, ref object rec)
        {
            bool ignoreFieldValue = pair.Item2.IgnoreFieldValue(Configuration.IgnoreFieldValueMode);

            if (ignoreFieldValue)
            {
                return(false);
            }
            else if (pair.Item2 == null && !Configuration.IsDynamicObject)
            {
                rec = RecordType.CreateInstanceAndDefaultToMembers(Configuration.RecordFieldConfigurationsDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value as ChoRecordFieldConfiguration));
                return(true);
            }

            if (Configuration.SupportsMultiRecordTypes && Configuration.RecordTypeSelector != null)
            {
                Type recType = Configuration.RecordTypeSelector(pair);
                if (recType == null)
                {
                    if (Configuration.IgnoreIfNoRecordTypeFound)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, $"No record type found for [{pair.Item1}] line to parse.");
                        return(true);
                    }
                    else
                    {
                        throw new ChoParserException($"No record type found for [{pair.Item1}] line to parse.");
                    }
                }

                if (!Configuration.RecordTypeMapped)
                {
                    Configuration.MapRecordFields(recType);
                    Configuration.Validate(null);
                }

                rec = recType.IsDynamicType() ? new ChoDynamicObject()
                {
                    ThrowExceptionIfPropNotExists = Configuration.ThrowExceptionIfDynamicPropNotExists == null ? ChoDynamicObjectSettings.ThrowExceptionIfPropNotExists : Configuration.ThrowExceptionIfDynamicPropNotExists.Value,
                } : ChoActivator.CreateInstance(recType);
                RecordType = recType;
            }
            else if (Configuration.IsDynamicObject)
            {
                rec = Configuration.IsDynamicObject ? new ChoDynamicObject()
                {
                    ThrowExceptionIfPropNotExists = Configuration.ThrowExceptionIfDynamicPropNotExists == null ? ChoDynamicObjectSettings.ThrowExceptionIfPropNotExists : Configuration.ThrowExceptionIfDynamicPropNotExists.Value,
                }
            }
            : ChoActivator.CreateInstance(RecordType);

            try
            {
                if (!RaiseBeforeRecordLoad(rec, ref pair))
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping...");
                    rec = null;
                    return(true);
                }
                //if (Configuration.CustomNodeSelecter != null)
                //{
                //    pair = new Tuple<long, IDictionary<string, object>>(pair.Item1, Configuration.CustomNodeSelecter(pair.Item2));
                //}

                if (pair.Item2 == null)
                {
                    rec = null;
                    return(true);
                }

                if (!FillRecord(ref rec, pair))
                {
                    return(false);
                }

                if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel)
                {
                    rec.DoObjectLevelValidation(Configuration, Configuration.ParquetRecordFieldConfigurations);
                }


                bool skip = false;
                if (!RaiseAfterRecordLoad(rec, pair, ref skip))
                {
                    return(false);
                }
                else if (skip)
                {
                    rec = null;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Reader.IsValid = false;
                if (ex is ChoMissingRecordFieldException && Configuration.ThrowAndStopOnMissingField)
                {
                    if (!RaiseRecordLoadError(rec, pair, ex))
                    {
                        throw;
                    }
                    else
                    {
                        //ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring record...".FormatString(ex.Message));
                        //rec = null;
                    }
                }
                else
                {
                    ChoETLFramework.HandleException(ref ex);
                    if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring record...".FormatString(ex.Message));
                        rec = null;
                    }
                    else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue)
                    {
                        if (!RaiseRecordLoadError(rec, pair, ex))
                        {
                            throw;
                        }
                        else
                        {
                            //ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring record...".FormatString(ex.Message));
                            //rec = null;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }

                return(true);
            }

            return(true);
        }

        object fieldValue = null;
        ChoParquetRecordFieldConfiguration fieldConfig = null;
        PropertyInfo pi = null;
Beispiel #28
0
        private IEnumerable <object> AsEnumerable(IEnumerable <DataColumn[]> dataColumns, TraceSwitch traceSwitch, Func <object, bool?> filterFunc = null)
        {
            TraceSwitch = traceSwitch;

            long counter = 0;
            Tuple <long, IDictionary <string, object> > pair = null;
            bool?         skip                       = false;
            bool?         skipUntil                  = true;
            bool?         doWhile                    = true;
            bool          abortRequested             = false;
            List <object> buffer                     = new List <object>();
            IDictionary <string, Type> recFieldTypes = null;

            foreach (var dc in dataColumns)
            {
                foreach (var obj in Unpack(dc))
                {
                    pair = new Tuple <long, IDictionary <string, object> >(++counter, obj);
                    skip = false;

                    if (skipUntil != null)
                    {
                        if (skipUntil.Value)
                        {
                            skipUntil = RaiseSkipUntil(pair);
                            if (skipUntil == null)
                            {
                            }
                            else
                            {
                                skip = skipUntil.Value;
                            }
                        }
                    }
                    if (skip == null)
                    {
                        break;
                    }
                    if (skip.Value)
                    {
                        continue;
                    }

                    if (!_configCheckDone)
                    {
                        if (Configuration.SupportsMultiRecordTypes && Configuration.RecordTypeSelector != null && !Configuration.RecordTypeMapped)
                        {
                        }
                        else
                        {
                            Configuration.Validate(pair);
                        }
                        var dict = Configuration.ParquetRecordFieldConfigurations.ToDictionary(i => i.Name, i => i.FieldType == null ? null : i.FieldType);
                        //if (Configuration.MaxScanRows == 0)
                        RaiseMembersDiscovered(dict);
                        Configuration.UpdateFieldTypesIfAny(dict);
                        _configCheckDone = true;
                    }

                    object rec = null;
                    if (TraceSwitch.TraceVerbose)
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Loading node [{0}]...".FormatString(pair.Item1));
                    }

                    rec = Configuration.IsDynamicObject ? new ChoDynamicObject()
                    {
                        ThrowExceptionIfPropNotExists = Configuration.ThrowExceptionIfDynamicPropNotExists == null ? ChoDynamicObjectSettings.ThrowExceptionIfPropNotExists : Configuration.ThrowExceptionIfDynamicPropNotExists.Value,
                    } : ChoActivator.CreateInstance(RecordType);

                    if (!LoadNode(pair, ref rec))
                    {
                        yield break;
                    }

                    if (rec == null)
                    {
                        continue;
                    }

                    if (!Configuration.SupportsMultiRecordTypes && Configuration.IsDynamicObject)
                    {
                        if (Configuration.AreAllFieldTypesNull && Configuration.AutoDiscoverFieldTypes && Configuration.MaxScanRows > 0 && counter <= Configuration.MaxScanRows)
                        {
                            buffer.Add(rec);
                            if (recFieldTypes == null)
                            {
                                recFieldTypes = Configuration.ParquetRecordFieldConfigurations.ToDictionary(i => i.FieldName, i => i.FieldType == null ? null : i.FieldType);
                            }
                            RaiseRecordFieldTypeAssessment(recFieldTypes, (IDictionary <string, object>)rec, counter == Configuration.MaxScanRows);
                            if (counter == Configuration.MaxScanRows)
                            {
                                Configuration.UpdateFieldTypesIfAny(recFieldTypes);
                                var dict = recFieldTypes = Configuration.ParquetRecordFieldConfigurations.ToDictionary(i => i.FieldName, i => i.FieldType == null ? null : i.FieldType);
                                RaiseMembersDiscovered(dict);

                                foreach (object rec1 in buffer)
                                {
                                    yield return(new ChoDynamicObject(MigrateToNewSchema(rec1 as IDictionary <string, object>, recFieldTypes)));
                                }

                                buffer.Clear();
                            }
                        }
                        else
                        {
                            yield return(rec);
                        }
                    }
                    else
                    {
                        yield return(rec);
                    }

                    if (Configuration.NotifyAfter > 0 && pair.Item1 % Configuration.NotifyAfter == 0)
                    {
                        if (RaisedRowsLoaded(pair.Item1))
                        {
                            ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested.");
                            abortRequested = true;
                            yield break;
                        }
                    }

                    if (doWhile != null)
                    {
                        doWhile = RaiseDoWhile(pair);
                        if (doWhile != null && doWhile.Value)
                        {
                            break;
                        }
                    }

                    pair = null;
                }
            }

            if (!Configuration.SupportsMultiRecordTypes && Configuration.IsDynamicObject)
            {
                if (buffer.Count > 0)
                {
                    Configuration.UpdateFieldTypesIfAny(recFieldTypes);
                    var dict = recFieldTypes = Configuration.ParquetRecordFieldConfigurations.ToDictionary(i => i.FieldName, i => i.FieldType == null ? null : i.FieldType);
                    RaiseMembersDiscovered(dict);

                    foreach (object rec1 in buffer)
                    {
                        yield return(new ChoDynamicObject(MigrateToNewSchema(rec1 as IDictionary <string, object>, recFieldTypes)));
                    }
                }
            }

            if (!abortRequested && pair != null)
            {
                RaisedRowsLoaded(pair.Item1, true);
            }
        }
Beispiel #29
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);
        }
Beispiel #30
0
        private bool ToText(long index, object rec, out string recText)
        {
            if (typeof(IChoScalarObject).IsAssignableFrom(Configuration.RecordType))
            {
                rec = ChoActivator.CreateInstance(Configuration.RecordType, rec);
            }

            recText = null;
            msg.Clear();

            if (Configuration.ColumnCountStrict)
            {
                CheckColumnCountStrict(rec);
            }
            if (Configuration.ColumnOrderStrict)
            {
                CheckColumnOrderStrict(rec);
            }

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

            foreach (KeyValuePair <string, ChoCSVRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict)
            {
                if (Configuration.IsDynamicObject)
                {
                    if (Configuration.IgnoredFields.Contains(kvp.Key))
                    {
                        continue;
                    }
                }

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

                rec = GetDeclaringRecord(kvp.Value.DeclaringMember, rootRec);

                dict = rec.ToDynamicObject() as IDictionary <string, Object>;
                if (Configuration.IsDynamicObject && Configuration.UseNestedKeyFormat)
                {
                    dict = dict.Flatten(Configuration.NestedColumnSeparator).ToDictionary();
                }

                if (Configuration.ThrowAndStopOnMissingField)
                {
                    if (Configuration.IsDynamicObject)
                    {
                        if (!dict.ContainsKey(kvp.Key))
                        {
                            if (fieldConfig.FieldPosition > dict.Count)
                            {
                                throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' CSV column.".FormatString(fieldConfig.FieldName));
                            }
                        }
                    }
                    else
                    {
                        if (pi == null)
                        {
                            pi = Configuration.PIDict.Where(kvp1 => kvp.Value.FieldPosition == kvp.Value.FieldPosition).FirstOrDefault().Value;
                        }

                        if (pi == null)
                        {
                            throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' CSV column.".FormatString(fieldConfig.FieldName));
                        }
                    }
                }

                try
                {
                    if (Configuration.IsDynamicObject)
                    {
                        fieldValue = dict.ContainsKey(kvp.Key) ? dict[kvp.Key] : dict[dict.Keys.ElementAt(fieldConfig.FieldPosition - 1)]; // dict.GetValue(kvp.Key, Configuration.FileHeaderConfiguration.IgnoreCase, Configuration.Culture);
                        if (rec is ChoDynamicObject)
                        {
                            var dobj = rec as ChoDynamicObject;
                            kvp.Value.FieldType = dobj.GetMemberType(kvp.Key);
                        }
                        if (kvp.Value.FieldType == null)
                        {
                            if (fieldValue == null)
                            {
                                kvp.Value.FieldType = typeof(string);
                            }
                            else
                            {
                                kvp.Value.FieldType = fieldValue.GetType();
                            }
                        }
                    }
                    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;
                        }
                    }

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

                    if (fieldConfig.ValueSelector == null)
                    {
                        if (fieldConfig.ValueConverter != null)
                        {
                            fieldValue = fieldConfig.ValueConverter(fieldValue);
                        }
                        else
                        {
                            rec.GetNConvertMemberValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue);
                        }
                    }
                    else
                    {
                        fieldValue = fieldConfig.ValueSelector(rec);
                    }

                    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)
                        {
                            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, fieldText, 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);
                        }
                    }
                }

                if (fieldValue == null)
                {
                    fieldText = String.Empty;
                }
                else
                {
                    if (fieldValue is IList)
                    {
                        StringBuilder sb    = new StringBuilder();
                        bool          first = true;
                        foreach (var item in (IList)fieldValue)
                        {
                            if (first)
                            {
                                sb.Append(NormalizeFieldValue(kvp.Key, item.ToNString(), null, false, null, ChoFieldValueJustification.None, GetFillChar(kvp.Value.FillChar)));
                                first = false;
                            }
                            else
                            {
                                sb.AppendFormat("{0}{1}", Configuration.Delimiter, NormalizeFieldValue(kvp.Key, item.ToNString(), null, false, null, ChoFieldValueJustification.None, GetFillChar(kvp.Value.FillChar)));
                            }
                        }
                        fieldText = sb.ToString();
                    }
                    else
                    {
                        fieldText = fieldValue.ToString();
                    }
                }

                if (firstColumn)
                {
                    msg.Append(NormalizeFieldValue(kvp.Key, fieldText, kvp.Value.Size, kvp.Value.Truncate, kvp.Value.QuoteField,
                                                   GetFieldValueJustification(kvp.Value.FieldValueJustification), GetFillChar(kvp.Value.FillChar),
                                                   false, kvp.Value.NullValue, kvp.Value.GetFieldValueTrimOption(kvp.Value.FieldType, Configuration.FieldValueTrimOption), fieldConfig));
                    firstColumn = false;
                }
                else
                {
                    msg.AppendFormat("{0}{1}", Configuration.Delimiter, NormalizeFieldValue(kvp.Key, fieldText, kvp.Value.Size, kvp.Value.Truncate, kvp.Value.QuoteField, GetFieldValueJustification(kvp.Value.FieldValueJustification),
                                                                                            GetFillChar(kvp.Value.FillChar), false, kvp.Value.NullValue, kvp.Value.GetFieldValueTrimOption(kvp.Value.FieldType, Configuration.FieldValueTrimOption), fieldConfig));
                }
            }

            recText = msg.ToString();
            return(true);
        }