Skip to content

Commit fe6dc41

Browse files
authored
Merge pull request #245 from metafacture/passIncludeOptionsAsVariables
Pass `include()` options as variables.
2 parents 0e27728 + 12e954c commit fe6dc41

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,19 @@ You can use path wildcards to select fields matching a pattern. They only match
153153

154154
##### `include`
155155

156-
---- TODO: THIS NEEDS MORE CONTENT -----
156+
Includes a Fix file and executes it as if its statements were written in place of the function call.
157+
158+
Parameters:
159+
160+
- `path` (required): Path to Fix file (if the path starts with a `.`, it is resolved relative to the including file's directory; otherwise, it is resolved relative to the current working directory).
161+
162+
Options:
163+
164+
- All options are made available as "dynamic" local variables in the included Fix file.
165+
166+
```perl
167+
include("<path>"[, <dynamicLocalVariables>...])
168+
```
157169

158170
##### `nothing`
159171

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
5757
// TODO: Catmandu load path
5858
final String includePath = metafix.resolvePath(includeFile);
5959

60-
metafix.getRecordTransformer(includePath).transform(record);
60+
metafix.getRecordTransformer(includePath).transform(record, options);
6161
}
6262
},
6363
nothing {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,50 @@ public void shouldIncludeFixFile() {
233233
);
234234
}
235235

236+
@Test
237+
public void shouldIncludeFixFileWithVariables() {
238+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
239+
"include('src/test/resources/org/metafacture/metafix/fixes/vars.fix', a: '1', b: '23')",
240+
"include('src/test/resources/org/metafacture/metafix/fixes/vars.fix', a: '2', b: '42')"
241+
),
242+
i -> {
243+
i.startRecord("1");
244+
i.endRecord();
245+
246+
i.startRecord("2");
247+
i.endRecord();
248+
},
249+
o -> {
250+
o.get().startRecord("1");
251+
o.get().literal("test1", "1-23");
252+
o.get().literal("test2", "1-42");
253+
o.get().endRecord();
254+
255+
o.get().startRecord("2");
256+
o.get().literal("test1", "2-23");
257+
o.get().literal("test2", "2-42");
258+
o.get().endRecord();
259+
}
260+
);
261+
}
262+
263+
@Test
264+
public void shouldNotLeakVariablesFromIncludingFixFile() {
265+
MetafixTestHelpers.assertProcessException(IllegalArgumentException.class, "Variable 'a' was not assigned!\nAssigned variables:\n{}", () ->
266+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
267+
"include('src/test/resources/org/metafacture/metafix/fixes/vars.fix', a: '1', b: '23')",
268+
"add_field('vars', '$[a]-$[b]')"
269+
),
270+
i -> {
271+
i.startRecord("1");
272+
i.endRecord();
273+
},
274+
o -> {
275+
}
276+
)
277+
);
278+
}
279+
236280
@Test
237281
public void shouldIncludeFixFileInBind() {
238282
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
paste("test$[a]", "_id", "~$[b]", join_char: "-")

0 commit comments

Comments
 (0)