Skip to content

Commit acdffe9

Browse files
committed
Switch Fix command resolution from enums to registry.
Allowing additional commands to be registered by other Metafacture modules as well as by third-party libraries/applications. Also opens up the possibility to eventually enable Flux command registration in the same vein. The command registry is scoped by Metafix instance in order to avoid global state.
1 parent eb4d9b5 commit acdffe9

File tree

92 files changed

+807
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+807
-7
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
@Documented
26+
@Retention(RetentionPolicy.RUNTIME)
27+
@Target(ElementType.TYPE)
28+
public @interface FixCommand {
29+
30+
/**
31+
* Returns the Fix command name.
32+
*
33+
* @return Fix command name
34+
*/
35+
String value();
36+
37+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.metafacture.framework.annotations.Out;
2929
import org.metafacture.framework.helpers.DefaultStreamReceiver;
3030
import org.metafacture.mangling.StreamFlattener;
31+
import org.metafacture.metafix.api.FixRegistry;
3132
import org.metafacture.metafix.fix.Expression;
3233
import org.metafacture.metamorph.api.Maps;
3334

@@ -83,6 +84,7 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {
8384
private static final String ENTITIES_NOT_BALANCED = "Entity starts and ends are not balanced";
8485

8586
private final Deque<Integer> entityCountStack = new LinkedList<>();
87+
private final FixRegistry registry = new FixRegistry();
8688
private final List<Closeable> resources = new ArrayList<>();
8789
private final List<Expression> expressions = new ArrayList<>();
8890
private final Map<String, Map<String, String>> maps = new HashMap<>();
@@ -308,6 +310,15 @@ public List<Expression> getExpressions() {
308310
return expressions;
309311
}
310312

313+
/**
314+
* Returns the Fix registry.
315+
*
316+
* @return the Fix registry
317+
*/
318+
public FixRegistry getRegistry() {
319+
return registry;
320+
}
321+
311322
@Override
312323
public void startRecord(final String identifier) {
313324
currentRecord = new Record();

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.metafacture.metafix.api.FixContext;
2323
import org.metafacture.metafix.api.FixFunction;
2424
import org.metafacture.metafix.api.FixPredicate;
25+
import org.metafacture.metafix.api.FixRegistry;
2526
import org.metafacture.metafix.fix.Do;
2627
import org.metafacture.metafix.fix.ElsIf;
2728
import org.metafacture.metafix.fix.Else;
@@ -62,6 +63,7 @@ public class RecordTransformer { // checkstyle-disable-line ClassFanOutComplexit
6263

6364
private static final Logger LOG = LoggerFactory.getLogger(RecordTransformer.class);
6465

66+
private final FixRegistry registry;
6567
private final List<Consumer<Record>> consumers = new LinkedList<>();
6668
private final List<Map<String, String>> vars = new ArrayList<>(Collections.nCopies(Vars.values().length, null));
6769
private final Metafix metafix;
@@ -82,6 +84,7 @@ private enum Vars {
8284
private RecordTransformer(final Metafix metafix, final List<Expression> expressions, final RecordTransformer parent) {
8385
this.metafix = metafix;
8486
this.parent = parent;
87+
this.registry = metafix.getRegistry();
8588

8689
expressions.forEach(e -> {
8790
final Params params = new Params(e.getParams(), this);
@@ -143,7 +146,7 @@ public void transform(final Record record) {
143146

144147
private void processDo(final Do expression, final Params params, final Options options) {
145148
processFix(() -> executionExceptionMessage(expression), () -> {
146-
final FixContext context = getInstance(expression.getName(), FixContext.class, FixBind::valueOf);
149+
final FixContext context = getInstance(expression.getName(), FixContext.class, registry::getBind);
147150
final RecordTransformer recordTransformer = childTransformer(expression.getElements());
148151

149152
return record -> context.execute(metafix, record, params.resolve(), options.resolve(), recordTransformer);
@@ -158,10 +161,10 @@ private void processIf(final If ifExpression, final Params ifParams, final Optio
158161
final Supplier<String> elseMessageSupplier = () -> executionExceptionMessage(elseExpression, elseExpression.eResource());
159162

160163
processFix(() -> executionExceptionMessage(ifExpression, ifExpression.eResource()), () -> {
161-
final FixPredicate ifPredicate = getInstance(ifExpression.getName(), FixPredicate.class, FixConditional::valueOf);
164+
final FixPredicate ifPredicate = getInstance(ifExpression.getName(), FixPredicate.class, registry::getConditional);
162165
final RecordTransformer ifTransformer = childTransformer(ifExpression.getElements());
163166

164-
final List<FixPredicate> elseIfPredicates = mapList(elseIfExpressions, e -> getInstance(e.getName(), FixPredicate.class, FixConditional::valueOf));
167+
final List<FixPredicate> elseIfPredicates = mapList(elseIfExpressions, e -> getInstance(e.getName(), FixPredicate.class, registry::getConditional));
165168
final List<Params> elseIfParamsList = mapList(elseIfExpressions, e -> new Params(e.getParams(), this));
166169
final List<Options> elseIfOptionsList = mapList(elseIfExpressions, e -> new Options(e.getOptions(), this));
167170
final List<RecordTransformer> elseIfTransformers = mapList(elseIfExpressions, e -> childTransformer(e.getElements()));
@@ -200,7 +203,7 @@ private void processIf(final If ifExpression, final Params ifParams, final Optio
200203

201204
private void processUnless(final Unless expression, final Params params, final Options options) {
202205
processFix(() -> executionExceptionMessage(expression, expression.eResource()), () -> {
203-
final FixPredicate predicate = getInstance(expression.getName(), FixPredicate.class, FixConditional::valueOf);
206+
final FixPredicate predicate = getInstance(expression.getName(), FixPredicate.class, registry::getConditional);
204207
final RecordTransformer recordTransformer = childTransformer(expression.getElements());
205208

206209
return record -> {
@@ -213,7 +216,7 @@ private void processUnless(final Unless expression, final Params params, final O
213216

214217
private void processFunction(final MethodCall expression, final Params params, final Options options) {
215218
processFix(() -> executionExceptionMessage(expression), () -> {
216-
final FixFunction function = getInstance(expression.getName(), FixFunction.class, FixMethod::valueOf);
219+
final FixFunction function = getInstance(expression.getName(), FixFunction.class, registry::getMethod);
217220
return record -> function.apply(metafix, record, params.resolve(), options.resolve());
218221
});
219222
}

0 commit comments

Comments
 (0)