forcewake / FlatFile

FlatFile is a library to work with flat files
MIT License
106 stars 39 forks source link

FixedLength - Overall Line Length #69

Open OpenSpacesAndPlaces opened 6 years ago

OpenSpacesAndPlaces commented 6 years ago

I might be missing a setting, but is there a way to verify the overall length of the line?

I was setting up some test cases for errors, and one of the ones I tried was making a line way longer than it is supposed to be, but it doesn't seem to hit any catches.

I would expect it at least hitting the truncateIfExceeded = false.

OpenSpacesAndPlaces commented 6 years ago

@forcewake

I also tried working around it like:

    [FixedLengthField(11,1, Converter = typeof(DTOHelper.EndOfLineConverter), NullValue = "",)]
        public String EndOfLine { get; set; }
    public class EndOfLineConverter : ITypeConverter
        {
            public bool CanConvertFrom(Type type)
            {
                return type == typeof(string);
            }

            public object ConvertFromString(String source)
            {
                string sValue = source as string;

                if (!string.IsNullOrEmpty(sValue))
                {
                    ThrowDetailedError(sValue);
                }

                return true;
            }

            public bool CanConvertTo(Type type)
            {
                return type == typeof(string);
            }

            public string ConvertToString(object source)
            {
                throw new NotImplementedException();
            }

            private void ThrowDetailedError(String sValue)
            {
                String sError = "Line exceeds allowed length. We found these additional characters: " + sValue + Environment.NewLine;
                throw new NotSupportedException(sError);
            }
        }

But then on every line except the problem case it hits this:

  throw new IndexOutOfRangeException(
                    string.Format("The field at {0} with a length of {1} cannot be found on the line because the line is too short. " +
"Setting a NullValue for this field will allow the line to be parsed and this field to be null.", field.Index, field.Length));

Any suggestions?

OpenSpacesAndPlaces commented 6 years ago

Re: Workaround

When I pulled the source and ran - what what was preventing the workaround was:

FieldSettingsBaseAttribute
    public bool IsNullable
        {
            get { return !string.IsNullOrEmpty(NullValue); }
        }

If I make it settable then the above works fine: public bool IsNullable { get; set; }

That said, it work be better if there was just an inherent check for "line.Length" > "settings.length".