Skip to content

Commit

Permalink
Merge pull request #379 from metafacture/374-introduceAddArrayAndAddHash
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasNx authored Nov 7, 2024
2 parents 8ea116a + 511347b commit 3bb2b28
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
56 changes: 32 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,19 @@ Options:

#### Record-level functions

##### `add_array`

Creates a new array (with optional values).

```perl
add_array("<targetFieldName>")
add_array("<targetFieldName>", "<value_1>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=add_array)

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_array+{")

##### `add_field`

Creates a field with a defined value.
Expand All @@ -315,6 +328,19 @@ add_field("<targetFieldName>", "<fieldValue>")

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_field+{")

##### `add_hash`

Creates a new hash (with optional values).

```perl
add_hash("<targetFieldName>")
add_hash("<targetFieldName>", "subfieldName": "<subfieldValue>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=add_hash)

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+add_hash+{")

##### `array`

Converts a hash/object into an array.
Expand Down Expand Up @@ -535,39 +561,21 @@ retain("<sourceField_1>"[, ...])

##### `set_array`

Creates a new array (with optional values).
_Currently alias for [`add_array`](#add_array)._

```perl
set_array("<targetFieldName>")
set_array("<targetFieldName>", "<value_1>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=set_array)

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+set_array+{")
We advise you to use [`add_array`](#add_array) instead of `set_array` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309)

##### `set_field`

Creates (or replaces) a field with a defined value.

```perl
set_field("<targetFieldName>", "<fieldValue>")
```
_Currently alias for [`add_field`](#add_field)._

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+set_field+{")
We advise you to use [`add_field`](#add_field) instead of `set_field` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309)

##### `set_hash`

Creates a new hash (with optional values).

```perl
set_hash("<targetFieldName>")
set_hash("<targetFieldName>", "subfieldName": "<subfieldValue>"[, ...])
```

[Example in Playground](https://metafacture.org/playground/?example=set_hash)
_Currently alias for [`add_hash`](#add_hash)._

[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-fix+path:FixMethod.java+"+set_hash+{")
We advise you to use [`add_hash`](#add_hash) instead of `set_hash` due to changing behaviour in an upcoming release. For more information see: [#309](https://github.com/metafacture/metafacture-fix/issues/309)

##### `timestamp`

Expand Down
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

0 comments on commit 3bb2b28

Please sign in to comment.