Skip to content

Commit

Permalink
feat: 1425 output fieldname of missing recommended field notice even …
Browse files Browse the repository at this point in the history
…if there is no column (#1511)

* replace FieldLevelEnum with GtfsColumnDescriptor

* use columnDescriptor instead of columnDescriptor.fieldLevel() in TableDescriptorGenerator

* fix broken tests

* fixe asString_recommended_missing test
format code

* format code

* remove the old asString method

---------
Close #1425 
Co-authored-by: David Gamez <1192523+davidgamez@users.noreply.github.com>
  • Loading branch information
qcdyx committed Jun 21, 2023
1 parent eada0f4 commit 417a53a
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.mobilitydata.gtfsvalidator.notice.TooManyRowsNotice;
import org.mobilitydata.gtfsvalidator.notice.UnexpectedEnumValueNotice;
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
import org.mobilitydata.gtfsvalidator.table.GtfsColumnDescriptor;
import org.mobilitydata.gtfsvalidator.table.GtfsEnum;
import org.mobilitydata.gtfsvalidator.type.GtfsColor;
import org.mobilitydata.gtfsvalidator.type.GtfsDate;
Expand Down Expand Up @@ -125,16 +126,15 @@ public boolean checkRowLength() {
}

@Nullable
public String asString(int columnIndex, FieldLevelEnum level) {
public String asString(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
String s = row.asString(columnIndex);
if (level == FieldLevelEnum.REQUIRED && s == null) {
if (columnDescriptor.fieldLevel() == FieldLevelEnum.REQUIRED && s == null) {
noticeContainer.addValidationNotice(
new MissingRequiredFieldNotice(
fileName, getRowNumber(), header.getColumnName(columnIndex)));
} else if (level == FieldLevelEnum.RECOMMENDED && s == null) {
new MissingRequiredFieldNotice(fileName, getRowNumber(), columnDescriptor.columnName()));
} else if (columnDescriptor.fieldLevel() == FieldLevelEnum.RECOMMENDED && s == null) {
noticeContainer.addValidationNotice(
new MissingRecommendedFieldNotice(
fileName, getRowNumber(), header.getColumnName(columnIndex)));
fileName, getRowNumber(), columnDescriptor.columnName()));
}
if (s != null) {
s =
Expand All @@ -147,23 +147,23 @@ public String asString(int columnIndex, FieldLevelEnum level) {
}

@Nullable
public String asText(int columnIndex, FieldLevelEnum level) {
return asString(columnIndex, level);
public String asText(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return asString(columnIndex, columnDescriptor);
}

@Nullable
public String asId(int columnIndex, FieldLevelEnum level) {
return asValidatedString(columnIndex, level, fieldValidator::validateId);
public String asId(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return asValidatedString(columnIndex, columnDescriptor, fieldValidator::validateId);
}

@Nullable
public String asUrl(int columnIndex, FieldLevelEnum level) {
return asValidatedString(columnIndex, level, fieldValidator::validateUrl);
public String asUrl(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return asValidatedString(columnIndex, columnDescriptor, fieldValidator::validateUrl);
}

@Nullable
public String asEmail(int columnIndex, FieldLevelEnum level) {
return asValidatedString(columnIndex, level, fieldValidator::validateEmail);
public String asEmail(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return asValidatedString(columnIndex, columnDescriptor, fieldValidator::validateEmail);
}

/**
Expand All @@ -172,14 +172,14 @@ public String asEmail(int columnIndex, FieldLevelEnum level) {
* unknown, only phone number starting by "+" are validated.
*
* @param columnIndex the column index
* @param level whether the value is required, recommended or optional according to GTFS
* @param columnDescriptor Gtfs Column Descriptor
* @return the string value of the phone number to be validated if a valid number according to the
* {@code CountryCode}, returns {@code null} otherwise. Note that if {@code CountryCode} is
* unknown, only phone number starting by "+" are validated.
*/
@Nullable
public String asPhoneNumber(int columnIndex, FieldLevelEnum level) {
return asValidatedString(columnIndex, level, fieldValidator::validatePhoneNumber);
public String asPhoneNumber(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return asValidatedString(columnIndex, columnDescriptor, fieldValidator::validatePhoneNumber);
}

@Nullable
Expand All @@ -190,8 +190,9 @@ public String asPhoneNumber(int columnIndex, FieldLevelEnum level) {
* @param level whether the value is required, recommended or optional according to GTFS
* @return If parsing was successful returns {@code Locale}, otherwise, {@code null} is returned.
*/
public Locale asLanguageCode(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, RowParser::parseLocale, InvalidLanguageCodeNotice::new);
public Locale asLanguageCode(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(
columnIndex, columnDescriptor, RowParser::parseLocale, InvalidLanguageCodeNotice::new);
}

/**
Expand All @@ -209,28 +210,30 @@ private static Locale parseLocale(String languageTag) {
}

@Nullable
public ZoneId asTimezone(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, ZoneId::of, InvalidTimezoneNotice::new);
public ZoneId asTimezone(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(columnIndex, columnDescriptor, ZoneId::of, InvalidTimezoneNotice::new);
}

@Nullable
public Currency asCurrencyCode(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, Currency::getInstance, InvalidCurrencyNotice::new);
public Currency asCurrencyCode(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(
columnIndex, columnDescriptor, Currency::getInstance, InvalidCurrencyNotice::new);
}

@Nullable
public Double asFloat(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, Double::parseDouble, InvalidFloatNotice::new);
public Double asFloat(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(columnIndex, columnDescriptor, Double::parseDouble, InvalidFloatNotice::new);
}

@Nullable
public Double asFloat(int columnIndex, FieldLevelEnum level, NumberBounds bounds) {
return checkBounds(asFloat(columnIndex, level), 0.0, columnIndex, "float", bounds);
public Double asFloat(
int columnIndex, GtfsColumnDescriptor columnDescriptor, NumberBounds bounds) {
return checkBounds(asFloat(columnIndex, columnDescriptor), 0.0, columnIndex, "float", bounds);
}

@Nullable
public Double asLatitude(int columnIndex, FieldLevelEnum level) {
Double value = asFloat(columnIndex, level);
public Double asLatitude(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
Double value = asFloat(columnIndex, columnDescriptor);
if (value != null && !(-90 <= value && value <= 90)) {
noticeContainer.addValidationNotice(
new NumberOutOfRangeNotice(
Expand All @@ -245,8 +248,8 @@ public Double asLatitude(int columnIndex, FieldLevelEnum level) {
}

@Nullable
public Double asLongitude(int columnIndex, FieldLevelEnum level) {
Double value = asFloat(columnIndex, level);
public Double asLongitude(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
Double value = asFloat(columnIndex, columnDescriptor);
if (value != null && !(-180 <= value && value <= 180)) {
noticeContainer.addValidationNotice(
new NumberOutOfRangeNotice(
Expand All @@ -261,24 +264,30 @@ public Double asLongitude(int columnIndex, FieldLevelEnum level) {
}

@Nullable
public Integer asInteger(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, Integer::parseInt, InvalidIntegerNotice::new);
public Integer asInteger(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(columnIndex, columnDescriptor, Integer::parseInt, InvalidIntegerNotice::new);
}

@Nullable
public Integer asInteger(int columnIndex, FieldLevelEnum level, NumberBounds bounds) {
return checkBounds(asInteger(columnIndex, level), 0, columnIndex, "integer", bounds);
public Integer asInteger(
int columnIndex, GtfsColumnDescriptor columnDescriptor, NumberBounds bounds) {
return checkBounds(asInteger(columnIndex, columnDescriptor), 0, columnIndex, "integer", bounds);
}

@Nullable
public BigDecimal asDecimal(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, BigDecimal::new, InvalidFloatNotice::new);
public BigDecimal asDecimal(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(columnIndex, columnDescriptor, BigDecimal::new, InvalidFloatNotice::new);
}

@Nullable
public BigDecimal asDecimal(int columnIndex, FieldLevelEnum level, NumberBounds bounds) {
public BigDecimal asDecimal(
int columnIndex, GtfsColumnDescriptor columnDescriptor, NumberBounds bounds) {
return checkBounds(
asDecimal(columnIndex, level), new BigDecimal(0), columnIndex, "decimal", bounds);
asDecimal(columnIndex, columnDescriptor),
new BigDecimal(0),
columnIndex,
"decimal",
bounds);
}

/**
Expand Down Expand Up @@ -337,14 +346,18 @@ private <T extends Comparable<T>> T checkBounds(
}

@Nullable
public GtfsColor asColor(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, GtfsColor::fromString, InvalidColorNotice::new);
public GtfsColor asColor(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(
columnIndex, columnDescriptor, GtfsColor::fromString, InvalidColorNotice::new);
}

@Nullable
public <E extends GtfsEnum> Integer asEnum(
int columnIndex, FieldLevelEnum level, EnumCreator<E> enumCreator, E unrecognized) {
Integer i = asInteger(columnIndex, level);
int columnIndex,
GtfsColumnDescriptor columnDescriptor,
EnumCreator<E> enumCreator,
E unrecognized) {
Integer i = asInteger(columnIndex, columnDescriptor);
if (i == null) {
return null;
}
Expand All @@ -358,13 +371,13 @@ public <E extends GtfsEnum> Integer asEnum(
}

@Nullable
public GtfsTime asTime(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, GtfsTime::fromString, InvalidTimeNotice::new);
public GtfsTime asTime(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(columnIndex, columnDescriptor, GtfsTime::fromString, InvalidTimeNotice::new);
}

@Nullable
public GtfsDate asDate(int columnIndex, FieldLevelEnum level) {
return parseAsType(columnIndex, level, GtfsDate::fromString, InvalidDateNotice::new);
public GtfsDate asDate(int columnIndex, GtfsColumnDescriptor columnDescriptor) {
return parseAsType(columnIndex, columnDescriptor, GtfsDate::fromString, InvalidDateNotice::new);
}

public enum NumberBounds {
Expand All @@ -384,7 +397,7 @@ public interface EnumCreator<E> {
* parsing failed.
*
* @param columnIndex index of the column to parse
* @param level whether the value is required, recommended or optional according to GTFS
* @param columnDescriptor Gtfs Column Descriptor
* @param parsingFunction function that converts string to an object to return
* @param noticingFunction function to create a notice about parse errors
* @param <T> the type to return
Expand All @@ -393,10 +406,10 @@ public interface EnumCreator<E> {
@Nullable
private <T> T parseAsType(
int columnIndex,
FieldLevelEnum level,
GtfsColumnDescriptor columnDescriptor,
Function<String, T> parsingFunction,
NoticingFunction noticingFunction) {
String s = asString(columnIndex, level);
String s = asString(columnIndex, columnDescriptor);
if (s == null) {
return null;
}
Expand All @@ -418,14 +431,16 @@ private <T> T parseAsType(
* error is emitted, the value is considered invalid.
*
* @param columnIndex index of the column to parse
* @param level whether the value is required, recommended or optional according to GTFS
* @param columnDescriptor GTFS column descriptor
* @param validatingFunction the predicate to validate a given string
* @return the cell value at the given column or null if the value is missing
*/
@Nullable
private String asValidatedString(
int columnIndex, FieldLevelEnum level, FieldValidatingFunction validatingFunction) {
String s = asString(columnIndex, level);
int columnIndex,
GtfsColumnDescriptor columnDescriptor,
FieldValidatingFunction validatingFunction) {
String s = asString(columnIndex, columnDescriptor);
if (s == null) {
return null;
}
Expand Down
Loading

0 comments on commit 417a53a

Please sign in to comment.