Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Merge pull request #157 from metafacture/resolveRelativeFileMapPaths
Browse files Browse the repository at this point in the history
Apply relative path "magic" to file maps.
  • Loading branch information
blackwinter authored Feb 17, 2022
2 parents 53f59e3 + db056fc commit 0212600
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
20 changes: 3 additions & 17 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.metafacture.metamorph.api.Maps;
import org.metafacture.metamorph.maps.FileMap;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -44,26 +43,13 @@ public enum FixMethod implements FixFunction {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String includeFile = params.get(0);
final String includePath;

if (!Metafix.isFixFile(includeFile)) {
throw new IllegalArgumentException("Not a Fix file: " + includeFile);
}

// TODO: Catmandu load path
if (includeFile.startsWith(".")) {
final String fixFile = metafix.getFixFile();

if (fixFile != null) {
includePath = Paths.get(fixFile).resolveSibling(includeFile).toString();
}
else {
throw new IllegalArgumentException("Cannot resolve relative path: " + includeFile);
}
}
else {
includePath = includeFile;
}
final String includePath = metafix.resolvePath(includeFile);

final RecordTransformer recordTransformer = metafix.getRecordTransformer();
recordTransformer.setRecord(recordTransformer.transformRecord(
Expand All @@ -83,9 +69,9 @@ public void apply(final Metafix metafix, final Record record, final List<String>
final FileMap fileMap = new FileMap();

fileMap.setSeparator(options.getOrDefault(FILEMAP_SEPARATOR_OPTION, FILEMAP_DEFAULT_SEPARATOR));
fileMap.setFile(fileName);
fileMap.setFile(metafix.resolvePath(fileName));

metafix.putMap(params.size() <= 1 ? fileName : params.get(1), fileMap);
metafix.putMap(params.size() > 1 ? params.get(1) : fileName, fileMap);
}
},
put_map {
Expand Down
15 changes: 13 additions & 2 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -126,8 +127,18 @@ public Metafix(final Reader fixDef, final Map<String, String> vars) {
return fixDef.endsWith(FIX_EXTENSION);
}

/*package-private*/ String getFixFile() {
return fixFile;
public String resolvePath(final String path) {
if (path.startsWith(".")) {
if (fixFile != null) {
return Paths.get(fixFile).resolveSibling(path).toString();
}
else {
throw new IllegalArgumentException("Cannot resolve relative path: " + path);
}
}
else {
return path;
}
}

public RecordTransformer getRecordTransformer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ public void shouldLookupInSeparateExternalFileMap() {
);
}

@Test
public void shouldNotLookupInRelativeExternalFileMapFromInlineScript() {
final String mapFile = "../maps/test.csv";

MetafixTestHelpers.assertThrowsCause(IllegalArgumentException.class, "Cannot resolve relative path: " + mapFile, () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
LOOKUP + " '" + mapFile + "')"
),
i -> {
i.startRecord("");
i.endRecord();
},
o -> {
}
)
);
}

@Test
public void shouldLookupInRelativeExternalFileMapFromExternalScript() {
assertMap(
"src/test/resources/org/metafacture/metafix/fixes/filemap_lookup.fix"
);
}

@Test
public void shouldLookupInSeparateExternalFileMapWithName() {
assertMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ public void shouldPutExternalFileMap() {
assertMap("put_filemap('" + CSV_MAP + "')", CSV_MAP);
}

@Test
public void shouldNotPutRelativeExternalFileMapFromInlineScript() {
final String mapFile = "../maps/test.csv";

MetafixTestHelpers.assertThrowsCause(IllegalArgumentException.class, "Cannot resolve relative path: " + mapFile, () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"put_filemap('" + mapFile + "')"
),
i -> {
i.startRecord("");
i.endRecord();
},
o -> {
}
)
);
}

@Test
public void shouldPutRelativeExternalFileMapFromExternalScript() {
assertMap("src/test/resources/org/metafacture/metafix/fixes/filemap.fix", MAP_NAME);
}

@Test
public void shouldPutMultipleExternalFileMaps() {
assertFix("put_filemap('" + CSV_MAP + "')\nput_filemap('" + TSV_MAP + "')", f -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
put_filemap("../maps/test.csv", "testMap")
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lookup("title.*", "../maps/test.csv")

0 comments on commit 0212600

Please sign in to comment.