Skip to content

Commit 1d06fef

Browse files
committed
Integrate feedback from #243.
1 parent df8395c commit 1d06fef

File tree

4 files changed

+92
-90
lines changed

4 files changed

+92
-90
lines changed

README.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,25 @@ array("foo")
234234
# {"name":"value"} => ["name", "value"]
235235
```
236236

237+
##### `call_macro`
238+
239+
Calls a named macro, i.e. a list of statements that have been previously defined with the [`do put_macro`](#do-put_macro) bind.
240+
241+
Parameters:
242+
243+
- `name` (required): Unique name of the macro.
244+
245+
Options:
246+
247+
- All options are made available as "dynamic" local variables in the macro.
248+
249+
```perl
250+
do put_macro("<macroName>"[, <staticLocalVariables>...])
251+
...
252+
end
253+
call_macro("<macroName>"[, <dynamicLocalVariables>...])
254+
```
255+
237256
##### `copy_field`
238257

239258
Copies (or appends to) a field from an existing field.
@@ -266,25 +285,6 @@ hash("foo")
266285
# ["name", "value"] => {"name":"value"}
267286
```
268287

269-
##### `macro`
270-
271-
Calls a named macro, i.e. a list of statements that have been previously defined with the [`do macro`](#do-macro) bind.
272-
273-
Parameters:
274-
275-
- `name` (required): Unique name of the macro.
276-
277-
Options:
278-
279-
- All options are made available as "dynamic" local variables.
280-
281-
```perl
282-
do macro("<macroName>"[, <staticLocalVariables>...])
283-
...
284-
end
285-
macro("<macroName>"[, <dynamicLocalVariables>...])
286-
```
287-
288288
##### `move_field`
289289

290290
Moves (or appends to) a field from an existing field. Can be used to rename a field.
@@ -632,25 +632,6 @@ do list(path: "<sourceField>", "var": "<variableName>")
632632
end
633633
```
634634

635-
#### `do macro`
636-
637-
Defines a named macro, i.e. a list of statements that can be executed later with the [`macro`](#macro) function.
638-
639-
Parameters:
640-
641-
- `name` (required): Unique name of the macro.
642-
643-
Options:
644-
645-
- All options are made available as "static" local variables.
646-
647-
```perl
648-
do macro("<macroName>"[, <staticLocalVariables>...])
649-
...
650-
end
651-
macro("<macroName>"[, <dynamicLocalVariables>...])
652-
```
653-
654635
#### `do once`
655636

656637
Executes the statements only once (when the bind is first encountered), not repeatedly for each record.
@@ -672,6 +653,25 @@ do once("vars setup")
672653
end
673654
```
674655

656+
#### `do put_macro`
657+
658+
Defines a named macro, i.e. a list of statements that can be executed later with the [`call_macro`](#call_macro) function.
659+
660+
Parameters:
661+
662+
- `name` (required): Unique name of the macro.
663+
664+
Options:
665+
666+
- All options are made available as "static" local variables in the macro.
667+
668+
```perl
669+
do put_macro("<macroName>"[, <staticLocalVariables>...])
670+
...
671+
end
672+
call_macro("<macroName>"[, <dynamicLocalVariables>...])
673+
```
674+
675675
### Conditionals
676676

677677
Conditionals start with `if` in case of affirming the condition or `unless` rejecting the condition.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ public void execute(final Metafix metafix, final Record record, final List<Strin
6060
}
6161
},
6262

63-
macro {
64-
@Override
65-
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
66-
recordTransformer.addVars(options);
67-
metafix.putMacro(params.get(0), recordTransformer);
68-
}
69-
},
70-
7163
once {
7264
private final Map<Metafix, Set<String>> executed = new HashMap<>();
7365

@@ -77,6 +69,14 @@ public void execute(final Metafix metafix, final Record record, final List<Strin
7769
recordTransformer.transform(record);
7870
}
7971
}
72+
},
73+
74+
put_macro {
75+
@Override
76+
public void execute(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options, final RecordTransformer recordTransformer) {
77+
recordTransformer.addVars(options);
78+
metafix.putMacro(params.get(0), recordTransformer);
79+
}
8080
}
8181

8282
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ public void apply(final Metafix metafix, final Record record, final List<String>
120120
})));
121121
}
122122
},
123+
call_macro {
124+
@Override
125+
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
126+
final String macroName = params.get(0);
127+
final RecordTransformer recordTransformer = metafix.getMacro(macroName);
128+
129+
if (recordTransformer != null) {
130+
recordTransformer.transform(record, options);
131+
}
132+
else {
133+
throw new IllegalArgumentException("Macro '" + macroName + "' undefined!");
134+
}
135+
}
136+
},
123137
copy_field {
124138
@Override
125139
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
@@ -153,18 +167,6 @@ public void apply(final Metafix metafix, final Record record, final List<String>
153167
})));
154168
}
155169
},
156-
macro {
157-
@Override
158-
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
159-
final RecordTransformer recordTransformer = metafix.getMacro(params.get(0));
160-
if (recordTransformer != null) {
161-
recordTransformer.transform(record, options);
162-
}
163-
else {
164-
// TODO?: Metamorph throws MorphBuildException("Macro '" + macroName + "' undefined!")
165-
}
166-
}
167-
},
168170
move_field {
169171
@Override
170172
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {

metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,11 +1992,11 @@ public void arrayFromHash() {
19921992
@Test
19931993
public void shouldCallMacro() {
19941994
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1995-
"do macro('test')",
1995+
"do put_macro('test')",
19961996
" add_field('test', '42')",
19971997
"end",
1998-
"macro('test')",
1999-
"macro('test')"
1998+
"call_macro('test')",
1999+
"call_macro('test')"
20002000
),
20012001
i -> {
20022002
i.startRecord("1");
@@ -2012,30 +2012,30 @@ public void shouldCallMacro() {
20122012

20132013
@Test
20142014
public void shouldNotCallUnknownMacro() {
2015-
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2016-
"macro('test')",
2017-
"macro('test')"
2018-
),
2019-
i -> {
2020-
i.startRecord("1");
2021-
i.endRecord();
2022-
},
2023-
o -> {
2024-
o.get().startRecord("1");
2025-
o.get().endRecord();
2026-
}
2015+
MetafixTestHelpers.assertProcessException(IllegalArgumentException.class, "Macro 'test' undefined!", () ->
2016+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2017+
"call_macro('test')",
2018+
"call_macro('test')"
2019+
),
2020+
i -> {
2021+
i.startRecord("1");
2022+
i.endRecord();
2023+
},
2024+
o -> {
2025+
}
2026+
)
20272027
);
20282028
}
20292029

20302030
@Test
20312031
public void shouldCallMacroWithVariables() {
20322032
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
20332033
"put_vars(a: '1', b: '2')", // global variables
2034-
"do macro('test', b: '22', c: '33')", // "static" local variables
2034+
"do put_macro('test', b: '22', c: '33')", // "static" local variables
20352035
" add_field('test', '$[a]-$[b]-$[c]-$[d]')",
20362036
"end",
2037-
"macro('test', c: '333', d: '444')", // "dynamic" local variables
2038-
"macro('test', b: '555', d: '666')",
2037+
"call_macro('test', c: '333', d: '444')", // "dynamic" local variables
2038+
"call_macro('test', b: '555', d: '666')",
20392039
"add_field('vars', '$[a]-$[b]')"
20402040
),
20412041
i -> {
@@ -2056,13 +2056,13 @@ public void shouldCallMacroWithVariables() {
20562056
public void shouldCallMacroWithVariablesPassedToNestedBinds() {
20572057
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
20582058
"put_vars(a: '1', b: '2')", // global variables
2059-
"do macro('test', b: '22', c: '33')", // "static" local variables
2059+
"do put_macro('test', b: '22', c: '33')", // "static" local variables
20602060
" do once()",
20612061
" add_field('test', '$[a]-$[b]-$[c]-$[d]')",
20622062
" end",
20632063
"end",
2064-
"macro('test', c: '333', d: '444')", // "dynamic" local variables
2065-
"macro('test', b: '555', d: '666')",
2064+
"call_macro('test', c: '333', d: '444')", // "dynamic" local variables
2065+
"call_macro('test', b: '555', d: '666')",
20662066
"add_field('vars', '$[a]-$[b]')"
20672067
),
20682068
i -> {
@@ -2082,13 +2082,13 @@ public void shouldCallMacroWithVariablesPassedToNestedBinds() {
20822082
public void shouldCallMacroWithVariablesPassedToNestedConditionals() {
20832083
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
20842084
"put_vars(a: '1', b: '2')", // global variables
2085-
"do macro('test', b: '22', c: '33')", // "static" local variables
2085+
"do put_macro('test', b: '22', c: '33')", // "static" local variables
20862086
" if any_equal('cond', '$[d]')",
20872087
" add_field('test', '$[a]-$[b]-$[c]-$[d]')",
20882088
" end",
20892089
"end",
2090-
"macro('test', c: '333', d: '444')", // "dynamic" local variables
2091-
"macro('test', b: '555', d: '666')",
2090+
"call_macro('test', c: '333', d: '444')", // "dynamic" local variables
2091+
"call_macro('test', b: '555', d: '666')",
20922092
"add_field('vars', '$[a]-$[b]')"
20932093
),
20942094
i -> {
@@ -2111,10 +2111,10 @@ public void shouldNotLeakVariablesFromMacro() {
21112111
MetafixTestHelpers.assertProcessException(IllegalArgumentException.class, "Variable 'c' was not assigned!\nAssigned variables:\n{a=1, b=2}", () ->
21122112
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
21132113
"put_vars(a: '1', b: '2')", // global variables
2114-
"do macro('test', b: '22', c: '33')", // "static" local variables
2114+
"do put_macro('test', b: '22', c: '33')", // "static" local variables
21152115
"end",
2116-
"macro('test', c: '333', d: '444')", // "dynamic" local variables
2117-
"macro('test', b: '555', d: '666')",
2116+
"call_macro('test', c: '333', d: '444')", // "dynamic" local variables
2117+
"call_macro('test', b: '555', d: '666')",
21182118
"add_field('test', '$[a]-$[b]-$[c]-$[d]')"
21192119
),
21202120
i -> {
@@ -2130,15 +2130,15 @@ public void shouldNotLeakVariablesFromMacro() {
21302130
@Test
21312131
public void shouldCallNestedMacro() {
21322132
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2133-
"do macro('test1', c: '23')",
2133+
"do put_macro('test1', c: '23')",
21342134
" add_field('test$[a]', '42')",
2135-
" macro('test2', b: '$[b]', c: '$[c]')",
2135+
" call_macro('test2', b: '$[b]', c: '$[c]')",
21362136
"end",
2137-
"do macro('test2')",
2137+
"do put_macro('test2')",
21382138
" add_field('test$[b]', '$[c]')",
21392139
"end",
2140-
"macro('test1', a: '1', b: '2')",
2141-
"macro('test1', a: '3', b: '4')"
2140+
"call_macro('test1', a: '1', b: '2')",
2141+
"call_macro('test1', a: '3', b: '4')"
21422142
),
21432143
i -> {
21442144
i.startRecord("1");

0 commit comments

Comments
 (0)