Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions add_array and add_hash #374 #379

Merged
merged 10 commits into from
Nov 7, 2024
28 changes: 20 additions & 8 deletions metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,29 @@ public void apply(final Metafix metafix, final Record record, final List<String>

// RECORD-LEVEL METHODS:

add_array {
@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 = newArray(params.subList(1, params.size()).stream().map(Value::new));
record.set(field, newValue);
newValue.asArray().forEach(value -> value.withPathSet(newValue.getPath() + "." + value.getPath()));
}
},
add_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.set(params.get(0), new Value(params.get(1)));
}
},
add_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))));
record.set(field, newValue);
}
},
array { // array-from-hash
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
Expand Down Expand Up @@ -391,24 +408,19 @@ public void apply(final Metafix metafix, final Record record, final List<String>
set_array {
@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 = newArray(params.subList(1, params.size()).stream().map(Value::new));
record.set(field, newValue);
newValue.asArray().forEach(value -> value.withPathSet(newValue.getPath() + "." + value.getPath()));
add_array.apply(metafix, record, params, options);
}
},
set_field {
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
record.set(params.get(0), new Value(params.get(1)));
add_field.apply(metafix, record, params, options);
}
},
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))));
record.set(field, newValue);
add_hash.apply(metafix, record, params, options);
}
},
timestamp {
Expand Down
Loading