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

Commit

Permalink
Move transform methods to Record, avoid FixPath in FixMethod (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Feb 28, 2022
1 parent c25ede3 commit 692b4f9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 57 deletions.
41 changes: 20 additions & 21 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,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 @@ -240,7 +240,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 @@ -306,19 +306,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 @@ -327,7 +327,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 @@ -338,7 +338,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 @@ -347,14 +347,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 @@ -383,14 +383,14 @@ 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), k -> map.getOrDefault(k, defaultValue));
}
},
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 @@ -399,13 +399,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 @@ -426,7 +426,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 @@ -441,7 +441,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 @@ -455,7 +455,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 @@ -464,29 +464,29 @@ 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());
}
};

Expand All @@ -498,5 +498,4 @@ public void apply(final Metafix metafix, final Record record, final List<String>
private static final Random RANDOM = new Random();

private static final Map<String, Fix> INCLUDE_FIX = new HashMap<>();

}
42 changes: 6 additions & 36 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@

import org.metafacture.metafix.Value.Array;
import org.metafacture.metafix.Value.Hash;
import org.metafacture.metafix.Value.TypeMatcher;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -117,45 +113,19 @@ public String toString() {
return path.length;
}

/*package-private*/ void transformIn(final Hash hash, final UnaryOperator<String> operator) {
final Value findIn = findIn(hash);
// System.out.println("Found in hash for '" + Arrays.asList(path) + "': " + findIn);
Value.asList(findIn, results -> {
for (int i = 0; i < results.size(); ++i) {
final Value oldValue = results.get(i);
final FixPath p = pathTo(oldValue, i);
oldValue.matchType()
.ifString(s -> {
final Value newValue = new Value(operator.apply(s));
p.insertInto(hash, InsertMode.REPLACE, newValue);
// System.out.printf("Inserted at '%s': '%s' into '%s'\n", p, newValue, hash);
})
.orElseThrow();
}
});
}

/*package-private*/ void transformIn(final Hash hash, final BiConsumer<TypeMatcher, Consumer<Value>> consumer) {
final Value oldValue = findIn(hash);

if (oldValue != null) {
final Value newValue = oldValue.extractType(consumer);

if (newValue != null) {
insertInto(hash, InsertMode.REPLACE, newValue);
}
}
}
// TODO: this is still very much work in progress, I think we should
// try to replace this with consistent usage of Value#getPath
// (e.g. take care of handling repeated fields and their paths)

private FixPath pathTo(final Value oldValue, final int i) {
/*package-private*/ FixPath to(final Value value, final int i) {
FixPath result = this;
// One *: replace with index of current result
if (countAsterisks() == 1) {
result = new FixPath(replaceInPath(ASTERISK, i));
}
// Multiple * or wildcards: use the old value's path
else if (oldValue.getPath() != null && (countAsterisks() >= 2) || hasWildcard()) {
result = new FixPath(oldValue.getPath());
else if (value.getPath() != null && (countAsterisks() >= 2) || hasWildcard()) {
result = new FixPath(value.getPath());
}
return result;
}
Expand Down
44 changes: 44 additions & 0 deletions metafix/src/main/java/org/metafacture/metafix/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package org.metafacture.metafix;

import org.metafacture.metafix.FixPath.InsertMode;
import org.metafacture.metafix.Value.TypeMatcher;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

/**
* Represents a metadata record, i.e., a {@link Value.Hash Hash} of fields
Expand Down Expand Up @@ -187,4 +190,45 @@ public void retainFields(final Collection<String> fields) {
super.retainFields(fields);
}

/**
* Transform this record by applying the given operator to all matching values for the given field.
*
* @param field The field
* @param operator The operator
*/
public void transform(final String field, final UnaryOperator<String> operator) {
final FixPath findPath = new FixPath(field);
Value.asList(findPath.findIn(this), results -> {
for (int i = 0; i < results.size(); ++i) {
final Value oldValue = results.get(i);
final FixPath insertPath = findPath.to(oldValue, i);
oldValue.matchType()
.ifString(s -> {
final Value newValue = new Value(operator.apply(s));
insertPath.insertInto(this, InsertMode.REPLACE, newValue);
})
.orElseThrow();
}
});
}

/**
* Transform this record by consuming all matching values for the given field with the given consumer.
*
* @param field The field
* @param consumer The consumer
*/
public void transform(final String field, final BiConsumer<TypeMatcher, Consumer<Value>> consumer) {
final FixPath path = new FixPath(field);
final Value oldValue = path.findIn(this);

if (oldValue != null) {
final Value newValue = oldValue.extractType(consumer);

if (newValue != null) {
path.insertInto(this, InsertMode.REPLACE, newValue);
}
}
}

}

0 comments on commit 692b4f9

Please sign in to comment.