Skip to content

Commit

Permalink
Only accept string values in to_var() Fix function. (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Oct 11, 2024
1 parent 46d1ef5 commit cee638a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void apply(final Metafix metafix, final Record record, final List<String>
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final Value value = record.get(params.get(0));
metafix.getVars().put(params.get(1), Value.isNull(value) ? options.getOrDefault(DEFAULT_OPTION, "") : value.toString());
metafix.getVars().put(params.get(1), Value.isNull(value) ? options.getOrDefault(DEFAULT_OPTION, "") : value.asString());
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4097,51 +4097,24 @@ public void shouldCreateVariableFromLiteralValue() {
i.endEntity();
i.endRecord();
i.startRecord("2");
i.startEntity("data");
i.literal("title", "test1");
i.literal("title", "test2");
i.endEntity();
i.endRecord();
i.startRecord("3");
i.startEntity("data");
i.startEntity("title");
i.literal("key", "value");
i.endEntity();
i.endEntity();
i.endRecord();
i.startRecord("4");
i.endRecord();
i.startRecord("5");
i.startEntity("data");
i.literal("title", "final-test");
i.endEntity();
i.endRecord();
},
(o, f) -> {
o -> {
o.get().startRecord("1");
o.get().startEntity("data");
o.get().literal("title", "test");
o.get().endEntity();
o.get().literal("testResult", "This is a test");
o.get().endRecord();
o.get().startRecord("2");
o.get().startEntity("data");
o.get().literal("title", "test1");
o.get().literal("title", "test2");
o.get().endEntity();
o.get().literal("testResult", "This is a [test1, test2]");
o.get().endRecord();
o.get().startRecord("3");
o.get().startEntity("data");
o.get().startEntity("title");
o.get().literal("key", "value");
f.apply(2).endEntity();
o.get().literal("testResult", "This is a {key=value}");
o.get().endRecord();
o.get().startRecord("4");
o.get().literal("testResult", "This is a ");
o.get().endRecord();
o.get().startRecord("5");
o.get().startRecord("3");
o.get().startEntity("data");
o.get().literal("title", "final-test");
o.get().endEntity();
Expand Down Expand Up @@ -4180,4 +4153,45 @@ public void shouldCreateVariableFromLiteralValueWithDefault() {
);
}

@Test
public void shouldNotCreateVariableFromArrayValue() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Array", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"to_var('data.title', 'testVar')"
),
i -> {
i.startRecord("1");
i.startEntity("data");
i.literal("title", "test1");
i.literal("title", "test2");
i.endEntity();
i.endRecord();
},
o -> {
}
)
);
}

@Test
public void shouldNotCreateVariableFromHashValue() {
MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Hash", () ->
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"to_var('data.title', 'testVar')"
),
i -> {
i.startRecord("1");
i.startEntity("data");
i.startEntity("title");
i.literal("key", "value");
i.endEntity();
i.endEntity();
i.endRecord();
},
o -> {
}
)
);
}

}

0 comments on commit cee638a

Please sign in to comment.