Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit cee638a

Browse files
committed
Only accept string values in to_var() Fix function. (#365)
1 parent 46d1ef5 commit cee638a

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
127127
@Override
128128
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
129129
final Value value = record.get(params.get(0));
130-
metafix.getVars().put(params.get(1), Value.isNull(value) ? options.getOrDefault(DEFAULT_OPTION, "") : value.toString());
130+
metafix.getVars().put(params.get(1), Value.isNull(value) ? options.getOrDefault(DEFAULT_OPTION, "") : value.asString());
131131
}
132132
},
133133

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

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,51 +4097,24 @@ public void shouldCreateVariableFromLiteralValue() {
40974097
i.endEntity();
40984098
i.endRecord();
40994099
i.startRecord("2");
4100-
i.startEntity("data");
4101-
i.literal("title", "test1");
4102-
i.literal("title", "test2");
4103-
i.endEntity();
41044100
i.endRecord();
41054101
i.startRecord("3");
41064102
i.startEntity("data");
4107-
i.startEntity("title");
4108-
i.literal("key", "value");
4109-
i.endEntity();
4110-
i.endEntity();
4111-
i.endRecord();
4112-
i.startRecord("4");
4113-
i.endRecord();
4114-
i.startRecord("5");
4115-
i.startEntity("data");
41164103
i.literal("title", "final-test");
41174104
i.endEntity();
41184105
i.endRecord();
41194106
},
4120-
(o, f) -> {
4107+
o -> {
41214108
o.get().startRecord("1");
41224109
o.get().startEntity("data");
41234110
o.get().literal("title", "test");
41244111
o.get().endEntity();
41254112
o.get().literal("testResult", "This is a test");
41264113
o.get().endRecord();
41274114
o.get().startRecord("2");
4128-
o.get().startEntity("data");
4129-
o.get().literal("title", "test1");
4130-
o.get().literal("title", "test2");
4131-
o.get().endEntity();
4132-
o.get().literal("testResult", "This is a [test1, test2]");
4133-
o.get().endRecord();
4134-
o.get().startRecord("3");
4135-
o.get().startEntity("data");
4136-
o.get().startEntity("title");
4137-
o.get().literal("key", "value");
4138-
f.apply(2).endEntity();
4139-
o.get().literal("testResult", "This is a {key=value}");
4140-
o.get().endRecord();
4141-
o.get().startRecord("4");
41424115
o.get().literal("testResult", "This is a ");
41434116
o.get().endRecord();
4144-
o.get().startRecord("5");
4117+
o.get().startRecord("3");
41454118
o.get().startEntity("data");
41464119
o.get().literal("title", "final-test");
41474120
o.get().endEntity();
@@ -4180,4 +4153,45 @@ public void shouldCreateVariableFromLiteralValueWithDefault() {
41804153
);
41814154
}
41824155

4156+
@Test
4157+
public void shouldNotCreateVariableFromArrayValue() {
4158+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Array", () ->
4159+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4160+
"to_var('data.title', 'testVar')"
4161+
),
4162+
i -> {
4163+
i.startRecord("1");
4164+
i.startEntity("data");
4165+
i.literal("title", "test1");
4166+
i.literal("title", "test2");
4167+
i.endEntity();
4168+
i.endRecord();
4169+
},
4170+
o -> {
4171+
}
4172+
)
4173+
);
4174+
}
4175+
4176+
@Test
4177+
public void shouldNotCreateVariableFromHashValue() {
4178+
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Hash", () ->
4179+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
4180+
"to_var('data.title', 'testVar')"
4181+
),
4182+
i -> {
4183+
i.startRecord("1");
4184+
i.startEntity("data");
4185+
i.startEntity("title");
4186+
i.literal("key", "value");
4187+
i.endEntity();
4188+
i.endEntity();
4189+
i.endRecord();
4190+
},
4191+
o -> {
4192+
}
4193+
)
4194+
);
4195+
}
4196+
41834197
}

0 commit comments

Comments
 (0)