Skip to content

Commit

Permalink
Extract lookup into FixFunction (metafacture-core#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Dec 13, 2022
1 parent c3418a4 commit 94ae82a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
62 changes: 16 additions & 46 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import org.metafacture.metamorph.functions.Timestamp;
import org.metafacture.metamorph.maps.FileMap;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Consumer;
Expand Down Expand Up @@ -96,14 +96,19 @@ public void apply(final Metafix metafix, final Record record, final List<String>
put_rdfmap {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String resourceName = params.get(0);
final RdfMap rdf = new RdfMap();
rdf.setResource(metafix.resolvePath(resourceName));
withOption(options, RdfMap.TARGET, rdf::setTarget);
withOption(options, RdfMap.TARGET_LANGUAGE, rdf::setTargetLanguage);
withOption(options, Maps.DEFAULT_MAP_KEY, rdf::setDefault);
final String mapName = (params.size() > 1 ? params.get(1) : params.get(0)) + options.get(RdfMap.TARGET) + options.getOrDefault(RdfMap.TARGET_LANGUAGE, "");
metafix.putMap(mapName, rdf);
final String rdfMapName = params.size() == 1 ? params.get(0) : params.get(1) + options.get(RdfMap.TARGET) + options.getOrDefault(RdfMap.TARGET_LANGUAGE, "");
final String replaceTargets = options.get(RdfMap.TARGET) + options.getOrDefault(RdfMap.TARGET_LANGUAGE, "");
final String resourceName = Optional.ofNullable(params.get(0))
.map(str -> str.replaceAll(replaceTargets + "$", ""))
.orElse(params.get(0));
final RdfMap rdfMap = new RdfMap();

rdfMap.setResource(metafix.resolvePath(resourceName));
withOption(options, RdfMap.TARGET, rdfMap::setTarget);
withOption(options, RdfMap.TARGET_LANGUAGE, rdfMap::setTargetLanguage);
withOption(options, Maps.DEFAULT_MAP_KEY, rdfMap::setDefault);

metafix.putMap(rdfMapName, rdfMap);
}
},
put_var {
Expand Down Expand Up @@ -484,6 +489,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>

@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
<<<<<<< HEAD
final Map<String, String> map;

if (params.size() <= 1) {
Expand Down Expand Up @@ -534,11 +540,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
lookup_rdf {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final Map<String, String> map = getMap(metafix, record, params, options, KIND_OF_RDFMAP);
record.transform(params.get(0), oldValue -> {
final String newValue = map.getOrDefault(oldValue, map.get(Maps.DEFAULT_MAP_KEY));
return newValue != null ? newValue : getBoolean(options, "delete") ? null : oldValue;
});
lookup(metafix, record, params, options, put_rdfmap);
}
},
prepend {
Expand Down Expand Up @@ -663,8 +665,6 @@ public void apply(final Metafix metafix, final Record record, final List<String>
}
};

public static final String KIND_OF_RDFMAP = "rdfmap";
public static final String KIND_OF_FILEMAP = "filemap";
private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile("\\(\\?<(.+?)>");

private static final String FILEMAP_SEPARATOR_OPTION = "sep_char";
Expand All @@ -674,35 +674,5 @@ public void apply(final Metafix metafix, final Record record, final List<String>

private static final Random RANDOM = new Random();

private static Map<String, String> getMap(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final String kindOfMap) {
final Map<String, String> map;
if (params.size() <= 1) {
map = options;
}
else {
final String mapName;
if (kindOfMap.equals(KIND_OF_FILEMAP)) {
mapName = params.get(1);
}
else {
mapName = params.get(1) + options.get(RdfMap.TARGET) + options.getOrDefault(RdfMap.TARGET_LANGUAGE, "");
}
if (!metafix.getMapNames().contains(mapName)) {
if (mapName.contains(".") || mapName.contains(File.separator)) {
if (kindOfMap.equals(KIND_OF_FILEMAP)) {
put_filemap.apply(metafix, record, Arrays.asList(mapName), options);
}
if (kindOfMap.equals(KIND_OF_RDFMAP)) {
put_rdfmap.apply(metafix, record, Arrays.asList(params.get(1)), options);
}
}
else {
// Probably an unknown internal map? Log a warning?
}
}
map = metafix.getMap(mapName);
}
return map;
}

}
32 changes: 32 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/api/FixFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import org.metafacture.metafix.Metafix;
import org.metafacture.metafix.Record;
import org.metafacture.metafix.Value;
import org.metafacture.metafix.maps.RdfMap;
import org.metafacture.metamorph.api.Maps;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -107,4 +111,32 @@ default Stream<Value> flatten(final Stream<Value> stream) {
));
}

default void lookup(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final FixFunction kindOfMap) {
final Map<String, String> map = getMap(metafix, record, params, options, kindOfMap);
record.transform(params.get(0), oldValue -> {
final String newValue = map.getOrDefault(oldValue, map.get(Maps.DEFAULT_MAP_KEY));
return newValue != null ? newValue : getBoolean(options, "delete") ? null : oldValue;
});
}

static Map<String, String> getMap(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final FixFunction kindOfMap) {
final Map<String, String> map;
if (params.size() <= 1) {
map = options;
}
else {
final String mapName = kindOfMap.toString().equals("put_rdfmap") ? (params.size() > 1 ? params.get(1) : params.get(0)) + options.get(RdfMap.TARGET) + options.getOrDefault(RdfMap.TARGET_LANGUAGE, "") : params.get(1);
if (!metafix.getMapNames().contains(mapName)) {
if (mapName.contains(".") || mapName.contains(File.separator)) {
kindOfMap.apply(metafix, record, Arrays.asList(mapName), options);
}
else {
// Probably an unknown internal map? Log a warning?
}
}
map = metafix.getMap(mapName);
}
return map;
}

}

0 comments on commit 94ae82a

Please sign in to comment.