Skip to content

Commit

Permalink
refactor(solrteur): switch Block model to use keyword IQSS#7662
Browse files Browse the repository at this point in the history
Instead of defining a static trigger, we want to be able to configure
the trigger sign. Due to this, we use the keyword only and move the
trigger handling into the ParsingState (which is analysing the line for
state transition anyway).
  • Loading branch information
poikilotherm committed Apr 29, 2022
1 parent 2aa8a4e commit 3733117
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
34 changes: 19 additions & 15 deletions modules/solr-configset/src/main/scripts/cli/util/model/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
import java.util.stream.Collectors;

public final class Block {
public static final String TRIGGER = Constants.TRIGGER_INDICATOR + "metadataBlock";

public static final String KEYWORD = "metadataBlock";
public static final String NAME_PATTERN = "[a-z]+(([\\d_])|([A-Z0-9][a-z0-9]+))*([A-Z])?";

/**
* Programmatic variant of the spec of a #metadataBlock. List all the column headers and associated restrictions
* on the values of a column.
*/
public enum Header {
TRIGGER(
Block.TRIGGER,
KEYWORD(
Block.KEYWORD,
String::isEmpty,
"must have no value (be empty)"
),
Expand Down Expand Up @@ -87,15 +86,18 @@ public static List<String> getHeaders() {

/**
* Parse a {@link String} as a header of a metadata block definition. Will validate the presence or absence
* of column headers as defined by the spec. This is a forgiving parser - the order of columns may be different
* from what the spec defaults to (spec isn't precise about strict ordering).
* of column headers as defined by the spec. This is not a lenient parser - headers need to comply with order
* from the spec. On the other hand, it is case-insensitive.
*
* @param line The textual line to parse for column headers
* @return A list of {@link Header} that reflects the order as given in {@see line} (order may differ from spec).
* @throws ParserException when presented column headers are missing, invalid or the complete line is just wrong.
* @param config The parser configuration to be used
* @return A list of {@link Header} build from the line of text
* @throws ParserException When presented column headers are missing, invalid or the complete line is just wrong.
* @throws IllegalStateException When a column header cannot be found within the enum {@link Header}.
* This should never happen, as the validation would fail before!
*/
public static List<Header> parseAndValidate(final String line) throws ParserException {
List<String> validatedColumns = Validator.validateHeaderLine(line, Block.TRIGGER, getHeaders());
public static List<Header> parseAndValidate(final String line, final Configuration config) throws ParserException {
List<String> validatedColumns = Validator.validateHeaderLine(line, getHeaders(), config);
// the IllegalStateException shall never happen, as we already validated the header!
return validatedColumns.stream()
.map(Header::getByValue)
Expand Down Expand Up @@ -133,13 +135,15 @@ public String getErrorMessage() {
* over an object to work with and containing a complete POJO representation of a (custom) metadata block.
*/
public static final class BlockBuilder {
private final Configuration config;
private final List<Header> header;
private final Map<Header,String> settings = new EnumMap<Header, String>(Header.class);
private final Map<Header,String> settings = new EnumMap<>(Header.class);
private final List<Field.FieldBuilder> fieldBuilders = new ArrayList<>();
private boolean hasErrors = false;
private boolean hasParsedALine = false;

private BlockBuilder() {
this.config = Configuration.defaultConfig();
this.header = new ArrayList<>();
}

Expand All @@ -150,8 +154,9 @@ private BlockBuilder() {
* @param header The textual line with the column headers.
* @throws ParserException
*/
public BlockBuilder(final String header) throws ParserException {
this.header = Block.Header.parseAndValidate(header);
public BlockBuilder(final String header, final Configuration config) throws ParserException {
this.config = config;
this.header = Block.Header.parseAndValidate(header, config);
}

/**
Expand Down Expand Up @@ -187,8 +192,7 @@ public void parseAndValidateLine(final String line) throws ParserException {
this.hasParsedALine = true;
}

String[] lineParts = line.split(Constants.COLUMN_SEPARATOR);
validateColumns(lineParts);
validateColumns(line.split(config.columnSeparator()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BlockTest {
"#metadataBlock\tNAME\tDataversealias\tDisplayname\tBlockURI"
})
void successfulParseAndValidateHeaderLine(String headerLine) throws ParserException {
List<Block.Header> headers = Block.Header.parseAndValidate(headerLine);
List<Block.Header> headers = Block.Header.parseAndValidate(headerLine, Configuration.defaultConfig());
assertFalse(headers.isEmpty());
assertEquals(List.of(Block.Header.values()), headers);
}
Expand All @@ -41,7 +41,7 @@ void successfulParseAndValidateHeaderLine(String headerLine) throws ParserExcept
"dataverseAlias\tdisplayName\tblockURI\t#metadataBlock\tname"
})
void failingParseAndValidateHeaderLine(String headerLine) throws ParserException {
ParserException exception = assertThrows(ParserException.class, () -> Block.Header.parseAndValidate(headerLine));
ParserException exception = assertThrows(ParserException.class, () -> Block.Header.parseAndValidate(headerLine, Configuration.defaultConfig()));
assertTrue(exception.hasSubExceptions());
logger.log(Level.INFO,
exception.getSubExceptions().stream().map(Throwable::getMessage).collect(Collectors.joining("\n"))
Expand Down

0 comments on commit 3733117

Please sign in to comment.