Skip to content

Commit dad2308

Browse files
authored
Merge pull request #201 from metafacture/200-resolveVariablesInOptions
Resolve variables in options.
2 parents 185c235 + b85d835 commit dad2308

File tree

7 files changed

+92
-8
lines changed

7 files changed

+92
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps { // checkstyle
7171
private final List<Closeable> resources = new ArrayList<>();
7272
private final List<Expression> expressions = new ArrayList<>();
7373
private final Map<String, Map<String, String>> maps = new HashMap<>();
74+
private final Map<String, String> vars = new HashMap<>();
7475
private final RecordTransformer recordTransformer = new RecordTransformer(this);
7576
private final StreamFlattener flattener = new StreamFlattener();
7677

7778
private Fix fix;
7879
private List<Value> entities = new ArrayList<>();
79-
private Map<String, String> vars = new HashMap<>();
8080
private Record currentRecord = new Record();
8181
private StreamReceiver outputStreamReceiver;
8282
private String fixFile;

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ public class RecordTransformer { // checkstyle-disable-line ClassFanOutComplexit
5757

5858
private static final Logger LOG = LoggerFactory.getLogger(RecordTransformer.class);
5959

60+
private final Map<String, String> vars;
6061
private final Metafix metafix;
6162

6263
private Record record;
6364

6465
/*package-private*/ RecordTransformer(final Metafix metafix) {
6566
this.metafix = metafix;
67+
vars = metafix.getVars();
6668
}
6769

6870
/*package-private*/ Record transform(final Fix fix) {
@@ -194,10 +196,11 @@ private String executionExceptionMessage(final EObject object, final Resource re
194196
}
195197

196198
private List<String> resolveParams(final List<String> params) {
197-
final Map<String, String> vars = metafix.getVars();
199+
return params.stream().map(this::resolveVars).collect(Collectors.toList());
200+
}
198201

199-
return params.stream().map(s -> s == null ? null :
200-
StringUtil.format(s, Metafix.VAR_START, Metafix.VAR_END, false, vars)).collect(Collectors.toList());
202+
private String resolveVars(final String value) {
203+
return value == null ? null : StringUtil.format(value, Metafix.VAR_START, Metafix.VAR_END, false, vars);
201204
}
202205

203206
private Map<String, String> options(final Options options) {
@@ -208,7 +211,7 @@ private Map<String, String> options(final Options options) {
208211
final List<String> values = options.getValues();
209212

210213
for (int i = 0; i < keys.size(); i += 1) {
211-
map.put(keys.get(i), values.get(i));
214+
map.put(resolveVars(keys.get(i)), resolveVars(values.get(i)));
212215
}
213216
}
214217

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,41 @@ public void shouldResolveVariablesInMultipleVariables() {
8585
}
8686

8787
@Test
88-
public void shouldNotResolveVariablesInMultipleVariablesFromMap() {
89-
assertVar("put_vars(varName: 'value$[var]', '$[varName]Var': 'value2')",
88+
public void shouldResolveVariablesInOptionsKeys() {
89+
assertVar("put_vars('varName$[var]': 'value')",
9090
ImmutableMap.of("var", "1"),
91-
ImmutableMap.of("varName", "value$[var]", "$[varName]Var", "value2"));
91+
ImmutableMap.of("varName1", "value"));
92+
}
93+
94+
@Test
95+
public void shouldResolveVariablesInOptionsValues() {
96+
assertVar("put_vars('varName': 'value$[var]')",
97+
ImmutableMap.of("var", "1"),
98+
ImmutableMap.of("varName", "value1"));
99+
}
100+
101+
@Test
102+
public void shouldResolveVariablesInOptionsFromPreviousMap() {
103+
assertVar("put_vars('varName': 'value$[var]')\nput_vars('$[varName]Var': 'value2')",
104+
ImmutableMap.of("var", "1"),
105+
ImmutableMap.of("varName", "value1", "value1Var", "value2"));
106+
}
107+
108+
@Test
109+
public void shouldNotResolveVariablesInOptionsFromCurrentMap() {
110+
MetafixTestHelpers.assertThrowsCause(IllegalArgumentException.class, "Variable 'varName' was not assigned!\nAssigned variables:\n{var=1}", () ->
111+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
112+
"put_vars(varName: 'value$[var]', '$[varName]Var': 'value2')"
113+
),
114+
ImmutableMap.of("var", "1"),
115+
i -> {
116+
i.startRecord("");
117+
i.endRecord();
118+
},
119+
o -> {
120+
}
121+
)
122+
);
92123
}
93124

94125
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"arrayOfStrings" : [ "one", "two", "three" ],
3+
"object" : {
4+
"field" : "five",
5+
"field_2" : "six"
6+
},
7+
"arrayOfObjects" : [ {
8+
"field_3" : "SEVEN",
9+
"field_4" : "abcight"
10+
}, {
11+
"field_3" : "NINE",
12+
"field_4" : "tabcn"
13+
} ],
14+
"test" : [ "one", "two", "three" ]
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"arrayOfStrings" : [ "one", "two", "three" ],
3+
"object" : {
4+
"field" : "five",
5+
"field_2" : "six"
6+
},
7+
"arrayOfObjects" : [ {
8+
"field_3" : "seven",
9+
"field_4" : "eight"
10+
}, {
11+
"field_3" : "nine",
12+
"field_4" : "ten"
13+
} ]
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
put_vars(
2+
"test": "arrayOfStrings[]",
3+
"test_2": "arrayOfObjects[]"
4+
)
5+
set_array("test[]")
6+
do list (path: "$[test]", "var": "$i")
7+
copy_field("$i", "test[].$append")
8+
end
9+
10+
do list (path: "$[test_2]", "var": "$i")
11+
upcase("$i.field_3")
12+
replace_all("$i.field_4","e","abc")
13+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FLUX_DIR + "input.json"
2+
|open-file
3+
|as-records
4+
|decode-json
5+
|fix(FLUX_DIR + "test.fix")
6+
|encode-json(prettyPrinting="true")
7+
|write(FLUX_DIR + "output-metafix.json")
8+
;

0 commit comments

Comments
 (0)