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

Commit

Permalink
Remove appendTo method, hide FixPath behind record (#102, #92, #89)
Browse files Browse the repository at this point in the history
Replace replace*/append* methods with insert* and InsertMode usage

Use get / set / add / remove methods on Record for interaction
  • Loading branch information
fsteeg committed Feb 28, 2022
1 parent 1a3278a commit 8c42874
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 89 deletions.
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
32 changes: 19 additions & 13 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,9 @@ 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(v -> record.add(newName, v)));
}
},
format {
Expand Down Expand Up @@ -149,8 +151,12 @@ 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);
final String oldName = params.get(0);
final String newName = params.get(1);
Value.asList(record.get(oldName), a -> a.forEach(v -> {
record.add(newName, v);
record.remove(oldName);
}));
}
},
parse_text {
Expand Down Expand Up @@ -195,10 +201,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, new Value(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))
.map(Value::asString).collect(Collectors.joining(joinChar != null ? joinChar : " "))));
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 +217,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, new Value(String.valueOf(RANDOM.nextInt(max))));
record.set(field, new Value(String.valueOf(RANDOM.nextInt(max))));
}
},
reject {
Expand Down Expand Up @@ -268,21 +274,21 @@ public void apply(final Metafix metafix, final Record record, final List<String>
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 newValue = newArray(params.subList(1, params.size()).stream().map(Value::new));
new FixPath(field).replaceIn(record, newValue);
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, new Value(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 newValue = Value.newHash(h -> options.forEach((f, v) -> h.put(f, new Value(v))));
new FixPath(field).replaceIn(record, newValue);
record.set(field, newValue);
}
},
vacuum {
Expand Down
84 changes: 31 additions & 53 deletions metafix/src/main/java/org/metafacture/metafix/FixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@
* @author Fabian Steeg (fsteeg)
*
*/
public class FixPath {
/*package-private*/ class FixPath {

private static final String ASTERISK = "*";
private String[] path;

public FixPath(final String path) {
/*package-private*/ FixPath(final String path) {
this(Value.split(path));
}

private FixPath(final String[] path) {
this.path = path;
}

public Value findIn(final Hash hash) {
/*package-private*/ Value findIn(final Hash hash) {
final String currentSegment = path[0];
final FixPath remainingPath = new FixPath(tail(path));

Expand Down Expand Up @@ -101,38 +101,16 @@ private Value findInValue(final Value value, final String[] p) {
);
}

public Value replaceIn(final Hash hash, final Value newValue) {
return new FixPath(path).insertInto(hash, InsertMode.REPLACE, newValue);
}

public Value appendIn(final Hash hash, final String newValue) {
return new FixPath(path).insertInto(hash, InsertMode.APPEND, new Value(newValue));
}

/*package-private*/ void appendIn(final Hash hash, final Value v) {
// TODO: impl and call just value.append
if (v != null) {
v.matchType()
.ifString(s -> appendIn(hash, s))
//.ifArray(a -> /* TODO: see MetafixMethodTest.moveToNestedArray */)
.ifHash(h -> {
if (path.length == 1) {
hash.add(path[0], v);
}
else {
appendIn(hash, new FixPath(tail(path)).findIn(h));
}
})
.orElseThrow();
}
}

@Override
public String toString() {
return Arrays.asList(path).toString();
}

public void transformIn(final Hash hash, final UnaryOperator<String> operator) {
/*package-private*/ int size() {
return path.length;
}

/*package-private*/ void transformIn(final Hash hash, final UnaryOperator<String> operator) {
// basic idea: reuse findIn logic here? setIn(hash, operator.apply(findIn(hash)))
final String currentSegment = path[0];
final String[] remainingPath = tail(path);
Expand All @@ -152,7 +130,7 @@ public void transformIn(final Hash hash, final UnaryOperator<String> operator) {

if (operator != null) {
value.matchType()
.ifString(s -> new FixPath(f).appendIn(hash, operator.apply(s)))
.ifString(s -> new FixPath(f).insertInto(hash, InsertMode.APPEND, new Value(operator.apply(s))))
.orElseThrow();
}
}
Expand All @@ -166,14 +144,33 @@ public void transformIn(final Hash hash, final UnaryOperator<String> operator) {
});
}

/* package-private */ enum InsertMode {

REPLACE {
@Override
void apply(final Hash hash, final String field, final Value value) {
hash.put(field, value);
}
},
APPEND {
@Override
void apply(final Hash hash, final String field, final Value value) {
hash.add(field, value);
}
};

abstract void apply(Hash hash, String field, Value value);

}

/*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) {
new FixPath(path).insertInto(hash, InsertMode.REPLACE, newValue);
insertInto(hash, InsertMode.REPLACE, newValue);
}
}
}
Expand Down Expand Up @@ -204,25 +201,6 @@ else if (Value.isNumber(currentSegment)) {
array.removeIf(v -> Value.isNull(v));
}

private enum InsertMode {

REPLACE {
@Override
void apply(final Hash hash, final String field, final Value value) {
hash.put(field, value);
}
},
APPEND {
@Override
void apply(final Hash hash, final String field, final Value value) {
hash.add(field, value);
}
};

abstract void apply(Hash hash, String field, Value value);

}

/*package-private*/ void removeNestedFrom(final Array array) {
if (path.length >= 1 && path[0].equals(ASTERISK)) {
array.removeAll();
Expand Down Expand Up @@ -253,7 +231,7 @@ else if (hash.containsField(field)) {
}
}

/*package-private*/ Value insertInto(final Array array, final InsertMode mode, final Value newValue) {
/*package-private*/ private Value insertInto(final Array array, final InsertMode mode, final Value newValue) {
// basic idea: reuse findIn logic here? setIn(findIn(array), newValue)
final String field = path[0];
if (path.length == 1) {
Expand Down Expand Up @@ -282,7 +260,7 @@ else if (hash.containsField(field)) {
//TODO: WDCD? insert into each element?
}
else {
(mode != null ? mode : InsertMode.APPEND).apply(hash, field, newValue);
mode.apply(hash, field, newValue);
}
}
else {
Expand Down
38 changes: 36 additions & 2 deletions metafix/src/main/java/org/metafacture/metafix/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package org.metafacture.metafix;

import org.metafacture.metafix.FixPath.InsertMode;

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

/**
* Represents a metadata record, i.e., a {@link Value.Hash Hash} of fields
Expand Down Expand Up @@ -112,7 +115,20 @@ public String toString() {
*/
@Override
public Value get(final String field) {
return containsField(field) ? super.get(field) : virtualFields.get(field);
final Value result;
if (containsField(field)) {
result = super.get(field);
}
else {
final FixPath fixPath = new FixPath(field);
if (fixPath.size() > 1) {
result = fixPath.findIn(this);
}
else {
result = virtualFields.get(field);
}
}
return result;
}

/**
Expand All @@ -129,10 +145,28 @@ public void add(final String field, final Value newValue) {
super.add(field, newValue);
}
else {
put(field, newValue);
final FixPath fixPath = new FixPath(field);
if (fixPath.size() > 1) {
fixPath.insertInto(this, InsertMode.APPEND, newValue);
}
else {
put(field, newValue);
}
}
}

/**
* Sets a field/value pair to this record, replacing
* any previous association of the field with a value.
*
* @param field the field name
* @param newValue the new metadata value
*/
public void set(final String field, final Value newValue) {
final FixPath fixPath = new FixPath(field);
fixPath.insertInto(this, InsertMode.REPLACE, newValue);
}

/**
* Retains only the given field/value pairs in this record. Turns
* <i>virtual</i> fields into regular metadata fields if they're not already
Expand Down
16 changes: 8 additions & 8 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ public void addAll(final Hash hash) {
* @param newValue the new metadata value
*/
public void add(final String field, final Value newValue) {
final Value oldValue = get(field);
final Value oldValue = new FixPath(field).findIn(this);
put(field, oldValue == null ? newValue : oldValue.asList(a1 -> newValue.asList(a2 -> a2.forEach(a1::add))));
}

Expand All @@ -547,19 +547,19 @@ public void add(final String field, final Value newValue) {
* @param field the field name
*/
public void remove(final String field) {
modifyFields(field, this::removeField);
final FixPath fixPath = new FixPath(field);
if (fixPath.size() > 1) {
fixPath.removeNestedFrom(this);
}
else {
modifyFields(field, this::removeField);
}
}

public void removeField(final String field) {
map.remove(field);
}

public void copy(final List<String> params) {
final String oldName = params.get(0);
final String newName = params.get(1);
asList(new FixPath(oldName).findIn(this), a -> a.forEach(v -> new FixPath(newName).appendIn(this, v)));
}

/**
* Retains only the given field/value pairs in this hash.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.metafacture.metafix.api;

import org.metafacture.metafix.FixPath;
import org.metafacture.metafix.Metafix;
import org.metafacture.metafix.Record;
import org.metafacture.metafix.Value;
Expand All @@ -43,7 +42,7 @@ default boolean testConditional(final Record record, final List<String> params,
final String field = params.get(0);
final String string = params.get(1);

final Value value = new FixPath(field).findIn(record);
final Value value = record.get(field);
return value != null && qualifier.test(value.asList(null).asArray().stream(), v -> v.extractType((m, c) -> m
.ifString(s -> c.accept(conditional.test(s, string)))
.orElse(w -> c.accept(false))
Expand Down
Loading

0 comments on commit 8c42874

Please sign in to comment.