Example #1
0
        public ChoParquetWriter <T> WithFields(params string[] fieldsNames)
        {
            string fnTrim = null;

            if (!fieldsNames.IsNullOrEmpty())
            {
                int maxFieldPos       = Configuration.ParquetRecordFieldConfigurations.Count > 0 ? Configuration.ParquetRecordFieldConfigurations.Max(f => f.FieldPosition) : 0;
                PropertyDescriptor pd = null;
                ChoParquetRecordFieldConfiguration fc = null;
                foreach (string fn in fieldsNames)
                {
                    if (fn.IsNullOrEmpty())
                    {
                        continue;
                    }
                    if (!_clearFields)
                    {
                        ClearFields();
                        Configuration.MapRecordFields(Configuration.RecordType);
                    }

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

                    var nfc = new ChoParquetRecordFieldConfiguration(fnTrim, ++maxFieldPos)
                    {
                        FieldName = fn
                    };
                    nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd;
                    nfc.DeclaringMember    = fc != null ? fc.DeclaringMember : null;
                    if (pd != null)
                    {
                        if (nfc.FieldType == null)
                        {
                            nfc.FieldType = pd.PropertyType;
                        }
                    }

                    Configuration.ParquetRecordFieldConfigurations.Add(nfc);
                }
            }

            return(this);
        }
Example #2
0
 public virtual void Validate(object state)
 {
     if (!IsDynamicObject)
     {
         //PIDict = ChoType.GetProperties(RecordType).ToDictionary(p => p.Name);
         PDDict = new Dictionary <string, PropertyDescriptor>();
         if (PIDict != null)
         {
             foreach (var fn in PIDict.Keys)
             {
                 PDDict.Add(fn, ChoTypeDescriptor.GetProperty(RecordType, fn));
             }
         }
     }
 }
Example #3
0
        public ChoXmlReader <T> WithFields(params string[] fieldsNames)
        {
            string fnTrim = null;

            if (!fieldsNames.IsNullOrEmpty())
            {
                PropertyDescriptor             pd = null;
                ChoXmlRecordFieldConfiguration fc = null;
                foreach (string fn in fieldsNames)
                {
                    if (fn.IsNullOrEmpty())
                    {
                        continue;
                    }
                    if (!_clearFields)
                    {
                        ClearFields();
                        Configuration.MapRecordFields(Configuration.RecordType);
                    }

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

                    var nfc = new ChoXmlRecordFieldConfiguration(fnTrim, $"/{fnTrim}");
                    nfc.PropertyDescriptor = fc != null ? fc.PropertyDescriptor : pd;
                    nfc.DeclaringMember    = fc != null ? fc.DeclaringMember : null;
                    if (pd != null)
                    {
                        if (nfc.FieldType == null)
                        {
                            nfc.FieldType = pd.PropertyType;
                        }
                    }

                    Configuration.XmlRecordFieldConfigurations.Add(nfc);
                }
            }

            return(this);
        }
Example #4
0
        public static void SetDefaultValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture)
        {
            if (rec is ExpandoObject)
            {
                ((IDictionary <string, object>)rec).SetDefaultValue(fn, fieldConfig, culture);
                return;
            }

            object fieldValue = null;

            //Set default value to member
            try
            {
                bool   defaultValueExists = true;
                object defaultValue       = null;
                if (fieldConfig.IsDefaultValueSpecified)
                {
                    defaultValue = fieldConfig.DefaultValue;
                }
                else if (ChoType.HasDefaultValue(ChoTypeDescriptor.GetProperty(rec.GetType(), fn)))
                {
                    defaultValue = ChoType.GetRawDefaultValue(ChoTypeDescriptor.GetProperty(rec.GetType(), fn));
                }
                else
                {
                    defaultValueExists = false;
                }

                if (defaultValueExists)
                {
                    if (fieldConfig.Converters.IsNullOrEmpty())
                    {
                        fieldValue = ChoConvert.ConvertFrom(defaultValue, ChoType.GetMemberInfo(rec.GetType(), fn), null, culture);
                    }
                    else
                    {
                        fieldValue = ChoConvert.ConvertFrom(defaultValue, ChoType.GetMemberType(rec.GetType(), fn), null, fieldConfig.Converters.ToArray(), null, culture);
                    }

                    ChoType.SetMemberValue(rec, fn, fieldValue);
                }
            }
            catch { }
        }
Example #5
0
        private bool IsIgnored(Type type, string jsonPropertyName, string propertyName)
        {
            if (_configuration != null && _configuration.ContainsRecordConfigForType(type))
            {
                var dict = _configuration.JSONRecordFieldConfigurationsForType[type];
                if (dict != null && dict.ContainsKey(jsonPropertyName))
                {
                    return(false);
                }
            }

            if (!_ignores.ContainsKey(type))
            {
                var pd = ChoTypeDescriptor.GetProperty(type, propertyName);
                return(pd != null?pd.Attributes.OfType <ChoIgnoreMemberAttribute>().Any() : true);
            }

            return(_ignores[type].Contains(jsonPropertyName));
        }
Example #6
0
        public static object GetDefaultValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig)
        {
            if (fieldConfig.IsDefaultValueSpecified)
            {
                return(fieldConfig.DefaultValue);
            }
            else
            {
                if (rec is ExpandoObject)
                {
                }
                else if (ChoType.HasProperty(rec.GetType(), fn))
                {
                    return(ChoType.GetRawDefaultValue(ChoTypeDescriptor.GetProperty(rec.GetType(), fn)));
                }
            }

            return(null);
        }
Example #7
0
        public static bool SetFallbackValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture)
        {
            if (rec is ExpandoObject)
            {
                return(((IDictionary <string, object>)rec).SetFallbackValue(fn, fieldConfig, culture));
            }

            object fieldValue = null;

            //Set Fallback value to member
            bool   FallbackValueExists = true;
            object FallbackValue       = null;

            if (fieldConfig.IsFallbackValueSpecified)
            {
                FallbackValue = fieldConfig.FallbackValue;
            }
            else if (ChoType.HasFallbackValue(ChoTypeDescriptor.GetProperty(rec.GetType(), fn)))
            {
                FallbackValue = ChoType.GetRawFallbackValue(ChoTypeDescriptor.GetProperty(rec.GetType(), fn));
            }
            else
            {
                FallbackValueExists = false;
            }

            if (FallbackValueExists)
            {
                if (fieldConfig.Converters.IsNullOrEmpty())
                {
                    fieldValue = ChoConvert.ConvertFrom(FallbackValue, ChoType.GetMemberInfo(rec.GetType(), fn), null, culture);
                }
                else
                {
                    fieldValue = ChoConvert.ConvertFrom(FallbackValue, ChoType.GetMemberType(rec.GetType(), fn), null, fieldConfig.Converters.ToArray(), null, culture);
                }

                ChoType.SetMemberValue(rec, fn, fieldValue);
            }

            return(FallbackValueExists);
        }
Example #8
0
        public static PropertyDescriptor GetNestedProperty(Type recType, string pn)
        {
            if (pn.IsNullOrWhiteSpace())
            {
                return(null);
            }

            string[]           pnTokens = pn.SplitNTrim(".");
            PropertyDescriptor pd       = null;

            for (int index = 0; index < pnTokens.Length - 1; index++)
            {
                pd = ChoTypeDescriptor.GetProperty(recType, pnTokens[index]);
                if (pd == null)
                {
                    return(null);
                }

                recType = pd.PropertyType;
            }

            return(ChoTypeDescriptor.GetProperty(recType, pnTokens.Last()));
        }
        private bool IsIgnored(Type type, string jsonPropertyName, string propertyName, string propertyFullName)
        {
            if (_configuration.IgnoredFields.Contains(propertyFullName) || _configuration.IgnoredFields.Contains(propertyName))
            {
                return(true);
            }

            var pd = ChoTypeDescriptor.GetProperty(type, propertyName);

            if (pd != null)
            {
                if (pd.Attributes.OfType <ChoIgnoreMemberAttribute>().Any())
                {
                    return(true);
                }
                else if (pd.Attributes.OfType <JsonIgnoreAttribute>().Any())
                {
                    return(true);
                }
            }

            return(false);
        }
Example #10
0
        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);
        }
        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);
        }
        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);
            }
        }
        public override void Validate(object state)
        {
            if (TurnOnAutoDiscoverJsonConverters)
            {
                ChoJSONConvertersCache.Init();
            }

            if (_jsonSerializerSettings != null)
            {
                foreach (var conv in GetJSONConverters())
                {
                    _jsonSerializerSettings.Converters.Add(conv);
                }
                foreach (var conv in _jsonSerializerSettings.Converters.OfType <IChoJSONConverter>())
                {
                    conv.Serializer            = JsonSerializer;
                    conv.Context               = new ChoDynamicObject();
                    conv.Context.Configuration = this;
                }
                foreach (var conv in _jsonSerializerSettings.Converters)
                {
                    JsonSerializer.Converters.Add(conv);
                }
            }

            if (RecordType != null)
            {
                Init(RecordType);
            }

            base.Validate(state);

            string[] fieldNames = null;
            JObject  jObject    = null;

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

            if (fieldNames != null && JSONRecordFieldConfigurations.Count > 0 && FlattenNode)
            {
                JSONRecordFieldConfigurations.Clear();
            }

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

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

                        var obj = new ChoJSONRecordFieldConfiguration(fn, (string)null);
                        JSONRecordFieldConfigurations.Add(obj);
                    }
                }
            }
            else
            {
                foreach (var fc in JSONRecordFieldConfigurations)
                {
                    fc.ComplexJPathUsed = !(fc.JSONPath.IsNullOrWhiteSpace() || String.Compare(fc.FieldName, fc.JSONPath, true) == 0);
                }
            }

            //if (JSONRecordFieldConfigurations.Count <= 0)
            //    throw new ChoRecordConfigurationException("No record fields specified.");

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

            //Check field position for duplicate
            string[] dupFields = JSONRecordFieldConfigurations.GroupBy(i => i.FieldName)
                                 .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>(StringComparer.InvariantCultureIgnoreCase);
            PDDict = new Dictionary <string, PropertyDescriptor>(StringComparer.InvariantCultureIgnoreCase);
            foreach (var fc in JSONRecordFieldConfigurations)
            {
                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 = JSONRecordFieldConfigurations.Where(i => !i.Name.IsNullOrWhiteSpace()).ToDictionary(i => i.Name);

            LoadNCacheMembers(JSONRecordFieldConfigurations);
        }
        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)
                    {
                        if (IgnoredFields.Contains(fn))
                        {
                            continue;
                        }

                        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 && !IgnoreDuplicateFields)
            {
                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)
            {
                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 = ChoTypeDescriptor.GetProperties(RecordType).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  = 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 =>
                {
                    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);
            }

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

            if (!FileHeaderConfiguration.HasHeaderRecord)
            {
            }
            else
            {
            }

            LoadNCacheMembers(FixedLengthRecordFieldConfigurations);
        }
        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.JSONRecordFieldConfigurationsForType[property.DeclaringType];
                if (dict != null && dict.ContainsKey(property.UnderlyingName))
                {
                    property.Converter = property.MemberConverter = new ChoContractResolverJsonConverter(dict[property.UnderlyingName], _configuration.Culture,
                                                                                                         property.PropertyType, _configuration.ObjectValidationMode, member);
                }
            }
            else if (_configuration.JSONRecordFieldConfigurations.Any(f => f.DeclaringMember == propertyFullName))
            {
                var fc = _configuration.JSONRecordFieldConfigurations.First(f => f.DeclaringMember == propertyFullName);
                property.MemberConverter = new ChoContractResolverJsonConverter(fc, _configuration.Culture, property.PropertyType, _configuration.ObjectValidationMode, member);
                property.DefaultValue    = fc.DefaultValue;
                property.Order           = fc.Order;
            }
            else if (_configuration.JSONRecordFieldConfigurations.Any(f => f.Name == propertyFullName))
            {
                var fc = _configuration.JSONRecordFieldConfigurations.First(f => f.Name == propertyFullName);
                property.MemberConverter = new ChoContractResolverJsonConverter(fc, _configuration.Culture, property.PropertyType, _configuration.ObjectValidationMode, member);
                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;
                    }
                }
            }

            if (_configuration.NullValueHandling == ChoNullValueHandling.Ignore)
            {
                property.NullValueHandling = NullValueHandling.Ignore;
            }
            else
            {
                property.NullValueHandling = NullValueHandling.Include;
            }


            return(property);
        }
Example #16
0
        public override void Validate(object state)
        {
            if (RecordType != null)
            {
                Init(RecordType);
            }

            base.Validate(state);

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

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

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

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

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

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

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

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

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

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

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

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

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

            LoadNCacheMembers(YamlRecordFieldConfigurations);
        }
Example #17
0
        public static bool IsValidFor(this object @this, PropertyDescriptor mi, out Exception aggEx)
        {
            aggEx = null;
            ChoGuard.ArgumentNotNullOrEmpty(@this, "Target");

            if (@this == null)
            {
                return(true);
            }

            var    results = new List <ValidationResult>();
            object surrObj = ChoMetadataObjectCache.Default.GetMetadataObject(@this);
            bool   result  = false;

            if (surrObj is IChoValidatable)
            {
                result = ((IChoValidatable)surrObj).TryValidateFor(@this, mi.Name, results);
            }
            else
            {
                var context = new ValidationContext(@this, null, null);
                context.MemberName = mi.Name;

                result = Validator.TryValidateValue(mi.GetValue(@this), context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(@this.GetType(), mi.Name)));
            }

            if (!result)
            {
                if (results.Count > 0)
                {
                    aggEx = new ValidationException("Failed to validate '{0}' member. {2}{1}".FormatString(mi.Name, ToString(results), Environment.NewLine));
                }
                else
                {
                    aggEx = new ValidationException("Failed to valudate.");
                }
            }
            return(result);
        }
Example #18
0
 public static void DoMemberLevelValidation(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, ChoObjectValidationMode vm, object fieldValue)
 {
     if ((vm & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel)
     {
         if (fieldConfig.Validators.IsNullOrEmpty())
         {
             if (!(rec is ExpandoObject))
             {
                 ChoValidator.ValidateFor(fieldValue, fn, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(rec.GetType(), fn)).ToArray());
             }
         }
         else
         {
             ChoValidator.ValidateFor(fieldValue, fn, fieldConfig.Validators);
         }
     }
 }
Example #19
0
        public static bool IsValidFor(this object @this, MemberInfo mi, out Exception aggEx)
        {
            aggEx = null;
            ChoGuard.ArgumentNotNullOrEmpty(@this, "Target");

            if (@this == null)
            {
                return(true);
            }

            var    results = new List <ValidationResult>();
            object surrObj = ChoSurrogateObjectCache.Default.GetSurrogateObject(@this);

            if (surrObj is IChoValidatable)
            {
                ((IChoValidatable)surrObj).TryValidateFor(@this, mi.Name, results);
            }
            else
            {
                //if (ChoObjectMemberMetaDataCache.Default.IsRequired(mi) && ChoType.GetMemberValue(@this, mi) == null)
                //    results.Add(new ValidationResult("Null value found for {0} member.".FormatString(mi.Name)));

                var context = new ValidationContext(@this, null, null);
                context.MemberName = mi.Name;

                Validator.TryValidateValue(ChoType.GetMemberValue(@this, mi), context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(@this.GetType(), mi.Name)));
            }

            if (results.Count > 0)
            {
                aggEx = new ApplicationException("Failed to validate '{0}' member. {2}{1}".FormatString(mi.Name, ToString(results), Environment.NewLine));
                return(false);
            }
            else
            {
                return(true);
            }
        }