Skip to content

Commit

Permalink
Add FileMap options to put_filemap() Fix function.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Oct 21, 2022
1 parent 0735334 commit 24647b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public void apply(final Metafix metafix, final Record record, final List<String>
fileMap.setSeparator(options.getOrDefault(FILEMAP_SEPARATOR_OPTION, FILEMAP_DEFAULT_SEPARATOR));
fileMap.setFile(metafix.resolvePath(fileName));

withOption(options, "allow_empty_values", fileMap::setAllowEmptyValues, this::getBoolean);
withOption(options, "compression", fileMap::setCompression);
withOption(options, "decompress_concatenated", fileMap::setDecompressConcatenated, this::getBoolean);
withOption(options, "encoding", fileMap::setEncoding);
withOption(options, "expected_columns", fileMap::setExpectedColumns, this::getInteger);
withOption(options, "ignore_pattern", fileMap::setIgnorePattern);
withOption(options, "key_column", fileMap::setKeyColumn, this::getInteger);
withOption(options, "value_column", fileMap::setValueColumn, this::getInteger);

metafix.putMap(params.size() > 1 ? params.get(1) : fileName, fileMap);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.stream.Stream;

Expand All @@ -33,15 +34,23 @@ public interface FixFunction {
void apply(Metafix metafix, Record record, List<String> params, Map<String, String> options);

default void withOption(final Map<String, String> options, final String key, final Consumer<String> consumer) {
withOption(options, key, consumer, Map::get);
}

default <T> void withOption(final Map<String, String> options, final String key, final Consumer<T> consumer, final BiFunction<Map<String, String>, String, T> function) {
if (options.containsKey(key)) {
consumer.accept(options.get(key));
consumer.accept(function.apply(options, key));
}
}

default boolean getBoolean(final Map<String, String> options, final String key) {
return Boolean.parseBoolean(options.get(key));
}

default int getInteger(final Map<String, String> options, final String key) {
return Integer.parseInt(options.get(key));
}

default int getInteger(final List<String> params, final int index) {
return Integer.parseInt(params.get(index));
}
Expand Down

0 comments on commit 24647b6

Please sign in to comment.