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 #170 from metafacture/102-path
Browse files Browse the repository at this point in the history
Consolidate path traversal
  • Loading branch information
fsteeg authored Apr 8, 2022
2 parents c25517a + 5a3fa9b commit 15e2237
Show file tree
Hide file tree
Showing 50 changed files with 2,005 additions and 505 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ editorconfig {
excludes = [
'**/*.hprof.txt',
'**/*.out',
'**/*.err',
'**/*.vsix',
'**/.*',
'**/bin',
Expand All @@ -22,7 +23,7 @@ editorconfig {

task editorconfigChecker(type: Exec, group: 'Verification') {
executable 'misc/bin/editorconfig-checker'
args('-exclude', '/bin/|/\\.|^gradlew.*|^LICENSE$')
args('-exclude', '/bin/|/\\.|^gradlew.*|^LICENSE$|^\\.project$')
}

editorconfigCheck.dependsOn(editorconfigChecker)
Expand Down
2 changes: 2 additions & 0 deletions metafix-runner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ plugins {
dependencies {
implementation project(':metafix')

implementation "org.metafacture:metafacture-biblio:${versions.metafacture}"
implementation "org.metafacture:metafacture-json:${versions.metafacture}"
implementation "org.metafacture:metafacture-runner:${versions.metafacture}"
implementation "org.metafacture:metafacture-xml:${versions.metafacture}"
}

application {
Expand Down
2 changes: 1 addition & 1 deletion metafix/src/main/java/org/metafacture/metafix/FixBind.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum FixBind implements FixContext {
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final List<Expression> expressions) {
final RecordTransformer recordTransformer = metafix.getRecordTransformer();
final String scopeVariable = options.get("var");
Value.asList(new FixPath(options.get("path")).findIn(record), a -> {
Value.asList(record.get(options.get("path")), a -> {
for (int i = 0; i < a.size(); ++i) {
final Value value = a.get(i);

Expand Down
90 changes: 41 additions & 49 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
add_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).appendIn(record, params.get(1));
record.add(params.get(0), new Value(params.get(1)));
}
},
array { // array-from-hash
Expand All @@ -120,7 +120,12 @@ public void apply(final Metafix metafix, final Record record, final List<String>
copy_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.copy(params);
final String oldName = params.get(0);
final String newName = params.get(1);
Value.asList(record.get(oldName), a -> a.forEach(newValue -> {
record.add(newName, newValue);
newValue.updatePathRename(newName);
}));
}
},
format {
Expand Down Expand Up @@ -149,8 +154,8 @@ public void apply(final Metafix metafix, final Record record, final List<String>
move_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.copy(params);
new FixPath(params.get(0)).removeNestedFrom(record);
FixMethod.copy_field.apply(metafix, record, params, options);
record.remove(params.get(0));
}
},
parse_text {
Expand Down Expand Up @@ -195,10 +200,11 @@ 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) {
final String joinChar = options.get("join_char");
new FixPath(params.get(0)).replaceIn(record, params.subList(1, params.size()).stream()
.filter(f -> literalString(f) || new FixPath(f).findIn(record) != null)
.map(f -> literalString(f) ? new Value(f.substring(1)) : Value.asList(new FixPath(f).findIn(record), null).asArray().get(0))
final Value newValue = new Value(params.subList(1, params.size()).stream()
.filter(f -> literalString(f) || record.get(f) != null)
.map(f -> literalString(f) ? new Value(f.substring(1)) : Value.asList(record.get(f), null).asArray().get(0))
.map(Value::asString).collect(Collectors.joining(joinChar != null ? joinChar : " ")));
record.set(params.get(0), newValue);
}

private boolean literalString(final String s) {
Expand All @@ -210,8 +216,7 @@ private boolean literalString(final String s) {
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String field = params.get(0);
final int max = getInteger(params, 1);

new FixPath(field).replaceIn(record, String.valueOf(RANDOM.nextInt(max)));
record.set(field, new Value(String.valueOf(RANDOM.nextInt(max))));
}
},
reject {
Expand All @@ -223,7 +228,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
remove_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
params.forEach(p -> new FixPath(p).removeNestedFrom(record));
params.forEach(p -> record.remove(p));
}
},
rename {
Expand All @@ -234,7 +239,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>

final UnaryOperator<String> operator = s -> s.replaceAll(search, replace);

new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(renameArray(a, operator)))
.ifHash(h -> c.accept(renameHash(h, operator)))
.orElseThrow()
Expand Down Expand Up @@ -267,35 +272,22 @@ 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) {
final String field = params.get(0);
final List<String> toAdd = params.subList(1, params.size());
if (field.endsWith(DOT_APPEND)) {
record.addAll(field.replace(DOT_APPEND, EMPTY), toAdd);
}
else {
record.put(field, newArray(toAdd.stream().map(Value::new)));
}
final Value newValue = newArray(params.subList(1, params.size()).stream().map(Value::new));
record.set(field, newValue);
}
},
set_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).replaceIn(record, params.get(1));
record.set(params.get(0), new Value(params.get(1)));
}
},
set_hash {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String field = params.get(0);

final Value value = record.get(field.replace(DOT_APPEND, EMPTY));
final Value newValue = Value.newHash(h -> options.forEach((f, v) -> h.put(f, new Value(v))));

if (field.endsWith(DOT_APPEND) && value.isArray()) {
value.asArray().add(newValue);
}
else {
record.put(field, newValue);
}
record.set(field, newValue);
}
},
vacuum {
Expand All @@ -313,19 +305,19 @@ 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) {
final String value = params.get(1);
new FixPath(params.get(0)).transformIn(record, s -> s + value);
record.transform(params.get(0), s -> s + value);
}
},
capitalize {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, s -> s.substring(0, 1).toUpperCase() + s.substring(1));
record.transform(params.get(0), s -> s.substring(0, 1).toUpperCase() + s.substring(1));
}
},
count {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(new Value(a.size())))
.ifHash(h -> c.accept(new Value(h.size())))
);
Expand All @@ -334,7 +326,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
downcase {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, s -> s.toLowerCase());
record.transform(params.get(0), s -> s.toLowerCase());
}
},
filter {
Expand All @@ -345,7 +337,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>

final Predicate<Value> predicate = s -> search.matcher(s.asString()).find();

new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(newArray(a.stream().filter(invert ? predicate.negate() : predicate))))
);
}
Expand All @@ -354,14 +346,14 @@ 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) {
final String search = params.get(1);
new FixPath(params.get(0)).transformIn(record, s -> String.valueOf(s.indexOf(search))); // TODO: multiple
record.transform(params.get(0), s -> String.valueOf(s.indexOf(search))); // TODO: multiple
}
},
join_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String joinChar = params.size() > 1 ? params.get(1) : "";
new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(new Value(a.stream().map(Value::asString).collect(Collectors.joining(joinChar)))))
);
}
Expand Down Expand Up @@ -390,14 +382,17 @@ public void apply(final Metafix metafix, final Record record, final List<String>
}

final String defaultValue = map.get(Maps.DEFAULT_MAP_KEY); // TODO: Catmandu uses 'default'
new FixPath(params.get(0)).transformIn(record, k -> map.getOrDefault(k, defaultValue));
record.transform(params.get(0), oldValue -> {
final String newValue = map.getOrDefault(oldValue, defaultValue);
return newValue != null ? newValue : getBoolean(options, "delete") ? null : oldValue;
});
}
},
prepend {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final String value = params.get(1);
new FixPath(params.get(0)).transformIn(record, s -> value + s);
record.transform(params.get(0), s -> value + s);
}
},
replace_all {
Expand All @@ -406,13 +401,13 @@ public void apply(final Metafix metafix, final Record record, final List<String>
final String search = params.get(1);
final String replace = params.get(2);

new FixPath(params.get(0)).transformIn(record, s -> s.replaceAll(search, replace));
record.transform(params.get(0), s -> s.replaceAll(search, replace));
}
},
reverse {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> {
final List<Value> list = a.stream().collect(Collectors.toList());
Collections.reverse(list);
Expand All @@ -433,7 +428,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
final Comparator<Value> comparator = numeric ?
Comparator.comparing(function.andThen(Integer::parseInt)) : Comparator.comparing(function);

new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(new Value((uniq ? unique(a.stream()) : a.stream())
.sorted(reverse ? comparator.reversed() : comparator).collect(Collectors.toList()))))
);
Expand All @@ -448,7 +443,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
final Function<String, Value> splitFunction = s ->
newArray(Arrays.stream(splitPattern.split(s)).map(Value::new));

new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(newArray(a.stream().map(Value::asString).map(splitFunction))))
.ifHash(h -> c.accept(Value.newHash(n -> h.forEach((f, w) -> n.put(f, splitFunction.apply(w.asString()))))))
.ifString(s -> c.accept(splitFunction.apply(s)))
Expand All @@ -462,7 +457,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
final Integer end = params.size() > 2 ? offset + getInteger(params, 2) : null;
// TODO: final String replacement = params.size() > 3 ? params.get(3) : null;

new FixPath(params.get(0)).transformIn(record, s -> {
record.transform(params.get(0), s -> {
final int length = s.length();
return offset > length ? s : end == null || end > length ? s.substring(offset) : s.substring(offset, end);
});
Expand All @@ -471,37 +466,34 @@ public void apply(final Metafix metafix, final Record record, final List<String>
sum {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(new Value(a.stream().map(Value::asString).mapToInt(Integer::parseInt).sum())))
);
}
},
trim {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, String::trim);
record.transform(params.get(0), String::trim);
}
},
uniq {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, (m, c) -> m
record.transform(params.get(0), (m, c) -> m
.ifArray(a -> c.accept(newArray(unique(a.stream()))))
);
}
},
upcase {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
new FixPath(params.get(0)).transformIn(record, s -> s.toUpperCase());
record.transform(params.get(0), s -> s.toUpperCase());
}
};

private static final Pattern NAMED_GROUP_PATTERN = Pattern.compile("\\(\\?<(.+?)>");

private static final String EMPTY = "";
private static final String DOT_APPEND = "." + Value.ReservedField.$append.name();

private static final String FILEMAP_SEPARATOR_OPTION = "sep_char";
private static final String FILEMAP_DEFAULT_SEPARATOR = ",";

Expand Down
Loading

0 comments on commit 15e2237

Please sign in to comment.