Skip to content

Commit 061dfa5

Browse files
committed
Move Fix binds to individual classes.
Mainly motivated by the ability to link to them directly in the documentation instead of having to rely on the GitHub code search which is only available to logged-in users. Also opens up the possibility to align Fix commands with Flux commands in terms of documentation and tooling.
1 parent 6878e0b commit 061dfa5

File tree

8 files changed

+249
-67
lines changed

8 files changed

+249
-67
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ end
10991099
11001100
Iterates over each element of an array. In contrast to Catmandu, it can also iterate over a single object or string.
11011101
1102-
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixBind.java+"+list+{")
1102+
[Java Code](https://github.com/metafacture/metafacture-core/blob/master/metafix/src/main/java/org/metafacture/metafix/bind/List.java)
11031103
11041104
```perl
11051105
do list(path: "<sourceField>")
@@ -1127,7 +1127,7 @@ Iterates over each _named_ element of an array (like [`do list`](#do-list) with
11271127
11281128
[Example in Playground](https://metafacture.org/playground/?example=do+list_as)
11291129
1130-
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixBind.java+"+list_as+{")
1130+
[Java Code](https://github.com/metafacture/metafacture-core/blob/master/metafix/src/main/java/org/metafacture/metafix/bind/ListAs.java)
11311131
11321132
```perl
11331133
do list_as(element_1: "<sourceField_1>"[, ...])
@@ -1160,7 +1160,7 @@ end
11601160
11611161
[Example in Playground](https://metafacture.org/playground/?example=do_once)
11621162
1163-
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixBind.java+"+once+{")
1163+
[Java Code](https://github.com/metafacture/metafacture-core/blob/master/metafix/src/main/java/org/metafacture/metafix/bind/Once.java)
11641164
11651165
In order to execute multiple blocks only once, tag them with unique identifiers:
11661166
@@ -1200,7 +1200,7 @@ call_macro("<macroName>"[, <dynamicLocalVariables>...])
12001200
12011201
[Example in Playground](https://metafacture.org/playground/?example=do_put_macro)
12021202
1203-
[Java Code](https://github.com/search?type=code&q=repo:metafacture/metafacture-core+path:FixBind.java+"+put_macro+{")
1203+
[Java Code](https://github.com/metafacture/metafacture-core/blob/master/metafix/src/main/java/org/metafacture/metafix/bind/PutMacro.java)
12041204
12051205
### Conditionals
12061206

metafix/src/main/java/org/metafacture/metafix/FixBind.java

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,93 +17,38 @@
1717
package org.metafacture.metafix;
1818

1919
import org.metafacture.metafix.api.FixContext;
20+
import org.metafacture.metafix.bind.*; // checkstyle-disable-line AvoidStarImport
2021

21-
import java.util.HashMap;
22-
import java.util.HashSet;
2322
import java.util.List;
2423
import java.util.Map;
25-
import java.util.Set;
2624

2725
public enum FixBind implements FixContext {
2826

2927
list {
3028
@Override
3129
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
32-
final String scopeVariable = options.get("var");
33-
Value.asList(record.get(options.get("path")), a -> {
34-
for (int i = 0; i < a.size(); ++i) {
35-
final Value value = a.get(i);
36-
37-
// with var -> keep full record in scope, add the var:
38-
if (scopeVariable != null) {
39-
record.put(scopeVariable, value, false);
40-
recordTransformer.transform(record);
41-
record.remove(scopeVariable);
42-
}
43-
// w/o var -> use the currently bound value as the record:
44-
else {
45-
final int index = i;
46-
47-
value.matchType()
48-
.ifHash(h -> {
49-
final Record scopeRecord = new Record();
50-
scopeRecord.addAll(h);
51-
52-
recordTransformer.transform(scopeRecord);
53-
a.set(index, new Value(scopeRecord));
54-
})
55-
// TODO: bind to arrays (if that makes sense) and strings (access with '.')
56-
.orElseThrow();
57-
}
58-
}
59-
});
30+
new org.metafacture.metafix.bind.List().execute(metafix, record, params, options, recordTransformer);
6031
}
6132
},
6233

6334
list_as {
6435
@Override
6536
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
66-
final Map<String, Value.Array> lists = new HashMap<>();
67-
options.forEach((k, v) -> Value.asList(record.get(v), a -> lists.put(k, a)));
68-
69-
final int size = lists.values().stream().mapToInt(a -> a.size()).max().orElse(0);
70-
for (int i = 0; i < size; ++i) {
71-
final int index = i;
72-
73-
lists.forEach((k, v) -> {
74-
final Value value = index < v.size() ? v.get(index) : null;
75-
76-
if (value != null) {
77-
record.put(k, value);
78-
}
79-
else {
80-
record.remove(k);
81-
}
82-
});
83-
84-
recordTransformer.transform(record);
85-
}
86-
87-
lists.keySet().forEach(record::remove);
37+
new ListAs().execute(metafix, record, params, options, recordTransformer);
8838
}
8939
},
9040

9141
once {
92-
private final Map<Metafix, Set<String>> executed = new HashMap<>();
93-
9442
@Override
9543
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
96-
if (executed.computeIfAbsent(metafix, k -> new HashSet<>()).add(params.isEmpty() ? null : params.get(0))) {
97-
recordTransformer.transform(record);
98-
}
44+
new Once().execute(metafix, record, params, options, recordTransformer);
9945
}
10046
},
10147

10248
put_macro {
10349
@Override
10450
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
105-
recordTransformer.setVars(options);
106-
metafix.putMacro(params.get(0), recordTransformer);
51+
new PutMacro().execute(metafix, record, params, options, recordTransformer);
10752
}
10853
}
10954

metafix/src/main/java/org/metafacture/metafix/RecordTransformer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,12 @@ private String executionExceptionMessage(final EObject object, final Resource re
299299
resource.getURI(), node.getStartLine(), NodeModelUtils.getTokenText(node));
300300
}
301301

302-
/*package-private*/ void setVars(final Map<String, String> staticVars) {
302+
/**
303+
* Sets static variables.
304+
*
305+
* @param staticVars the static variables
306+
*/
307+
public void setVars(final Map<String, String> staticVars) {
303308
setVars(Vars.STATIC, staticVars);
304309
}
305310

metafix/src/main/java/org/metafacture/metafix/Value.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,14 @@ public void remove(final int index) {
782782
list.remove(index);
783783
}
784784

785-
/*package-private*/ void set(final int index, final Value value) {
785+
/**
786+
* Sets the Array element at the given index to the given Value and
787+
* appends the index number to its path.
788+
*
789+
* @param index the index
790+
* @param value the Value
791+
*/
792+
public void set(final int index, final Value value) {
786793
list.set(index, value.withPathAppend(index + 1));
787794
}
788795

@@ -893,7 +900,14 @@ public void put(final String field, final Value value) {
893900
put(field, value, true);
894901
}
895902

896-
/*package-private*/ void put(final String field, final Value value, final boolean appendToPath) {
903+
/**
904+
* Adds a field/value pair to this hash, provided it's not {@link #isNull(Value) null}.
905+
*
906+
* @param field the field name
907+
* @param value the metadata value
908+
* @param appendToPath whether to append the field name to the Value's path
909+
*/
910+
public void put(final String field, final Value value, final boolean appendToPath) {
897911
if (!isNull(value)) {
898912
map.put(field, appendToPath ? value.withPathAppend(field) : value);
899913
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2025 hbz NRW
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.metafix.bind;
18+
19+
import org.metafacture.metafix.Metafix;
20+
import org.metafacture.metafix.Record;
21+
import org.metafacture.metafix.RecordTransformer;
22+
import org.metafacture.metafix.Value;
23+
import org.metafacture.metafix.api.FixContext;
24+
25+
import java.util.Map;
26+
27+
public class List implements FixContext {
28+
29+
/**
30+
* Creates an instance of {@link List}.
31+
*/
32+
public List() {
33+
}
34+
35+
@Override
36+
public void execute(final Metafix metafix, final Record record, final java.util.List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
37+
final String scopeVariable = options.get("var");
38+
Value.asList(record.get(options.get("path")), a -> {
39+
for (int i = 0; i < a.size(); ++i) {
40+
final Value value = a.get(i);
41+
42+
// with var -> keep full record in scope, add the var:
43+
if (scopeVariable != null) {
44+
record.put(scopeVariable, value, false);
45+
recordTransformer.transform(record);
46+
record.remove(scopeVariable);
47+
}
48+
// w/o var -> use the currently bound value as the record:
49+
else {
50+
final int index = i;
51+
52+
value.matchType()
53+
.ifHash(h -> {
54+
final Record scopeRecord = new Record();
55+
scopeRecord.addAll(h);
56+
57+
recordTransformer.transform(scopeRecord);
58+
a.set(index, new Value(scopeRecord));
59+
})
60+
// TODO: bind to arrays (if that makes sense) and strings (access with '.')
61+
.orElseThrow();
62+
}
63+
}
64+
});
65+
}
66+
67+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 hbz NRW
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.metafix.bind;
18+
19+
import org.metafacture.metafix.Metafix;
20+
import org.metafacture.metafix.Record;
21+
import org.metafacture.metafix.RecordTransformer;
22+
import org.metafacture.metafix.Value;
23+
import org.metafacture.metafix.api.FixContext;
24+
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
public class ListAs implements FixContext {
30+
31+
/**
32+
* Creates an instance of {@link ListAs}.
33+
*/
34+
public ListAs() {
35+
}
36+
37+
@Override
38+
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
39+
final Map<String, Value.Array> lists = new HashMap<>();
40+
options.forEach((k, v) -> Value.asList(record.get(v), a -> lists.put(k, a)));
41+
42+
final int size = lists.values().stream().mapToInt(a -> a.size()).max().orElse(0);
43+
for (int i = 0; i < size; ++i) {
44+
final int index = i;
45+
46+
lists.forEach((k, v) -> {
47+
final Value value = index < v.size() ? v.get(index) : null;
48+
49+
if (value != null) {
50+
record.put(k, value);
51+
}
52+
else {
53+
record.remove(k);
54+
}
55+
});
56+
57+
recordTransformer.transform(record);
58+
}
59+
60+
lists.keySet().forEach(record::remove);
61+
}
62+
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 hbz NRW
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.metafacture.metafix.bind;
18+
19+
import org.metafacture.metafix.Metafix;
20+
import org.metafacture.metafix.Record;
21+
import org.metafacture.metafix.RecordTransformer;
22+
import org.metafacture.metafix.api.FixContext;
23+
24+
import java.util.HashMap;
25+
import java.util.HashSet;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
29+
30+
public class Once implements FixContext {
31+
32+
private static final Map<Metafix, Set<String>> EXECUTED = new HashMap<>();
33+
34+
/**
35+
* Creates an instance of {@link Once}.
36+
*/
37+
public Once() {
38+
}
39+
40+
@Override
41+
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
42+
if (EXECUTED.computeIfAbsent(metafix, k -> new HashSet<>()).add(params.isEmpty() ? null : params.get(0))) {
43+
recordTransformer.transform(record);
44+
}
45+
}
46+
47+
}

0 commit comments

Comments
 (0)