FlatFile icon indicating copy to clipboard operation
FlatFile copied to clipboard

FixedLength - Overall Line Length

Open OpenSpacesAndPlaces opened this issue 7 years ago • 2 comments

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 avatar Mar 23 '18 15:03 OpenSpacesAndPlaces

@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 avatar Mar 23 '18 17:03 OpenSpacesAndPlaces

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".

OpenSpacesAndPlaces avatar Mar 23 '18 18:03 OpenSpacesAndPlaces