public static bool TryConvertTo(object value, MemberInfo memberInfo, Type targetType, object sourceObject, CultureInfo culture, out object output) { output = (object)null; ChoGuard.ArgumentNotNull((object)memberInfo, "MemberInfo"); try { output = ChoConvert.ConvertTo(value, targetType, sourceObject, ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo), culture); return(true); } catch { return(false); } }
public override void Validate(object state) { if (RecordType != null) { Init(RecordType); } base.Validate(state); string[] fieldNames = null; IDictionary <string, object> yamlNode = null; if (state is Tuple <long, IDictionary <string, object> > ) { yamlNode = ((Tuple <long, IDictionary <string, object> >)state).Item2; } else { fieldNames = state as string[]; } if (AutoDiscoverColumns && YamlRecordFieldConfigurations.Count == 0) { if (RecordType != null && !IsDynamicObject && /*&& RecordType != typeof(ExpandoObject)*/ ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()).Any()) { MapRecordFields(RecordType); } else if (yamlNode != null) { Dictionary <string, ChoYamlRecordFieldConfiguration> dict = new Dictionary <string, ChoYamlRecordFieldConfiguration>(StringComparer.CurrentCultureIgnoreCase); foreach (var entry in yamlNode) { if (!dict.ContainsKey(entry.Key)) { dict.Add(entry.Key, new ChoYamlRecordFieldConfiguration(entry.Key, (string)null)); } else { throw new ChoRecordConfigurationException("Duplicate field(s) [Name(s): {0}] found.".FormatString(entry.Key)); } } foreach (ChoYamlRecordFieldConfiguration obj in dict.Values) { YamlRecordFieldConfigurations.Add(obj); } } else if (!fieldNames.IsNullOrEmpty()) { foreach (string fn in fieldNames) { if (IgnoredFields.Contains(fn)) { continue; } var obj = new ChoYamlRecordFieldConfiguration(fn, (string)null); YamlRecordFieldConfigurations.Add(obj); } } } else { foreach (var fc in YamlRecordFieldConfigurations) { fc.ComplexYamlPathUsed = !(fc.YamlPath.IsNullOrWhiteSpace() || String.Compare(fc.FieldName, fc.YamlPath, true) == 0); } } if (YamlRecordFieldConfigurations.Count <= 0) { throw new ChoRecordConfigurationException("No record fields specified."); } //Validate each record field foreach (var fieldConfig in YamlRecordFieldConfigurations) { fieldConfig.Validate(this); } //Check field position for duplicate string[] dupFields = YamlRecordFieldConfigurations.GroupBy(i => i.Name) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field(s) [Name(s): {0}] found.".FormatString(String.Join(",", dupFields))); } PIDict = new Dictionary <string, System.Reflection.PropertyInfo>(); PDDict = new Dictionary <string, PropertyDescriptor>(); foreach (var fc in YamlRecordFieldConfigurations) { var pd1 = fc.DeclaringMember.IsNullOrWhiteSpace() ? ChoTypeDescriptor.GetProperty(RecordType, fc.Name) : ChoTypeDescriptor.GetProperty(RecordType, fc.DeclaringMember); if (pd1 != null) { fc.PropertyDescriptor = pd1; } if (fc.PropertyDescriptor == null) { fc.PropertyDescriptor = TypeDescriptor.GetProperties(RecordType).AsTypedEnumerable <PropertyDescriptor>().Where(pd => pd.Name == fc.Name).FirstOrDefault(); } if (fc.PropertyDescriptor == null) { continue; } PIDict.Add(fc.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name)); PDDict.Add(fc.Name, fc.PropertyDescriptor); } RecordFieldConfigurationsDict = YamlRecordFieldConfigurations.Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name); LoadNCacheMembers(YamlRecordFieldConfigurations); }
private ChoXmlWriter <T> WithField(string name, string xPath = null, Type fieldType = null, ChoFieldValueTrimOption fieldValueTrimOption = ChoFieldValueTrimOption.Trim, bool isXmlAttribute = false, bool isAnyXmlNode = false, string fieldName = null, Func <object, object> valueConverter = null, Func <object, object> customSerializer = null, bool?isNullable = null, object defaultValue = null, object fallbackValue = null, bool encodeValue = true, string fullyQualifiedMemberName = null, string formatText = null, string nullValue = null) { if (!name.IsNullOrEmpty()) { if (!_clearFields) { ClearFields(); Configuration.MapRecordFields(Configuration.RecordType); } string fnTrim = name.NTrim(); ChoXmlRecordFieldConfiguration fc = null; PropertyDescriptor pd = null; xPath = xPath.IsNullOrWhiteSpace() ? $"/{fnTrim}" : xPath; if (Configuration.XmlRecordFieldConfigurations.Any(o => o.Name == fnTrim)) { fc = Configuration.XmlRecordFieldConfigurations.Where(o => o.Name == fnTrim).First(); Configuration.XmlRecordFieldConfigurations.Remove(fc); } else { pd = ChoTypeDescriptor.GetNestedProperty(typeof(T), fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); } var nfc = new ChoXmlRecordFieldConfiguration(fnTrim, xPath) { FieldType = fieldType, FieldValueTrimOption = fieldValueTrimOption, IsXmlAttribute = isXmlAttribute, FieldName = fieldName, ValueConverter = valueConverter, CustomSerializer = customSerializer, IsNullable = isNullable, DefaultValue = defaultValue, FallbackValue = fallbackValue, EncodeValue = encodeValue, FormatText = formatText, IsAnyXmlNode = isAnyXmlNode, NullValue = nullValue }; if (fullyQualifiedMemberName.IsNullOrWhiteSpace()) { nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd; nfc.DeclaringMember = fc != null ? fc.DeclaringMember : fullyQualifiedMemberName; } else { pd = ChoTypeDescriptor.GetNestedProperty(typeof(T), fullyQualifiedMemberName); nfc.PropertyDescriptor = pd; nfc.DeclaringMember = fullyQualifiedMemberName; } Configuration.XmlRecordFieldConfigurations.Add(nfc); } return(this); }
private ChoFixedLengthWriter <T> WithField(string name, int startIndex, int size, Type fieldType = null, bool?quoteField = null, char?fillChar = null, ChoFieldValueJustification?fieldValueJustification = null, bool truncate = true, string fieldName = null, Func <object, object> valueConverter = null, Func <dynamic, object> valueSelector = null, Func <string> headerSelector = null, object defaultValue = null, object fallbackValue = null, string fullyQualifiedMemberName = null, string formatText = null, string nullValue = null) { if (!name.IsNullOrEmpty()) { if (!_clearFields) { ClearFields(); Configuration.MapRecordFields(Configuration.RecordType); } if (fieldName.IsNullOrWhiteSpace()) { fieldName = name; } string fnTrim = name.NTrim(); ChoFixedLengthRecordFieldConfiguration fc = null; PropertyDescriptor pd = null; if (Configuration.FixedLengthRecordFieldConfigurations.Any(o => o.Name == fnTrim)) { fc = Configuration.FixedLengthRecordFieldConfigurations.Where(o => o.Name == fnTrim).First(); Configuration.FixedLengthRecordFieldConfigurations.Remove(fc); } else { pd = ChoTypeDescriptor.GetNestedProperty(typeof(T), fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); } var nfc = new ChoFixedLengthRecordFieldConfiguration(name.Trim(), startIndex, size) { FieldType = fieldType, QuoteField = quoteField, FillChar = fillChar, FieldValueJustification = fieldValueJustification, Truncate = truncate, FieldName = fieldName.IsNullOrWhiteSpace() ? name : fieldName, ValueConverter = valueConverter, ValueSelector = valueSelector, HeaderSelector = headerSelector, DefaultValue = defaultValue, FallbackValue = fallbackValue, FormatText = formatText, NullValue = nullValue }; if (fullyQualifiedMemberName.IsNullOrWhiteSpace()) { nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd; nfc.DeclaringMember = fc != null ? fc.DeclaringMember : fullyQualifiedMemberName; } else { pd = ChoTypeDescriptor.GetNestedProperty(typeof(T), fullyQualifiedMemberName); nfc.PropertyDescriptor = pd; nfc.DeclaringMember = fullyQualifiedMemberName; } if (pd != null) { if (nfc.FieldType == null) { nfc.FieldType = pd.PropertyType; } } Configuration.FixedLengthRecordFieldConfigurations.Add(nfc); } return(this); }
public override void Validate(object state) { base.Validate(state); if (Delimiter.IsNull()) { throw new ChoRecordConfigurationException("Delimiter can't be null or whitespace."); } if (Delimiter == EOLDelimiter) { throw new ChoRecordConfigurationException("Delimiter [{0}] can't be same as EODDelimiter [{1}]".FormatString(Delimiter, EOLDelimiter)); } if (Delimiter.Contains(QuoteChar)) { throw new ChoRecordConfigurationException("QuoteChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(QuoteChar, Delimiter)); } if (Comments != null && Comments.Contains(Delimiter)) { throw new ChoRecordConfigurationException("One of the Comments contains Delimiter. Not allowed."); } //Validate Header if (FileHeaderConfiguration != null) { if (FileHeaderConfiguration.FillChar != null) { ValidateChar(FileHeaderConfiguration.FillChar.Value, nameof(FileHeaderConfiguration.FillChar)); } } string[] headers = state as string[]; if (AutoDiscoverColumns && CSVRecordFieldConfigurations.Count == 0) { if (headers != null && IsDynamicObject) { int index = 0; CSVRecordFieldConfigurations = (from header in headers select new ChoCSVRecordFieldConfiguration(header, ++index)).ToList(); } else { MapRecordFields(RecordType); } } else { int maxFieldPos = CSVRecordFieldConfigurations.Max(r => r.FieldPosition); foreach (var fieldConfig in CSVRecordFieldConfigurations) { if (fieldConfig.FieldPosition > 0) { continue; } fieldConfig.FieldPosition = ++maxFieldPos; } } if (CSVRecordFieldConfigurations.Count > 0) { MaxFieldPosition = CSVRecordFieldConfigurations.Max(r => r.FieldPosition); } else { throw new ChoRecordConfigurationException("No record fields specified."); } //Validate each record field foreach (var fieldConfig in CSVRecordFieldConfigurations) { fieldConfig.Validate(this); } //Check if any field has 0 if (CSVRecordFieldConfigurations.Where(i => i.FieldPosition <= 0).Count() > 0) { throw new ChoRecordConfigurationException("Some fields contain invalid field position. All field positions must be > 0."); } //Check field position for duplicate int[] dupPositions = CSVRecordFieldConfigurations.GroupBy(i => i.FieldPosition) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupPositions.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field position(s) [Index: {0}] found.".FormatString(String.Join(",", dupPositions))); } if (!FileHeaderConfiguration.HasHeaderRecord) { } else { //Check if any field has empty names if (CSVRecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0) { throw new ChoRecordConfigurationException("Some fields has empty field name specified."); } //Check field names for duplicate string[] dupFields = CSVRecordFieldConfigurations.GroupBy(i => i.FieldName, FileHeaderConfiguration.StringComparer) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field name(s) [Name: {0}] found.".FormatString(String.Join(",", dupFields))); } } PIDict = new Dictionary <string, System.Reflection.PropertyInfo>(); PDDict = new Dictionary <string, PropertyDescriptor>(); foreach (var fc in CSVRecordFieldConfigurations) { if (fc.PropertyDescriptor == null) { fc.PropertyDescriptor = ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Name == fc.Name).FirstOrDefault(); } if (fc.PropertyDescriptor == null) { continue; } PIDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name)); PDDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor); } RecordFieldConfigurationsDict = CSVRecordFieldConfigurations.OrderBy(i => i.FieldPosition).Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name, FileHeaderConfiguration.StringComparer); RecordFieldConfigurationsDict2 = CSVRecordFieldConfigurations.OrderBy(i => i.FieldPosition).Where(i => !i.FieldName.IsNullOrWhiteSpace()).ToDictionary(i => i.FieldName, FileHeaderConfiguration.StringComparer); if (IsDynamicObject) { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => { if (kvp.Key == kvp.Value.Name) { return(kvp.Value.Name.ToValidVariableName()); } else { return(kvp.Value.Name); } }, FileHeaderConfiguration.StringComparer); } else { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Name, FileHeaderConfiguration.StringComparer); } FCArray = RecordFieldConfigurationsDict.ToArray(); LoadNCacheMembers(CSVRecordFieldConfigurations); if (Sanitize) { ValidateChar(InjectionEscapeChar, nameof(InjectionEscapeChar)); foreach (char injectionChar in InjectionChars) { ValidateChar(injectionChar, nameof(InjectionChars)); if (injectionChar.ToString().IsAlphaNumeric()) { throw new ChoRecordConfigurationException("Invalid '{0}' injection char specified.".FormatString(injectionChar)); } } } if (RecordTypeConfiguration != null) { if (RecordSelector == null && RecordTypeCodeExtractor == null) { } } }
public override void Validate(object state) { base.Validate(state); string line = null; string[] fieldNames = null; if (state is Tuple <long, string> ) { line = ((Tuple <long, string>)state).Item2; } else { fieldNames = state as string[]; } if (RecordLength <= 0 && line != null) { RecordLength = line.Length; } //Validate Header if (FileHeaderConfiguration != null) { if (FileHeaderConfiguration.FillChar != null) { if (FileHeaderConfiguration.FillChar.Value == ChoCharEx.NUL) { throw new ChoRecordConfigurationException("Invalid '{0}' FillChar specified.".FormatString(FileHeaderConfiguration.FillChar)); } if (EOLDelimiter.Contains(FileHeaderConfiguration.FillChar.Value)) { throw new ChoRecordConfigurationException("FillChar [{0}] can't be one of EOLDelimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar.Value, EOLDelimiter)); } if (Comments != null) { if ((from comm in Comments where comm.Contains(FileHeaderConfiguration.FillChar.Value.ToString()) select comm).Any()) { throw new ChoRecordConfigurationException("One of the Comments contains FillChar. Not allowed."); } } } } //string[] headers = state as string[]; if (AutoDiscoverColumns && FixedLengthRecordFieldConfigurations.Count == 0 /*&& headers != null*/) { if (RecordType != null && !IsDynamicObject && ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Attributes.OfType <ChoFixedLengthRecordFieldAttribute>().Any()).Any()) { MapRecordFields(RecordType); } else if (!line.IsNullOrEmpty()) { int index = 0; if (IsDynamicObject) { foreach (var item in DiscoverColumns(line)) { var obj = new ChoFixedLengthRecordFieldConfiguration(FileHeaderConfiguration.HasHeaderRecord ? item.Item1 : "Column{0}".FormatString(++index), item.Item2, item.Item3); FixedLengthRecordFieldConfigurations.Add(obj); } } else { Tuple <string, int, int>[] tuples = DiscoverColumns(line); foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(RecordType)) { if (index < tuples.Length) { var obj = new ChoFixedLengthRecordFieldConfiguration(FileHeaderConfiguration.HasHeaderRecord ? tuples[index].Item1 : pd.Name, tuples[index].Item2, tuples[index].Item3); FixedLengthRecordFieldConfigurations.Add(obj); index++; } else { break; } } } } else if (!fieldNames.IsNullOrEmpty()) { int startIndex = 0; int fieldLength = ChoFixedLengthFieldDefaultSizeConfiguation.Instance.GetSize(typeof(string)); foreach (string fn in fieldNames) { var obj = new ChoFixedLengthRecordFieldConfiguration(fn, startIndex, fieldLength); FixedLengthRecordFieldConfigurations.Add(obj); startIndex += fieldLength; } } } if (FixedLengthRecordFieldConfigurations.Count == 0) { throw new ChoRecordConfigurationException("No record fields specified."); } //Derive record length from fields if (RecordLength <= 0) { int maxStartIndex = FixedLengthRecordFieldConfigurations.Max(f => f.StartIndex); int maxSize = FixedLengthRecordFieldConfigurations.Where(f => f.StartIndex == maxStartIndex).Max(f1 => f1.Size.Value); var fc = FixedLengthRecordFieldConfigurations.Where(f => f.StartIndex == maxStartIndex && f.Size.Value == maxSize).FirstOrDefault(); if (fc != null) { RecordLength = fc.StartIndex + fc.Size.Value; } } if (RecordLength <= 0) { throw new ChoRecordConfigurationException("RecordLength must be > 0"); } //Check if any field has empty names if (FixedLengthRecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0) { throw new ChoRecordConfigurationException("Some fields has empty field name specified."); } //Check field names for duplicate string[] dupFields = FixedLengthRecordFieldConfigurations.GroupBy(i => i.FieldName, FileHeaderConfiguration.StringComparer) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field names [Name: {0}] specified to record fields.".FormatString(String.Join(",", dupFields))); } //Find duplicate fields with start index ChoFixedLengthRecordFieldConfiguration dupRecConfig = FixedLengthRecordFieldConfigurations.Where(c => c.Size > 0).GroupBy(i => i.StartIndex).Where(g => g.Count() > 1).Select(g => g.FirstOrDefault()).FirstOrDefault(); if (dupRecConfig != null) { throw new ChoRecordConfigurationException("Found duplicate '{0}' record field with same start index.".FormatString(dupRecConfig.FieldName)); } //Check any overlapping fields specified foreach (var f in FixedLengthRecordFieldConfigurations) { if (f.StartIndex + f.Size.Value > RecordLength) { throw new ChoRecordConfigurationException("Found '{0}' record field out of bounds of record length.".FormatString(f.FieldName)); } } PIDict = new Dictionary <string, System.Reflection.PropertyInfo>(); PDDict = new Dictionary <string, PropertyDescriptor>(); foreach (var fc in FixedLengthRecordFieldConfigurations) { if (fc.PropertyDescriptor == null) { fc.PropertyDescriptor = ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Name == fc.Name).FirstOrDefault(); } if (fc.PropertyDescriptor == null) { continue; } PIDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name)); PDDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor); } RecordFieldConfigurationsDict = FixedLengthRecordFieldConfigurations.OrderBy(i => i.StartIndex).Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name, FileHeaderConfiguration.StringComparer); RecordFieldConfigurationsDict2 = FixedLengthRecordFieldConfigurations.OrderBy(i => i.StartIndex).Where(i => !i.FieldName.IsNullOrWhiteSpace()).ToDictionary(i => i.FieldName, FileHeaderConfiguration.StringComparer); if (IsDynamicObject) { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => { if (kvp.Key == kvp.Value.Name) { return(kvp.Value.Name.ToValidVariableName()); } else { return(kvp.Value.Name); } }, FileHeaderConfiguration.StringComparer); } else { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Name, FileHeaderConfiguration.StringComparer); } //Validate each record field foreach (var fieldConfig in FixedLengthRecordFieldConfigurations) { fieldConfig.Validate(this); } if (!FileHeaderConfiguration.HasHeaderRecord) { } else { } LoadNCacheMembers(FixedLengthRecordFieldConfigurations); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var crs = Writer.ContractResolverState; if (crs == null) { var t = serializer.SerializeToJToken(value); t.WriteTo(writer); //serializer.Serialize(writer, value); return; } var fc = crs.FieldConfig; crs.Name = _fc == null?_mi.GetFullName() : crs.Name; var rec = ChoType.GetMemberObjectMatchingType(crs.Name, crs.Record); var name = ChoType.GetFieldName(crs.Name); var st = ChoType.GetMemberAttribute(_mi, typeof(ChoSourceTypeAttribute)) as ChoSourceTypeAttribute; if (st != null && st.Type != null) { _objType = st.Type; } if (_fc != null && _fc.SourceType != null) { _objType = _fc.SourceType; } if (RaiseBeforeRecordFieldWrite(rec, crs.Index, name, ref value)) { if (_fc != null) { if (value != null && _objType != null) { value = ChoConvert.ConvertTo(value, _objType, null, _fc.PropConverters, _fc.PropConverterParams, _culture); } if (_fc.CustomSerializer == null) { if (_fc.ValueConverter == null) { var t = serializer.SerializeToJToken(value); t.WriteTo(writer); } else { object retValue = _fc.ValueConverter(value); ValidateOnWrite(ref retValue); //ChoETLRecordHelper.DoMemberLevelValidation(retValue, _fc.Name, _fc, _validationMode); JToken t = JToken.FromObject(retValue, serializer); t.WriteTo(writer); } } else { object retValue = _fc.CustomSerializer(writer); ValidateOnWrite(ref retValue); JToken t = JToken.FromObject(retValue, serializer); t.WriteTo(writer); } } else { if (value != null && _objType != null) { value = ChoConvert.ConvertTo(value, _objType, null, ChoTypeDescriptor.GetTypeConverters(_mi), ChoTypeDescriptor.GetTypeConverterParams(_mi), _culture); } if (ValidateOnWrite(ref value)) { var t = serializer.SerializeToJToken(value); t.WriteTo(writer); } else { JToken t = JToken.FromObject(null, serializer); t.WriteTo(writer); } } RaiseAfterRecordFieldWrite(rec, crs.Index, name, value); } else { JToken t = JToken.FromObject(null, serializer); t.WriteTo(writer); } }
public override void Validate(object state) { if (state == null) { base.Validate(state); if (Separator.IsNullOrWhiteSpace()) { throw new ChoRecordConfigurationException("Separator can't be null or whitespace."); } if (Separator == EOLDelimiter) { throw new ChoRecordConfigurationException("Separator [{0}] can't be same as EODDelimiter [{1}]".FormatString(Separator, EOLDelimiter)); } if (Separator.Contains(QuoteChar)) { throw new ChoRecordConfigurationException("QuoteChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(QuoteChar, Separator)); } if (Comments != null && Comments.Contains(Separator)) { throw new ChoRecordConfigurationException("One of the Comments contains Delimiter. Not allowed."); } if (RecordStart.IsNullOrWhiteSpace() && RecordEnd.IsNullOrWhiteSpace()) { } else { if (RecordStart.IsNullOrWhiteSpace()) { throw new ChoRecordConfigurationException("RecordStart is missing."); } //else if (RecordEnd.IsNullOrWhiteSpace()) // RecordEnd = RecordStart; //throw new ChoRecordConfigurationException("RecordEnd is missing."); if (RecordStart.Contains("*") || RecordStart.Contains("?")) { _isWildcardComparisionOnRecordStart = true; _recordStartWildCard = new ChoWildcard(RecordStart); } if (!RecordEnd.IsNullOrWhiteSpace() && (RecordEnd.EndsWith("*") || RecordStart.Contains("?"))) { _isWildcardComparisionOnRecordEnd = true; _recordEndWildCard = new ChoWildcard(RecordEnd); } } //Validate Header if (FileHeaderConfiguration != null) { if (FileHeaderConfiguration.FillChar != null) { if (FileHeaderConfiguration.FillChar.Value == ChoCharEx.NUL) { throw new ChoRecordConfigurationException("Invalid '{0}' FillChar specified.".FormatString(FileHeaderConfiguration.FillChar)); } if (Separator.Contains(FileHeaderConfiguration.FillChar.Value)) { throw new ChoRecordConfigurationException("FillChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar, Separator)); } if (EOLDelimiter.Contains(FileHeaderConfiguration.FillChar.Value)) { throw new ChoRecordConfigurationException("FillChar [{0}] can't be one of EOLDelimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar.Value, EOLDelimiter)); } if ((from comm in Comments where comm.Contains(FileHeaderConfiguration.FillChar.Value.ToString()) select comm).Any()) { throw new ChoRecordConfigurationException("One of the Comments contains FillChar. Not allowed."); } } } } else { string[] headers = state as string[]; if (AutoDiscoverColumns && KVPRecordFieldConfigurations.Count == 0) { AutoDiscoveredColumns = true; if (headers != null && IsDynamicObject) { KVPRecordFieldConfigurations = (from header in headers where !IgnoredFields.Contains(header) select new ChoKVPRecordFieldConfiguration(header)).ToList(); } else { MapRecordFields(RecordType); } } if (KVPRecordFieldConfigurations.Count <= 0) { throw new ChoRecordConfigurationException("No record fields specified."); } //Validate each record field foreach (var fieldConfig in KVPRecordFieldConfigurations) { fieldConfig.Validate(this); } //Check if any field has empty names if (KVPRecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0) { throw new ChoRecordConfigurationException("Some fields has empty field name specified."); } //Check field names for duplicate string[] dupFields = KVPRecordFieldConfigurations.GroupBy(i => i.FieldName, FileHeaderConfiguration.StringComparer) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0 /* && !IgnoreDuplicateFields */) { throw new ChoRecordConfigurationException("Duplicate field name(s) [Name: {0}] found.".FormatString(String.Join(",", dupFields))); } PIDict = new Dictionary <string, System.Reflection.PropertyInfo>(FileHeaderConfiguration.StringComparer); PDDict = new Dictionary <string, PropertyDescriptor>(FileHeaderConfiguration.StringComparer); foreach (var fc in KVPRecordFieldConfigurations) { var pd1 = fc.DeclaringMember.IsNullOrWhiteSpace() ? ChoTypeDescriptor.GetProperty(RecordType, fc.Name) : ChoTypeDescriptor.GetProperty(RecordType, fc.DeclaringMember); if (pd1 != null) { fc.PropertyDescriptor = pd1; } if (fc.PropertyDescriptor == null) { fc.PropertyDescriptor = TypeDescriptor.GetProperties(RecordType).AsTypedEnumerable <PropertyDescriptor>().Where(pd => pd.Name == fc.Name).FirstOrDefault(); } if (fc.PropertyDescriptor == null) { continue; } PIDict.Add(fc.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name)); PDDict.Add(fc.Name, fc.PropertyDescriptor); } RecordFieldConfigurationsDict = KVPRecordFieldConfigurations.Where(i => !i.Name.IsNullOrWhiteSpace()).GroupBy(i => i.Name).Select(g => g.First()).ToDictionary(i => i.Name, FileHeaderConfiguration.StringComparer); RecordFieldConfigurationsDict2 = KVPRecordFieldConfigurations.Where(i => !i.FieldName.IsNullOrWhiteSpace()).GroupBy(i => i.Name).Select(g => g.First()).ToDictionary(i => i.FieldName, FileHeaderConfiguration.StringComparer); if (IsDynamicObject) { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => { if (kvp.Key == kvp.Value.Name) { return(kvp.Value.Name.ToValidVariableName()); } else { return(kvp.Value.Name); } }, kvp => kvp.Key, FileHeaderConfiguration.StringComparer); } else { AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Name, FileHeaderConfiguration.StringComparer); } FCArray = RecordFieldConfigurationsDict.ToArray(); LoadNCacheMembers(KVPRecordFieldConfigurations); } }
internal void WithField(string name, string jsonPath = null, Type fieldType = null, ChoFieldValueTrimOption fieldValueTrimOption = ChoFieldValueTrimOption.Trim, string fieldName = null, Func <object, object> valueConverter = null, Func <object, object> itemConverter = null, Func <object, object> customSerializer = null, object defaultValue = null, object fallbackValue = null, string fullyQualifiedMemberName = null, string formatText = null, bool?isArray = null, string nullValue = null, Type recordType = null, Type subRecordType = null, Func <JObject, Type> fieldTypeSelector = null) { ChoGuard.ArgumentNotNull(recordType, nameof(recordType)); if (!name.IsNullOrEmpty()) { if (subRecordType != null) { MapRecordFieldsForType(subRecordType); } string fnTrim = fieldName.IsNullOrWhiteSpace() ? name.NTrim() : fieldName; ChoJSONRecordFieldConfiguration fc = null; PropertyDescriptor pd = null; if (JSONRecordFieldConfigurations.Any(o => o.FieldName == fnTrim)) { fc = JSONRecordFieldConfigurations.Where(o => o.FieldName == fnTrim).First(); JSONRecordFieldConfigurations.Remove(fc); pd = ChoTypeDescriptor.GetNestedProperty(recordType, fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); } else if (subRecordType != null) { pd = ChoTypeDescriptor.GetNestedProperty(subRecordType, fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); } else { pd = ChoTypeDescriptor.GetNestedProperty(recordType, fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); } var nfc = new ChoJSONRecordFieldConfiguration(fnTrim, pd != null ? ChoTypeDescriptor.GetPropetyAttribute <ChoJSONRecordFieldAttribute>(pd) : null, pd != null ? pd.Attributes.OfType <Attribute>().ToArray() : null) { }; nfc.JSONPath = !jsonPath.IsNullOrWhiteSpace() ? jsonPath : nfc.JSONPath; nfc.FieldType = fieldType != null ? fieldType : nfc.FieldType; nfc.FieldValueTrimOption = fieldValueTrimOption; nfc.FieldName = fieldName.IsNullOrWhiteSpace() ? (name.IsNullOrWhiteSpace() ? nfc.FieldName : name) : fieldName; nfc.ValueConverter = valueConverter != null ? valueConverter : nfc.ValueConverter; nfc.CustomSerializer = customSerializer != null ? customSerializer : nfc.CustomSerializer; nfc.DefaultValue = defaultValue != null ? defaultValue : nfc.DefaultValue; nfc.FallbackValue = fallbackValue != null ? fallbackValue : nfc.FallbackValue; nfc.FormatText = !formatText.IsNullOrWhiteSpace() ? formatText : nfc.FormatText; nfc.ItemConverter = itemConverter != null ? itemConverter : nfc.ItemConverter; nfc.IsArray = isArray != null ? isArray : nfc.IsArray; nfc.NullValue = !nullValue.IsNullOrWhiteSpace() ? nullValue : nfc.NullValue; nfc.FieldTypeSelector = fieldTypeSelector != null ? fieldTypeSelector : nfc.FieldTypeSelector; if (fullyQualifiedMemberName.IsNullOrWhiteSpace()) { nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd; nfc.DeclaringMember = fc != null ? fc.DeclaringMember : fullyQualifiedMemberName; } else { if (subRecordType == null) { pd = ChoTypeDescriptor.GetNestedProperty(recordType, fullyQualifiedMemberName); } else { pd = ChoTypeDescriptor.GetNestedProperty(subRecordType, fullyQualifiedMemberName); } nfc.PropertyDescriptor = pd; nfc.DeclaringMember = fullyQualifiedMemberName; } if (pd != null) { if (nfc.FieldType == null) { nfc.FieldType = pd.PropertyType; } } if (subRecordType == null) { JSONRecordFieldConfigurations.Add(nfc); } else { AddFieldForType(subRecordType, nfc); } } }
public static object ConvertFrom(object value, Type targetType, object sourceObject = null, object[] converters = null, object[] parameters = null, CultureInfo culture = null) { Type origType = targetType; targetType = targetType.IsNullableType() ? targetType.GetUnderlyingType() : targetType; object obj1 = value; if (targetType == (Type)null) { return(value); } if (targetType == typeof(object)) { return(value); } if (culture == null) { culture = ChoConvert.DefaultCulture; } if (value is ICollection && !typeof(ICollection).IsAssignableFrom(targetType)) { value = ((IEnumerable)value).FirstOrDefault <object>(); } if (typeof(IList).IsAssignableFrom(targetType) && !(value is IList)) { value = new object[] { value }; } Type type = value == null ? typeof(object) : value.GetType(); try { if (converters.IsNullOrEmpty()) { converters = ChoTypeDescriptor.GetTypeConvertersForType(targetType); } if (converters != null && converters.Length > 0) { object[] objArray = (object[])null; for (int index = 0; index < converters.Length; ++index) { object obj2 = converters[index]; if (parameters != null && parameters.Length > 0) { objArray = parameters[index] as object[]; } if (obj2 is TypeConverter) { TypeConverter typeConverter = obj2 as TypeConverter; if (typeConverter.CanConvertFrom(type)) { value = typeConverter.ConvertFrom((ITypeDescriptorContext)null, culture, value); } } else if (obj2 is IValueConverter) { value = ((IValueConverter)obj2).Convert(value, targetType, (object)objArray, culture); } else if (obj2 is IChoValueConverter) { value = ((IChoValueConverter)obj2).Convert(value, targetType, (object)objArray, culture); } } //if (value != obj1) // return value; } if (value == null) { return(origType.Default()); } if (targetType.IsAssignableFrom(value.GetType()) || targetType == value.GetType()) { return(value); } if (value is IConvertible) { try { value = Convert.ChangeType(value, targetType, (IFormatProvider)culture); if (obj1 != value) { return(value); } } catch { } } if (ChoConvert.TryConvertXPlicit(value, targetType, "op_Explicit", ref value) || ChoConvert.TryConvertXPlicit(value, targetType, "op_Implicit", ref value)) { return(value); } object convValue = null; if (origType.IsNullableType()) { return(null); } else if (ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out convValue)) { return(convValue); } if (value is Array && typeof(IList).IsAssignableFrom(targetType)) { if (typeof(Array).IsAssignableFrom(targetType)) { MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertToArray", BindingFlags.NonPublic | BindingFlags.Static); MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() }); return(generic.Invoke(null, new object[] { value })); } else { MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertToList", BindingFlags.NonPublic | BindingFlags.Static); MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() }); return(generic.Invoke(null, new object[] { value })); } } else if (value is IList && typeof(Array).IsAssignableFrom(targetType)) { if (typeof(Array).IsAssignableFrom(targetType)) { MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertListToArray", BindingFlags.NonPublic | BindingFlags.Static); MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() }); return(generic.Invoke(null, new object[] { value })); } else { MethodInfo convertMethod = typeof(ChoConvert).GetMethod("ConvertListToList", BindingFlags.NonPublic | BindingFlags.Static); MethodInfo generic = convertMethod.MakeGenericMethod(new[] { targetType.GetItemType() }); return(generic.Invoke(null, new object[] { value })); } } throw new ApplicationException("Object conversion failed."); } catch (Exception ex) { if (type.IsSimple()) { throw new ApplicationException(string.Format("Can't convert '{2}' value from '{0}' type to '{1}' type.", (object)type, (object)targetType, value), ex); } throw new ApplicationException(string.Format("Can't convert object from '{0}' type to '{1}' type.", (object)type, (object)targetType), ex); } }
protected virtual void LoadNCacheMembers(IEnumerable <ChoRecordFieldConfiguration> fcs) { if (!IsDynamicObject) { string name = null; object defaultValue = null; object fallbackValue = null; foreach (var fc in fcs) { //if (fc is ChoFileRecordFieldConfiguration) // name = ((ChoFileRecordFieldConfiguration)fc).FieldName; //else name = fc.Name; if (!PDDict.ContainsKey(name)) { continue; } fc.PD = PDDict[name]; fc.PI = PIDict[name]; //Load default value defaultValue = ChoType.GetRawDefaultValue(PDDict[name]); if (defaultValue != null) { fc.DefaultValue = defaultValue; fc.IsDefaultValueSpecified = true; } //Load fallback value fallbackValue = ChoType.GetRawFallbackValue(PDDict[name]); if (fallbackValue != null) { fc.FallbackValue = fallbackValue; fc.IsFallbackValueSpecified = true; } //Load Converters fc.PropConverters = ChoTypeDescriptor.GetTypeConverters(fc.PI); fc.PropConverterParams = ChoTypeDescriptor.GetTypeConverterParams(fc.PI); } PropertyNames = PDDict.Keys.ToArray(); } //Validators HasConfigValidators = (from fc in fcs where fc.Validators != null select fc).FirstOrDefault() != null; if (!HasConfigValidators) { if (!IsDynamicObject) { string name = null; foreach (var fc in fcs) { if (fc is ChoFileRecordFieldConfiguration) { name = ((ChoFileRecordFieldConfiguration)fc).FieldName; } else { name = fc.Name; } if (!PDDict.ContainsKey(name)) { continue; } fc.Validators = ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(fc.PD).ToArray(); } } } ValDict = (from fc in fcs select new KeyValuePair <string, ValidationAttribute[]>(fc is ChoFileRecordFieldConfiguration ? ((ChoFileRecordFieldConfiguration)fc).FieldName : fc.Name, fc.Validators)) .GroupBy(i => i.Key).Select(g => g.First()).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); }
public static object ConvertFrom(object value, PropertyInfo propertyInfo, object sourceObject = null, CultureInfo culture = null) { ChoGuard.ArgumentNotNull(propertyInfo, "PropertyInfo"); return(ChoConvert.ConvertFrom(value, propertyInfo.PropertyType, sourceObject, ChoTypeDescriptor.GetTypeConverters(propertyInfo), ChoTypeDescriptor.GetTypeConverterParams(propertyInfo), culture)); }
public static object ConvertFrom(object value, MemberInfo memberInfo, object sourceObject = null, CultureInfo culture = null) { ChoGuard.ArgumentNotNull(memberInfo, "MemberInfo"); return(ChoConvert.ConvertFrom(value, ChoType.GetMemberType(memberInfo), sourceObject, ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo), culture)); }
public static object ConvertTo(object value, Type targetType, object sourceObject, object[] converters, object[] parameters, CultureInfo culture) { Type origType = targetType; targetType = targetType.IsNullableType() ? targetType.GetUnderlyingType() : targetType; object obj1 = value; if (targetType == (Type)null) { return(value); } //if (targetType == typeof(object)) // return value; if (culture == null) { culture = ChoConvert.DefaultCulture; } Type type = value == null ? typeof(object) : value.GetType().GetUnderlyingType(); try { object[] objArray = (object[])null; if (converters.IsNullOrEmpty()) { converters = ChoTypeDescriptor.GetTypeConvertersForType(type); } if (converters != null && converters.Length > 0) { for (int index = 0; index < converters.Length; ++index) { object obj2 = converters[index]; if (parameters != null && parameters.Length > 0) { objArray = parameters[index] as object[]; } if (obj2 is TypeConverter) { TypeConverter typeConverter = obj2 as TypeConverter; if (typeConverter.CanConvertTo(targetType)) { value = typeConverter.ConvertTo((ITypeDescriptorContext)null, culture, value, targetType); } } else if (obj2 is IValueConverter) { value = ((IValueConverter)obj2).ConvertBack(value, targetType, (object)objArray, culture); } else if (obj2 is IChoValueConverter) { value = ((IChoValueConverter)obj2).ConvertBack(value, targetType, (object)objArray, culture); } } if (obj1 != value) { return(value); } } if (value == null) { return(origType.Default()); } if (type == origType) { return(value); } if (targetType.IsAssignableFrom(value.GetType()) || targetType == value.GetType()) { return(value); } if (value is IConvertible) { try { value = Convert.ChangeType(value, targetType, (IFormatProvider)culture); if (obj1 != value) { return(value); } } catch { } } if (ChoConvert.TryConvertXPlicit(value, targetType, "op_Explicit", ref value) || ChoConvert.TryConvertXPlicit(value, targetType, "op_Implicit", ref value)) { //|| (!origType.IsNullableType() && ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out value))) // || ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out value)) return(value); } if (targetType == typeof( ChoDynamicObject)) { dynamic ret = new ChoDynamicObject(); ret.Value = value; return(ret); } object result = null; if (origType.IsNullableType()) { return(null); } else if (ChoConvert.TryConvertToSpecialValues(value, targetType, culture, out result)) { return(result); } throw new ApplicationException("Object conversion failed."); } catch (Exception ex) { if (type.IsSimple()) { throw new ApplicationException(string.Format("Can't convert '{2}' value from '{0}' type to '{1}' type.", (object)type, (object)targetType, value), ex); } throw new ApplicationException(string.Format("Can't convert object from '{0}' type to '{1}' type.", (object)type, (object)targetType), ex); } }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { StreamWriter sw = writer as StreamWriter; ChoGuard.ArgumentNotNull(sw, "StreamWriter"); if (records == null) { yield break; } if (!RaiseBeginWrite(sw)) { yield break; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; string recText = String.Empty; try { int index = 0; foreach (object record in records) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(++index)); } recText = String.Empty; _index++; if (record != null) { if (predicate == null || predicate(record)) { //Discover and load CSV columns from first record if (!_configCheckDone) { string[] fieldNames = null; if (record is ExpandoObject) { var dict = record as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoCSVRecordFieldAttribute>(record.GetType()).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(record.GetType()).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); WriteHeaderLine(sw); _configCheckDone = true; } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } else if (recText.Length > 0) { sw.Write("{1}{0}", recText, Configuration.FileHeaderConfiguration.HasHeaderRecord || HasExcelSeparator ? Configuration.EOLDelimiter : ""); continue; } try { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidatation(Configuration.CSVRecordFieldConfigurations.Cast <ChoRecordFieldConfiguration>().ToArray()); } if (ToText(_index, record, out recText)) { if (_index == 1) { sw.Write("{1}{0}", recText, Configuration.FileHeaderConfiguration.HasHeaderRecord || HasExcelSeparator ? Configuration.EOLDelimiter : ""); } else { sw.Write("{1}{0}", recText, Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } catch (ChoParserException) { throw; } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } } else { throw; } } } } yield return(record); } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } RaiseEndWrite(sw); }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { TextWriter sw = writer as TextWriter; ChoGuard.ArgumentNotNull(sw, "TextWriter"); if (records == null) { yield break; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; _se = new Lazy <XmlSerializer>(() => Configuration.XmlSerializer == null ? null : Configuration.XmlSerializer); string recText = String.Empty; try { foreach (object record in records) { _index++; if (TraceSwitch.TraceVerbose) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(_index)); } } recText = String.Empty; if (predicate == null || predicate(record)) { //Discover and load Xml columns from first record if (!_configCheckDone) { if (record == null) { continue; } string[] fieldNames = null; Type recordType = ElementType == null?record.GetType() : ElementType; if (typeof(ICollection).IsAssignableFrom(recordType)) { recordType = recordType.GetEnumerableItemType().GetUnderlyingType(); } else { recordType = recordType.GetUnderlyingType(); } Configuration.IsDynamicObject = recordType.IsDynamicType(); if (!Configuration.IsDynamicObject) { if (recordType.IsSimple()) { Configuration.RecordType = typeof(ChoScalarObject <>).MakeGenericType(recordType); } else { Configuration.RecordType = recordType; } } if (Configuration.IsDynamicObject) { var dict = record.ToDynamicObject() as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoXmlNodeRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); _configCheckDone = true; if (!RaiseBeginWrite(sw)) { yield break; } sw.Write("<{0}{1}>".FormatString(Configuration.RootName, GetNamespaceText())); } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } try { if (!Configuration.UseXmlSerialization) { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidation(Configuration, Configuration.XmlRecordFieldConfigurations); } if (ToText(_index, record, out recText)) { if (!recText.IsNullOrEmpty()) { sw.Write("{1}{0}", recText, Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } else { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.Off) != ChoObjectValidationMode.Off) { record.DoObjectLevelValidation(Configuration, Configuration.XmlRecordFieldConfigurations); } if (record != null) { if (_se.Value != null) { _se.Value.Serialize(sw, record); } else { sw.Write("{1}{0}", ChoUtility.XmlSerialize(record).Indent(2, Configuration.IndentChar.ToString()), Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, null)) { yield break; } } } } //catch (ChoParserException) //{ // throw; //} catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } } else { throw; } } } yield return(record); if (Configuration.NotifyAfter > 0 && _index % Configuration.NotifyAfter == 0) { if (RaisedRowsWritten(_index)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); yield break; } } } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } }
private static SqlCommand CreateInsertCommand(object target, string tableName, SqlConnection conn, SqlTransaction trans, Dictionary <string, PropertyInfo> PIDict = null) { Type objectType = target is Type ? target as Type : target.GetType(); StringBuilder script = new StringBuilder(); if (target is ExpandoObject) { tableName = tableName.IsNullOrWhiteSpace() ? "Table" : tableName; var eo = target as IDictionary <string, Object>; if (eo.Count == 0) { throw new InvalidDataException("No properties found in expando object."); } script.Append("INSERT INTO [" + tableName); script.Append("] ("); bool isFirst = true; foreach (KeyValuePair <string, object> kvp in eo) { if (isFirst) { script.AppendFormat("[{0}]", kvp.Key); isFirst = false; } else { script.AppendFormat(", [{0}]", kvp.Key); } } script.Append(") VALUES ("); isFirst = true; foreach (KeyValuePair <string, object> kvp in eo) { if (isFirst) { script.AppendFormat("@{0}", kvp.Key); isFirst = false; } else { script.AppendFormat(", @{0}", kvp.Key); } } script.AppendLine(")"); SqlCommand command2 = new SqlCommand(script.ToString(), conn, trans); foreach (KeyValuePair <string, object> kvp in eo) { command2.Parameters.AddWithValue("@{0}".FormatString(kvp.Key), kvp.Value == null ? DBNull.Value : kvp.Value); } return(command2); } else { if (!ChoTypeDescriptor.GetProperties(objectType).Any()) { throw new InvalidDataException("No properties found in '{0}' object.".FormatString(objectType.Name)); } object pv = null; script.Append("INSERT INTO [" + tableName); script.Append("] ("); bool isFirst = true; foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(objectType)) { if (isFirst) { script.AppendFormat("[{0}]", pd.Name); isFirst = false; } else { script.AppendFormat(", [{0}]", pd.Name); } } script.Append(") VALUES ("); isFirst = true; foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(objectType)) { if (isFirst) { script.AppendFormat("@{0}", pd.Name); isFirst = false; } else { script.AppendFormat(", @{0}", pd.Name); } } script.AppendLine(")"); SqlCommand command2 = new SqlCommand(script.ToString(), conn, trans); foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(objectType)) { pv = PIDict[pd.Name].GetValue(target); command2.Parameters.AddWithValue("@{0}".FormatString(pd.Name), pv == null ? DBNull.Value : pv); } return(command2); } }
private void DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false) { if (recordType == null) { return; } if (!recordType.IsDynamicType()) { IsComplexXPathUsed = false; Type pt = null; if (optIn) //ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType<ChoXmlNodeRecordFieldAttribute>().Any()).Any()) { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { pt = pd.PropertyType.GetUnderlyingType(); var fa = pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().FirstOrDefault(); bool optIn1 = fa == null || fa.UseXmlSerialization ? optIn : ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()).Any(); if (false) //optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt)) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1); } else if (pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()) { bool useCache = true; string xpath = null; ChoXmlNodeRecordFieldAttribute attr = ChoTypeDescriptor.GetPropetyAttribute <ChoXmlNodeRecordFieldAttribute>(pd); if (attr.XPath.IsNullOrEmpty()) { if (!attr.FieldName.IsNullOrWhiteSpace()) { attr.XPath = $"/{attr.FieldName}|/@{attr.FieldName}"; } else { attr.XPath = xpath = $"/{pd.Name}|/@{pd.Name}"; } IsComplexXPathUsed = true; } else { useCache = false; } var obj = new ChoXmlRecordFieldConfiguration(pd.Name, attr, pd.Attributes.OfType <Attribute>().ToArray()); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name); obj.UseCache = useCache; if (obj.XPath.IsNullOrWhiteSpace()) { if (!obj.FieldName.IsNullOrWhiteSpace()) { obj.XPath = $"/{obj.FieldName}|/@{obj.FieldName}"; } else { obj.XPath = $"/{obj.Name}|/@{obj.Name}"; } } obj.FieldType = pd.PropertyType.GetUnderlyingType(); if (!XmlRecordFieldConfigurations.Any(c => c.Name == pd.Name)) { XmlRecordFieldConfigurations.Add(obj); } } } } else { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { XmlIgnoreAttribute xiAttr = pd.Attributes.OfType <XmlIgnoreAttribute>().FirstOrDefault(); if (xiAttr != null) { continue; } pt = pd.PropertyType.GetUnderlyingType(); if (false) //pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt)) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn); } else { var obj = new ChoXmlRecordFieldConfiguration(pd.Name, $"/{pd.Name}|/@{pd.Name}"); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name); StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { obj.Size = slAttr.MaximumLength; } XmlElementAttribute xAttr = pd.Attributes.OfType <XmlElementAttribute>().FirstOrDefault(); if (xAttr != null && !xAttr.ElementName.IsNullOrWhiteSpace()) { obj.FieldName = xAttr.ElementName; } else { XmlAttributeAttribute xaAttr = pd.Attributes.OfType <XmlAttributeAttribute>().FirstOrDefault(); if (xAttr != null && !xaAttr.AttributeName.IsNullOrWhiteSpace()) { obj.FieldName = xaAttr.AttributeName; } else { DisplayNameAttribute dnAttr = pd.Attributes.OfType <DisplayNameAttribute>().FirstOrDefault(); if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace()) { obj.FieldName = dnAttr.DisplayName.Trim(); } else { DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (dpAttr != null) { if (!dpAttr.ShortName.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.ShortName; } else if (!dpAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.Name; } } } } } DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { obj.FormatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { obj.NullValue = dfAttr.NullDisplayText; } if (!XmlRecordFieldConfigurations.Any(c => c.Name == pd.Name)) { XmlRecordFieldConfigurations.Add(obj); } } } } } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { object retValue = null; var crs = Reader.ContractResolverState; if (crs == null) { return(serializer.Deserialize(reader, objectType)); } var fc = crs.FieldConfig; crs.Name = _fc == null?_mi.GetFullName() : crs.Name; var rec = ChoType.GetMemberObjectMatchingType(crs.Name, crs.Record); var name = ChoType.GetFieldName(crs.Name); var st = ChoType.GetMemberAttribute(_mi, typeof(ChoSourceTypeAttribute)) as ChoSourceTypeAttribute; if (st != null && st.Type != null) { _objType = st.Type; } if (_fc != null && _fc.SourceType != null) { _objType = _fc.SourceType; } retValue = reader; if (!RaiseBeforeRecordFieldLoad(rec, crs.Index, name, ref retValue)) { if (_fc != null) { if (_fc.CustomSerializer == null) { if (_fc.ValueConverter == null) { retValue = serializer.Deserialize(reader, objectType); } else { retValue = _fc.ValueConverter(serializer.Deserialize(reader, typeof(string))); } } else { retValue = _fc.CustomSerializer(reader); } ValidateORead(ref retValue); //ChoETLRecordHelper.DoMemberLevelValidation(retValue, _fc.Name, _fc, _validationMode); if (retValue != null) { retValue = ChoConvert.ConvertFrom(retValue, objectType, null, _fc.PropConverters, _fc.PropConverterParams, _culture); } } else { retValue = serializer.Deserialize(reader, objectType); ValidateORead(ref retValue); if (retValue != null) { retValue = ChoConvert.ConvertFrom(retValue, objectType, null, ChoTypeDescriptor.GetTypeConverters(_mi), ChoTypeDescriptor.GetTypeConverterParams(_mi), _culture); } } } if (!RaiseAfterRecordFieldLoad(rec, crs.Index, name, retValue)) { return(null); } return(retValue == reader?serializer.Deserialize(reader, objectType) : retValue); }
public override void Validate(object state) { base.Validate(state); //if (XPath.IsNull()) // throw new ChoRecordConfigurationException("XPath can't be null or whitespace."); if (XPath.IsNullOrWhiteSpace()) { if (!IsDynamicObject && (RecordType.IsGenericType && RecordType.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))) { NodeName = NodeName.IsNullOrWhiteSpace() ? "KeyValuePair" : NodeName; RootName = RootName.IsNullOrWhiteSpace() ? "KeyValuePairs" : RootName; } else if (!IsDynamicObject && !typeof(IChoScalarObject).IsAssignableFrom(RecordType)) { NodeName = NodeName.IsNullOrWhiteSpace() ? RecordType.Name : NodeName; RootName = RootName.IsNullOrWhiteSpace() ? NodeName.ToPlural() : RootName; } } else { RootName = RootName.IsNullOrWhiteSpace() ? XPath.SplitNTrim("/").Where(t => !t.IsNullOrWhiteSpace() && t.NTrim() != "." && t.NTrim() != ".." && t.NTrim() != "*").FirstOrDefault() : RootName; NodeName = NodeName.IsNullOrWhiteSpace() ? XPath.SplitNTrim("/").Where(t => !t.IsNullOrWhiteSpace() && t.NTrim() != "." && t.NTrim() != ".." && t.NTrim() != "*").Skip(1).FirstOrDefault() : NodeName; } string rootName = null; string nodeName = null; ChoXmlDocumentRootAttribute da = TypeDescriptor.GetAttributes(RecordType).OfType <ChoXmlDocumentRootAttribute>().FirstOrDefault(); if (da != null) { rootName = da.Name; } else { XmlRootAttribute ra = TypeDescriptor.GetAttributes(RecordType).OfType <XmlRootAttribute>().FirstOrDefault(); if (ra != null) { nodeName = ra.ElementName; } } RootName = RootName.IsNullOrWhiteSpace() && !rootName.IsNullOrWhiteSpace() ? rootName : RootName; NodeName = NodeName.IsNullOrWhiteSpace() && !nodeName.IsNullOrWhiteSpace() ? nodeName : NodeName; RootName = RootName.IsNullOrWhiteSpace() && !NodeName.IsNullOrWhiteSpace() ? NodeName.ToPlural() : RootName; if (!RootName.IsNullOrWhiteSpace() && RootName.ToSingular() != RootName) { NodeName = NodeName.IsNullOrWhiteSpace() && !RootName.IsNullOrWhiteSpace() ? RootName.ToSingular() : NodeName; } if (RootName.IsNullOrWhiteSpace()) { RootName = "Root"; } if (NodeName.IsNullOrWhiteSpace()) { NodeName = "XElement"; } //Encode Root and node names RootName = System.Net.WebUtility.HtmlEncode(RootName); NodeName = System.Net.WebUtility.HtmlEncode(NodeName); string[] fieldNames = null; XElement xpr = null; if (state is Tuple <long, XElement> ) { xpr = ((Tuple <long, XElement>)state).Item2; } else { fieldNames = state as string[]; } if (AutoDiscoverColumns && XmlRecordFieldConfigurations.Count == 0) { if (RecordType != null && !IsDynamicObject && ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()).Any()) { DiscoverRecordFields(RecordType); } else if (xpr != null) { XmlRecordFieldConfigurations.AddRange(DiscoverRecordFieldsFromXElement(xpr)); } else if (!fieldNames.IsNullOrEmpty()) { foreach (string fn in fieldNames) { if (IgnoredFields.Contains(fn)) { continue; } if (fn.StartsWith("_")) { string fn1 = fn.Substring(1); var obj = new ChoXmlRecordFieldConfiguration(fn, xPath: $"./{fn1}"); obj.FieldName = fn1; obj.IsXmlAttribute = true; XmlRecordFieldConfigurations.Add(obj); } else if (fn.EndsWith("_")) { string fn1 = fn.Substring(0, fn.Length - 1); var obj = new ChoXmlRecordFieldConfiguration(fn, xPath: $"./{fn1}"); obj.FieldName = fn1; obj.IsXmlCDATA = true; XmlRecordFieldConfigurations.Add(obj); } else { var obj = new ChoXmlRecordFieldConfiguration(fn, xPath: $"./{fn}"); XmlRecordFieldConfigurations.Add(obj); } } } } else { IsComplexXPathUsed = false; foreach (var fc in XmlRecordFieldConfigurations) { if (fc.IsArray == null) { fc.IsArray = typeof(ICollection).IsAssignableFrom(fc.FieldType); } if (fc.FieldName.IsNullOrWhiteSpace()) { fc.FieldName = fc.Name; } if (fc.XPath.IsNullOrWhiteSpace()) { fc.XPath = $"/{fc.FieldName}|/@{fc.FieldName}"; } else { if (fc.XPath == fc.FieldName || fc.XPath == $"/{fc.FieldName}" || fc.XPath == $"/{fc.FieldName}" || fc.XPath == $"/{fc.FieldName}" || fc.XPath == $"/@{fc.FieldName}" || fc.XPath == $"/@{fc.FieldName}" || fc.XPath == $"/@{fc.FieldName}" ) { } else { IsComplexXPathUsed = true; fc.UseCache = false; } } } } if (XmlRecordFieldConfigurations.Count <= 0) { throw new ChoRecordConfigurationException("No record fields specified."); } //Validate each record field foreach (var fieldConfig in XmlRecordFieldConfigurations) { fieldConfig.Validate(this); } //Check field position for duplicate string[] dupFields = XmlRecordFieldConfigurations.GroupBy(i => i.Name) .Where(g => g.Count() > 1) .Select(g => g.Key).ToArray(); if (dupFields.Length > 0) { throw new ChoRecordConfigurationException("Duplicate field(s) [Name(s): {0}] found.".FormatString(String.Join(",", dupFields))); } PIDict = new Dictionary <string, System.Reflection.PropertyInfo>(); PDDict = new Dictionary <string, PropertyDescriptor>(); foreach (var fc in XmlRecordFieldConfigurations) { var pd1 = fc.DeclaringMember.IsNullOrWhiteSpace() ? ChoTypeDescriptor.GetProperty(RecordType, fc.Name) : ChoTypeDescriptor.GetProperty(RecordType, fc.DeclaringMember); if (pd1 != null) { fc.PropertyDescriptor = pd1; } if (fc.PropertyDescriptor == null) { fc.PropertyDescriptor = TypeDescriptor.GetProperties(RecordType).AsTypedEnumerable <PropertyDescriptor>().Where(pd => pd.Name == fc.Name).FirstOrDefault(); } if (fc.PropertyDescriptor == null) { continue; } PIDict.Add(fc.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name)); PDDict.Add(fc.Name, fc.PropertyDescriptor); } RecordFieldConfigurationsDict = XmlRecordFieldConfigurations.OrderBy(c => c.IsXmlAttribute).Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name); if (XmlRecordFieldConfigurations.Where(e => e.IsNullable).Any() || NullValueHandling == ChoNullValueHandling.Default) { if (NamespaceManager != null) { if (!NamespaceManager.HasNamespace("xsi")) { NamespaceManager.AddNamespace("xsi", ChoXmlSettings.XmlSchemaInstanceNamespace); } if (!NamespaceManager.HasNamespace("xsd")) { NamespaceManager.AddNamespace("xsd", ChoXmlSettings.XmlSchemaNamespace); } } } LoadNCacheMembers(XmlRecordFieldConfigurations); }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { var property = base.CreateProperty(member, memberSerialization); var propertyFullName = member.GetFullName(); if (IsIgnored(property.DeclaringType, property.PropertyName, property.UnderlyingName, propertyFullName)) { property.ShouldSerialize = i => false; property.Ignored = true; } if (IsRenamed(property.DeclaringType, property.PropertyName, property.UnderlyingName, propertyFullName, out var newJsonPropertyName)) { if (!newJsonPropertyName.IsNullOrWhiteSpace()) { property.PropertyName = newJsonPropertyName; } } if (_configuration.ContainsRecordConfigForType(property.DeclaringType)) { var dict = _configuration.GetRecordConfigDictionaryForType(property.DeclaringType); if (dict != null && dict.ContainsKey(property.UnderlyingName)) { property.Converter = property.MemberConverter = new ChoContractResolverJsonConverter(dict[property.UnderlyingName] as ChoFileRecordFieldConfiguration, _configuration.Culture, property.PropertyType, _configuration.ObjectValidationMode, member) { Configuration = _configuration as ChoFileRecordConfiguration, Reader = Reader, CallbackRecordFieldRead = CallbackRecordFieldRead, Writer = Writer, CallbackRecordFieldWrite = CallbackRecordFieldWrite }; } } else if (_configuration.RecordFieldConfigurations.Any(f => f.DeclaringMember == propertyFullName)) { var fc = _configuration.RecordFieldConfigurations.First(f => f.DeclaringMember == propertyFullName) as ChoFileRecordFieldConfiguration; property.Converter = property.MemberConverter = new ChoContractResolverJsonConverter(fc, _configuration.Culture, property.PropertyType, _configuration.ObjectValidationMode, member) { Configuration = _configuration as ChoFileRecordConfiguration, Reader = Reader, CallbackRecordFieldRead = CallbackRecordFieldRead, Writer = Writer, CallbackRecordFieldWrite = CallbackRecordFieldWrite }; property.DefaultValue = fc.DefaultValue; property.Order = fc.Order; } else if (_configuration.RecordFieldConfigurations.Any(f => f.Name == propertyFullName)) { var fc = _configuration.RecordFieldConfigurations.First(f => f.Name == propertyFullName) as ChoFileRecordFieldConfiguration; property.MemberConverter = new ChoContractResolverJsonConverter(fc, _configuration.Culture, property.PropertyType, _configuration.ObjectValidationMode, member) { Configuration = _configuration as ChoFileRecordConfiguration, Reader = Reader, CallbackRecordFieldRead = CallbackRecordFieldRead, Writer = Writer, CallbackRecordFieldWrite = CallbackRecordFieldWrite }; property.DefaultValue = fc.DefaultValue; property.Order = fc.Order; } else { var pd = ChoTypeDescriptor.GetProperty(property.DeclaringType, property.UnderlyingName); if (pd != null) { if (pd.Attributes.OfType <DefaultValueAttribute>().Any()) { property.DefaultValue = pd.Attributes.OfType <DefaultValueAttribute>().First().Value; } if (pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()) { property.Order = pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().First().Order; } else if (pd.Attributes.OfType <DisplayAttribute>().Any()) { property.Order = pd.Attributes.OfType <DisplayAttribute>().First().Order; } else if (pd.Attributes.OfType <ColumnAttribute>().Any()) { property.Order = pd.Attributes.OfType <ColumnAttribute>().First().Order; } if (pd.Attributes.OfType <JsonPropertyAttribute>().Any()) { var jp = pd.Attributes.OfType <JsonPropertyAttribute>().First(); property.PropertyName = jp.PropertyName; property.Order = jp.Order; property.Required = jp.Required; property.ReferenceLoopHandling = jp.ItemReferenceLoopHandling; property.IsReference = jp.IsReference; property.TypeNameHandling = jp.TypeNameHandling; property.ObjectCreationHandling = jp.ObjectCreationHandling; property.ReferenceLoopHandling = jp.ReferenceLoopHandling; property.DefaultValueHandling = jp.DefaultValueHandling; property.NullValueHandling = jp.NullValueHandling; property.ItemTypeNameHandling = jp.ItemTypeNameHandling; property.ItemIsReference = jp.ItemIsReference; } else if (pd.Attributes.OfType <ChoJSONPathAttribute>().Any()) { property.PropertyName = pd.Attributes.OfType <ChoJSONPathAttribute>().First().JSONPath; } property.Converter = property.MemberConverter = new ChoContractResolverJsonConverter(null, _configuration.Culture, property.PropertyType, _configuration.ObjectValidationMode, member) { Configuration = _configuration as ChoFileRecordConfiguration, Reader = Reader, CallbackRecordFieldRead = CallbackRecordFieldRead, Writer = Writer, CallbackRecordFieldWrite = CallbackRecordFieldWrite }; } } if (_configuration.NullValueHandling == ChoNullValueHandling.Ignore) { property.NullValueHandling = NullValueHandling.Ignore; } else { property.NullValueHandling = NullValueHandling.Include; } return(property); }
private void CheckColumnsStrict(object rec) { if (Configuration.IsDynamicObject) { var eoDict = rec == null ? new Dictionary <string, object>() : rec.ToDynamicObject() as IDictionary <string, Object>; if (eoDict.Count != Configuration.YamlRecordFieldConfigurations.Count) { throw new ChoParserException("Incorrect number of fields found in record object. Expected [{0}] fields. Found [{1}] fields.".FormatString(Configuration.YamlRecordFieldConfigurations.Count, eoDict.Count)); } string[] missingColumns = Configuration.YamlRecordFieldConfigurations.Select(v => v.Name).Except(eoDict.Keys).ToArray(); if (missingColumns.Length > 0) { throw new ChoParserException("[{0}] fields are not found in record object.".FormatString(String.Join(",", missingColumns))); } } else { PropertyDescriptor[] pds = rec == null ? new PropertyDescriptor[] { } : ChoTypeDescriptor.GetProperties <ChoYamlRecordFieldAttribute>(rec.GetType()).ToArray(); if (pds.Length != Configuration.YamlRecordFieldConfigurations.Count) { throw new ChoParserException("Incorrect number of fields found in record object. Expected [{0}] fields. Found [{1}] fields.".FormatString(Configuration.YamlRecordFieldConfigurations.Count, pds.Length)); } string[] missingColumns = Configuration.YamlRecordFieldConfigurations.Select(v => v.Name).Except(pds.Select(pd => pd.Name)).ToArray(); if (missingColumns.Length > 0) { throw new ChoParserException("[{0}] fields are not found in record object.".FormatString(String.Join(",", missingColumns))); } } }
private ChoCSVReader <T> WithField(string name, int?position, Type fieldType = null, bool?quoteField = null, ChoFieldValueTrimOption fieldValueTrimOption = ChoFieldValueTrimOption.Trim, string fieldName = null, Func <object, object> valueConverter = null, Func <dynamic, object> valueSelector = null, object defaultValue = null, object fallbackValue = null, string altFieldNames = null, string fullyQualifiedMemberName = null, string formatText = null, string nullValue = null) { if (!name.IsNullOrEmpty()) { if (!_clearFields) { ClearFields(); Configuration.MapRecordFields(Configuration.RecordType); } if (fieldName.IsNullOrWhiteSpace()) { fieldName = name; } string fnTrim = name.NTrim(); ChoCSVRecordFieldConfiguration fc = null; PropertyDescriptor pd = null; if (Configuration.CSVRecordFieldConfigurations.Any(o => o.Name == fnTrim)) { fc = Configuration.CSVRecordFieldConfigurations.Where(o => o.Name == fnTrim).First(); if (position == null) { position = fc.FieldPosition; } Configuration.CSVRecordFieldConfigurations.Remove(fc); } else { pd = ChoTypeDescriptor.GetNestedProperty(typeof(T), fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); position = Configuration.CSVRecordFieldConfigurations.Count > 0 ? Configuration.CSVRecordFieldConfigurations.Max(f => f.FieldPosition) : 0; position++; } var nfc = new ChoCSVRecordFieldConfiguration(fnTrim, position.Value) { FieldType = fieldType, QuoteField = quoteField, FieldValueTrimOption = fieldValueTrimOption, FieldName = fieldName, ValueConverter = valueConverter, ValueSelector = valueSelector, DefaultValue = defaultValue, FallbackValue = fallbackValue, AltFieldNames = altFieldNames, FormatText = formatText, NullValue = nullValue }; if (fullyQualifiedMemberName.IsNullOrWhiteSpace()) { nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd; nfc.DeclaringMember = fc != null ? fc.DeclaringMember : fullyQualifiedMemberName; } else { pd = ChoTypeDescriptor.GetNestedProperty(typeof(T), fullyQualifiedMemberName); nfc.PropertyDescriptor = pd; nfc.DeclaringMember = fullyQualifiedMemberName; } if (pd != null) { if (nfc.FieldType == null) { nfc.FieldType = pd.PropertyType; } } Configuration.CSVRecordFieldConfigurations.Add(nfc); } return(this); }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { _sw = writer; TextWriter sw = writer as TextWriter; ChoGuard.ArgumentNotNull(sw, "TextWriter"); if (Configuration.JsonSerializerSettings.ContractResolver is ChoPropertyRenameAndIgnoreSerializerContractResolver) { ChoPropertyRenameAndIgnoreSerializerContractResolver cr = Configuration.JsonSerializerSettings.ContractResolver as ChoPropertyRenameAndIgnoreSerializerContractResolver; cr.CallbackRecordFieldWrite = _callbackRecordFieldWrite; cr.Writer = Writer; } if (records == null) { yield break; } if (Configuration.SingleDocument == null) { Configuration.SingleDocument = false; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; string recText = String.Empty; bool recordIgnored = false; try { foreach (object record in records) { _index++; //if (!isFirstRec) //{ // if (!recordIgnored) // sw.Write(","); // else // recordIgnored = false; //} if (TraceSwitch.TraceVerbose) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(_index)); } } recText = String.Empty; if (predicate == null || predicate(record)) { //Discover and load Xml columns from first record if (!Configuration.IsInitialized) { if (record == null) { continue; } string[] fieldNames = null; if (Configuration.RecordType == typeof(object)) { Type recordType = ElementType == null?record.GetType() : ElementType; RecordType = Configuration.RecordType = recordType.GetUnderlyingType(); //.ResolveType(); Configuration.IsDynamicObject = recordType.IsDynamicType(); } if (typeof(IDictionary).IsAssignableFrom(Configuration.RecordType) || typeof(IList).IsAssignableFrom(Configuration.RecordType)) { Configuration.UseYamlSerialization = true; } if (!Configuration.IsDynamicObject) { if (Configuration.YamlRecordFieldConfigurations.Count == 0) { Configuration.MapRecordFields(Configuration.RecordType); } } if (Configuration.IsDynamicObject) { var dict = record.ToDynamicObject() as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoYamlRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); Configuration.IsInitialized = true; if (!BeginWrite.Value) { yield break; } } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } try { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidation(Configuration, Configuration.YamlRecordFieldConfigurations); } if (ToText(_index, record, out recText)) { if (recText.EndsWith("...")) { recText = recText.Left(recText.Length - 3); } if (recText.EndsWith($"...{Environment.NewLine}")) { recText = recText.Left(recText.Length - $"...{Environment.NewLine}".Length); } if (!Configuration.SingleDocument.Value) { sw.Write($"---{EOLDelimiter}"); } sw.Write("{0}", recText); if (!Configuration.SingleDocument.Value) { sw.Write($"...{EOLDelimiter}"); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } //catch (ChoParserException) //{ // throw; //} catch (Exception ex) { ChoETLFramework.HandleException(ref ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { recordIgnored = true; ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } else { recordIgnored = true; //ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } } else { throw; } } } yield return(record); if (Configuration.NotifyAfter > 0 && _index % Configuration.NotifyAfter == 0) { if (RaisedRowsWritten(_index)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); yield break; } } } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } }
private Type DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false, List <ChoYamlRecordFieldConfiguration> recordFieldConfigurations = null, bool isTop = false) { if (recordType == null) { return(recordType); } if (!recordType.IsDynamicType()) { Type pt = null; if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()).Any()) { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { pt = pd.PropertyType.GetUnderlyingType(); bool optIn1 = ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()).Any(); if (optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1, recordFieldConfigurations, false); } else if (pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()) { var obj = new ChoYamlRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().First(), pd.Attributes.OfType <Attribute>().ToArray()); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name); if (recordFieldConfigurations != null) { if (!recordFieldConfigurations.Any(c => c.Name == pd.Name)) { recordFieldConfigurations.Add(obj); } } } } } else { if (isTop) { if (typeof(IList).IsAssignableFrom(recordType)) { throw new ChoParserException("Record type not supported."); } else if (typeof(IDictionary <string, object>).IsAssignableFrom(recordType)) { recordType = typeof(ExpandoObject); return(recordType); } else if (typeof(IDictionary).IsAssignableFrom(recordType)) { recordType = typeof(ExpandoObject); return(recordType); } } if (recordType.IsSimple()) { var obj = new ChoYamlRecordFieldConfiguration("Value", "$.Value"); obj.FieldType = recordType; recordFieldConfigurations.Add(obj); return(recordType); } foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { ChoYamlIgnoreAttribute jiAttr = pd.Attributes.OfType <ChoYamlIgnoreAttribute>().FirstOrDefault(); if (jiAttr != null) { continue; } pt = pd.PropertyType.GetUnderlyingType(); if (pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn, recordFieldConfigurations, false); } else { var obj = new ChoYamlRecordFieldConfiguration(pd.Name, (string)null); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name); StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { obj.Size = slAttr.MaximumLength; } //ChoUseYamlSerializationAttribute sAttr = pd.Attributes.OfType<ChoUseYamlSerializationAttribute>().FirstOrDefault(); //if (sAttr != null) // obj.UseYamlSerialization = sAttr.Flag; ChoYamlPathAttribute jpAttr = pd.Attributes.OfType <ChoYamlPathAttribute>().FirstOrDefault(); if (jpAttr != null) { obj.YamlPath = jpAttr.YamlPath; } ChoYamPropertyAttribute jAttr = pd.Attributes.OfType <ChoYamPropertyAttribute>().FirstOrDefault(); if (jAttr != null && !jAttr.PropertyName.IsNullOrWhiteSpace()) { obj.FieldName = jAttr.PropertyName; } else { DisplayNameAttribute dnAttr = pd.Attributes.OfType <DisplayNameAttribute>().FirstOrDefault(); if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace()) { obj.FieldName = dnAttr.DisplayName.Trim(); } else { DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (dpAttr != null) { if (!dpAttr.ShortName.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.ShortName; } else if (!dpAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.Name; } obj.Order = dpAttr.Order; } else { ColumnAttribute clAttr = pd.Attributes.OfType <ColumnAttribute>().FirstOrDefault(); if (clAttr != null) { obj.Order = clAttr.Order; if (!clAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = clAttr.Name; } } } } } DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { obj.FormatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { obj.NullValue = dfAttr.NullDisplayText; } if (recordFieldConfigurations != null) { if (!recordFieldConfigurations.Any(c => c.Name == pd.Name)) { recordFieldConfigurations.Add(obj); } } } } } } return(recordType); }
private static ChoTypePropertyInfo GetTypePropertyInfo(Type type, PropertyInfo pi) { if (_typeCache.ContainsKey(type)) { return(_typeCache[type][pi]); } lock (_padLock) { if (_typeCache.ContainsKey(type)) { return(_typeCache[type][pi]); } Dictionary <PropertyInfo, ChoTypePropertyInfo> dict = new Dictionary <PropertyInfo, ChoTypePropertyInfo>(); foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(type)) { PropertyInfo lpi = pd.ComponentType.GetProperty(pd.Name); object[] propConverters = ChoTypeDescriptor.GetTypeConverters(pi); object[] propConverterParams = ChoTypeDescriptor.GetTypeConverterParams(pi); int? size = null; string formatText = null; string nullValueText = null; if (pd.Attributes.OfType <ChoFileRecordFieldAttribute>().Any()) { var fa = pd.Attributes.OfType <ChoFileRecordFieldAttribute>().First(); size = fa.SizeInternal; formatText = fa.FormatText; nullValueText = fa.NullValue; } else { StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { size = slAttr.MaximumLength; } DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { formatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { nullValueText = dfAttr.NullDisplayText; } if (formatText.IsNullOrWhiteSpace()) { propConverterParams = new object[] { new object[] { formatText } } } ; } dict.Add(lpi, new ChoTypePropertyInfo { FormatText = formatText, PropConverterParams = propConverterParams, PropConverters = propConverters, Size = size, NullValueText = nullValueText }); } _typeCache.Add(type, dict); return(_typeCache[type][pi]); } }
internal void WithField(string name, string YamlPath = null, Type fieldType = null, ChoFieldValueTrimOption fieldValueTrimOption = ChoFieldValueTrimOption.Trim, string fieldName = null, Func <object, object> valueConverter = null, Func <object, object> itemConverter = null, Func <object, object> customSerializer = null, object defaultValue = null, object fallbackValue = null, string fullyQualifiedMemberName = null, string formatText = null, bool isArray = true, string nullValue = null, Type recordType = null, Type subRecordType = null, Func <IDictionary <string, object>, Type> fieldTypeSelector = null) { ChoGuard.ArgumentNotNull(recordType, nameof(recordType)); if (!name.IsNullOrEmpty()) { if (subRecordType != null) { MapRecordFieldsForType(subRecordType); } string fnTrim = name.NTrim(); ChoYamlRecordFieldConfiguration fc = null; PropertyDescriptor pd = null; if (YamlRecordFieldConfigurations.Any(o => o.Name == fnTrim)) { fc = YamlRecordFieldConfigurations.Where(o => o.Name == fnTrim).First(); YamlRecordFieldConfigurations.Remove(fc); } else if (subRecordType != null) { pd = ChoTypeDescriptor.GetNestedProperty(subRecordType, fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); } else { pd = ChoTypeDescriptor.GetNestedProperty(recordType, fullyQualifiedMemberName.IsNullOrWhiteSpace() ? name : fullyQualifiedMemberName); } var nfc = new ChoYamlRecordFieldConfiguration(fnTrim, YamlPath) { FieldType = fieldType, FieldValueTrimOption = fieldValueTrimOption, FieldName = fieldName.IsNullOrWhiteSpace() ? name : fieldName, ValueConverter = valueConverter, CustomSerializer = customSerializer, DefaultValue = defaultValue, FallbackValue = fallbackValue, FormatText = formatText, ItemConverter = itemConverter, IsArray = isArray, NullValue = nullValue, FieldTypeSelector = fieldTypeSelector, }; if (fullyQualifiedMemberName.IsNullOrWhiteSpace()) { nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd; nfc.DeclaringMember = fc != null ? fc.DeclaringMember : fullyQualifiedMemberName; } else { if (subRecordType == null) { pd = ChoTypeDescriptor.GetNestedProperty(recordType, fullyQualifiedMemberName); } else { pd = ChoTypeDescriptor.GetNestedProperty(subRecordType, fullyQualifiedMemberName); } nfc.PropertyDescriptor = pd; nfc.DeclaringMember = fullyQualifiedMemberName; } if (pd != null) { if (nfc.FieldType == null) { nfc.FieldType = pd.PropertyType; } } if (subRecordType == null) { YamlRecordFieldConfigurations.Add(nfc); } else { AddFieldForType(subRecordType, nfc); } } }
public static Dictionary <string, object> ToDictionary(this object target, string propName = null) { if (target == null) { return(null); } //ChoGuard.ArgumentNotNull(target, "Target"); if (target is IDictionary <string, object> ) { return((Dictionary <string, object>)target); } if (target is IDictionary) { Dictionary <string, object> dict1 = new Dictionary <string, object>(); foreach (var kvp in ((IDictionary)target).Keys) { dict1.Add(kvp.ToNString(), ((IDictionary)target)[kvp]); } return(dict1); } if (target is IEnumerable <KeyValuePair <string, object> > ) { return(new List <KeyValuePair <string, object> >(target as IEnumerable <KeyValuePair <string, object> >).ToDictionary(x => x.Key, x => x.Value.ToDictionaryInternal())); } if (target is IEnumerable <Tuple <string, object> > ) { return(new List <Tuple <string, object> >(target as IEnumerable <Tuple <string, object> >).ToDictionary(x => x.Item1, x => x.Item2.ToDictionaryInternal())); } if (target is IList) { return(((IList)(target)).OfType <object>().Select((item, index) => { return new KeyValuePair <string, object>("{0}".FormatString(index), item); }).ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToDictionaryInternal())); } Dictionary <string, object> dict = new Dictionary <string, object>(); foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(target.GetType())) { var value = ChoType.GetPropertyValue(target, pd.Name); if (value == null) { dict.Add(pd.Name, value); } else if (value.GetType().IsSimpleSpecial()) { dict.Add(pd.Name, value.ToNString()); } else if (value.GetType().IsSimple()) { dict.Add(pd.Name, value); } else { dict.Add(pd.Name, value.ToDictionary(pd.Name)); } } return(dict); }
private void DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false) { if (!recordType.IsDynamicType()) { Type pt = null; int startIndex = 0; int size = 0; if (optIn) //ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType<ChoFixedLengthRecordFieldAttribute>().Any()).Any()) { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { pt = pd.PropertyType.GetUnderlyingType(); if (!pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt)) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn); } else if (pd.Attributes.OfType <ChoFixedLengthRecordFieldAttribute>().Any()) { var obj = new ChoFixedLengthRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoFixedLengthRecordFieldAttribute>().First(), pd.Attributes.OfType <Attribute>().ToArray()); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name); if (!FixedLengthRecordFieldConfigurations.Any(c => c.Name == pd.Name)) { FixedLengthRecordFieldConfigurations.Add(obj); } } } } else { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { pt = pd.PropertyType.GetUnderlyingType(); if (pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt)) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn); } else { if (FixedLengthFieldDefaultSizeConfiguation == null) { size = ChoFixedLengthFieldDefaultSizeConfiguation.Instance.GetSize(pd.PropertyType); } else { size = FixedLengthFieldDefaultSizeConfiguation.GetSize(pd.PropertyType); } var obj = new ChoFixedLengthRecordFieldConfiguration(pd.Name, startIndex, size); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name); StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { obj.Size = slAttr.MaximumLength; } DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (dpAttr != null) { if (!dpAttr.ShortName.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.ShortName; } else if (!dpAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.Name; } } DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { obj.FormatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { obj.NullValue = dfAttr.NullDisplayText; } if (!FixedLengthRecordFieldConfigurations.Any(c => c.Name == pd.Name)) { FixedLengthRecordFieldConfigurations.Add(obj); } startIndex += size; } } } } }
public static void ConvertNSetValue(this object target, PropertyDescriptor pd, object fv, CultureInfo culture, long index = 0) { PropertyInfo pi = pd.ComponentType.GetProperty(pd.Name); IChoNotifyChildRecordRead callbackRecord = target as IChoNotifyChildRecordRead; if (callbackRecord != null) { object state = fv; bool retValue = ChoFuncEx.RunWithIgnoreError(() => callbackRecord.BeforeRecordFieldLoad(target, index, pd.Name, ref state), true); if (retValue) { fv = state; } } try { object[] PropConverters = ChoTypeDescriptor.GetTypeConverters(pi); object[] PropConverterParams = ChoTypeDescriptor.GetTypeConverterParams(pi); string formatText = null; DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { formatText = dfAttr.DataFormatString; } object[] fcParams = PropConverterParams; if (formatText.IsNullOrWhiteSpace()) { fcParams = new object[] { new object[] { formatText } } } ; if (PropConverters.IsNullOrEmpty()) { fv = ChoConvert.ConvertFrom(fv, pi.PropertyType, null, PropConverters, fcParams, culture); } else { fv = ChoConvert.ConvertFrom(fv, pi.PropertyType, null, PropConverters, fcParams, culture); } pd.SetValue(target, fv); if (callbackRecord != null) { ChoFuncEx.RunWithIgnoreError(() => callbackRecord.AfterRecordFieldLoad(target, index, pd.Name, fv), true); } } catch (Exception ex) { if (callbackRecord != null) { bool ret = ChoFuncEx.RunWithIgnoreError(() => callbackRecord.RecordFieldLoadError(target, index, pd.Name, fv, ex), false); if (!ret) { if (ex is ValidationException) { throw; } throw new ChoReaderException($"Failed to parse '{fv}' value for '{pd.Name}' field in '{target.GetType().Name}' object.", ex); } } } }