Skip to content

Commit 56f61b9

Browse files
authored
Merge pull request #235 from metafacture/234-addInConditional
Add `in()` Fix conditional.
2 parents 6a5155f + 97dd618 commit 56f61b9

File tree

19 files changed

+351
-0
lines changed

19 files changed

+351
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,16 @@ Executes the functions if/unless the field value matches the regular expression
657657

658658
Executes the functions if/unless the field value does not match the regular expression pattern. If it is an array or a hash none of the field values may match the regular expression pattern.
659659

660+
#### `in`
661+
662+
Executes the functions if/unless the field value [is contained in](https://perldoc.perl.org/perlop#Smartmatch-Operator) the value of the other field.
663+
664+
_Also aliased as [`is_contained_in`](#is_contained_in)._
665+
666+
#### `is_contained_in`
667+
668+
_Alias for [`in`](#in)._
669+
660670
## Xtext
661671

662672
This repo has been originally set up with [Xtext](https://www.eclipse.org/Xtext/) 2.17.0 and Eclipse for Java 2019-03, following [https://www.eclipse.org/Xtext/documentation/104_jvmdomainmodel.html](https://www.eclipse.org/Xtext/documentation/104_jvmdomainmodel.html).

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ public boolean test(final Metafix metafix, final Record record, final List<Strin
6868
}
6969
},
7070

71+
in {
72+
@Override
73+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
74+
final Value value1 = record.get(params.get(0));
75+
final Value value2 = record.get(params.get(1));
76+
77+
return value1 != null && value2 != null && value1.<Boolean>extractType((m, c) -> m
78+
.ifArray(a1 -> value2.matchType()
79+
.ifArray(a2 -> c.accept(a1.equals(a2)))
80+
)
81+
.ifHash(h1 -> value2.matchType()
82+
.ifHash(h2 -> c.accept(h1.equals(h2)))
83+
)
84+
.ifString(s1 -> value2.matchType()
85+
.ifArray(a2 -> c.accept(a2.stream().anyMatch(value1::equals)))
86+
.ifHash(h2 -> c.accept(h2.containsField(s1)))
87+
.ifString(s2 -> c.accept(s1.equals(s2)))
88+
)
89+
);
90+
}
91+
},
92+
is_contained_in {
93+
@Override
94+
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
95+
return in.test(metafix, record, params, options);
96+
}
97+
},
98+
7199
all_match {
72100
@Override
73101
public boolean test(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {

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

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,4 +1134,205 @@ public void ifOnNonExistingIndexInRepeatedField() {
11341134
}
11351135
);
11361136
}
1137+
1138+
@Test
1139+
public void shouldContainStringInString() {
1140+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1141+
"if in(foo,bar)",
1142+
" add_field(forty_two,ok)",
1143+
"end"
1144+
),
1145+
i -> {
1146+
i.startRecord("1");
1147+
i.literal("foo", "42");
1148+
i.literal("bar", "42");
1149+
i.endRecord();
1150+
i.startRecord("2");
1151+
i.literal("foo", "42");
1152+
i.endRecord();
1153+
},
1154+
o -> {
1155+
o.get().startRecord("1");
1156+
o.get().literal("foo", "42");
1157+
o.get().literal("bar", "42");
1158+
o.get().literal("forty_two", "ok");
1159+
o.get().endRecord();
1160+
o.get().startRecord("2");
1161+
o.get().literal("foo", "42");
1162+
o.get().endRecord();
1163+
}
1164+
);
1165+
}
1166+
1167+
@Test
1168+
public void shouldContainStringInArray() {
1169+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1170+
"if in(foo,bar)",
1171+
" add_field(test,ok)",
1172+
"end"
1173+
),
1174+
i -> {
1175+
i.startRecord("1");
1176+
i.literal("foo", "1");
1177+
i.literal("bar", "1");
1178+
i.literal("bar", "2");
1179+
i.literal("bar", "3");
1180+
i.endRecord();
1181+
i.startRecord("2");
1182+
i.literal("foo", "42");
1183+
i.literal("bar", "1");
1184+
i.literal("bar", "2");
1185+
i.literal("bar", "3");
1186+
i.endRecord();
1187+
},
1188+
o -> {
1189+
o.get().startRecord("1");
1190+
o.get().literal("foo", "1");
1191+
o.get().literal("bar", "1");
1192+
o.get().literal("bar", "2");
1193+
o.get().literal("bar", "3");
1194+
o.get().literal("test", "ok");
1195+
o.get().endRecord();
1196+
o.get().startRecord("2");
1197+
o.get().literal("foo", "42");
1198+
o.get().literal("bar", "1");
1199+
o.get().literal("bar", "2");
1200+
o.get().literal("bar", "3");
1201+
o.get().endRecord();
1202+
}
1203+
);
1204+
}
1205+
1206+
@Test
1207+
public void shouldContainStringInHash() {
1208+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1209+
"if in(foo,bar)",
1210+
" add_field(test,ok)",
1211+
"end"
1212+
),
1213+
i -> {
1214+
i.startRecord("1");
1215+
i.literal("foo", "name");
1216+
i.startEntity("bar");
1217+
i.literal("name", "Patrick");
1218+
i.endEntity();
1219+
i.endRecord();
1220+
i.startRecord("2");
1221+
i.literal("foo", "name");
1222+
i.startEntity("bar");
1223+
i.startEntity("deep");
1224+
i.literal("name", "Nicolas");
1225+
i.endEntity();
1226+
i.endEntity();
1227+
i.endRecord();
1228+
},
1229+
(o, f) -> {
1230+
o.get().startRecord("1");
1231+
o.get().literal("foo", "name");
1232+
o.get().startEntity("bar");
1233+
o.get().literal("name", "Patrick");
1234+
o.get().endEntity();
1235+
o.get().literal("test", "ok");
1236+
o.get().endRecord();
1237+
o.get().startRecord("2");
1238+
o.get().literal("foo", "name");
1239+
o.get().startEntity("bar");
1240+
o.get().startEntity("deep");
1241+
o.get().literal("name", "Nicolas");
1242+
f.apply(2).endEntity();
1243+
o.get().endRecord();
1244+
}
1245+
);
1246+
}
1247+
1248+
@Test
1249+
public void shouldContainArrayInArray() {
1250+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1251+
"if in(foo,bar)",
1252+
" add_field(test,ok)",
1253+
"end"
1254+
),
1255+
i -> {
1256+
i.startRecord("1");
1257+
i.literal("foo", "1");
1258+
i.literal("foo", "2");
1259+
i.literal("bar", "1");
1260+
i.literal("bar", "2");
1261+
i.endRecord();
1262+
i.startRecord("2");
1263+
i.literal("foo", "1");
1264+
i.literal("foo", "2");
1265+
i.literal("bar", "1");
1266+
i.literal("bar", "2");
1267+
i.literal("bar", "3");
1268+
i.endRecord();
1269+
},
1270+
o -> {
1271+
o.get().startRecord("1");
1272+
o.get().literal("foo", "1");
1273+
o.get().literal("foo", "2");
1274+
o.get().literal("bar", "1");
1275+
o.get().literal("bar", "2");
1276+
o.get().literal("test", "ok");
1277+
o.get().endRecord();
1278+
o.get().startRecord("2");
1279+
o.get().literal("foo", "1");
1280+
o.get().literal("foo", "2");
1281+
o.get().literal("bar", "1");
1282+
o.get().literal("bar", "2");
1283+
o.get().literal("bar", "3");
1284+
o.get().endRecord();
1285+
}
1286+
);
1287+
}
1288+
1289+
@Test
1290+
public void shouldContainHashInHash() {
1291+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
1292+
"if in(foo,bar)",
1293+
" add_field(test,ok)",
1294+
"end"
1295+
),
1296+
i -> {
1297+
i.startRecord("1");
1298+
i.startEntity("foo");
1299+
i.literal("a", "b");
1300+
i.endEntity();
1301+
i.startEntity("bar");
1302+
i.literal("a", "b");
1303+
i.endEntity();
1304+
i.endRecord();
1305+
i.startRecord("2");
1306+
i.startEntity("foo");
1307+
i.literal("a", "b");
1308+
i.endEntity();
1309+
i.startEntity("bar");
1310+
i.literal("a", "b");
1311+
i.literal("c", "d");
1312+
i.endEntity();
1313+
i.endRecord();
1314+
},
1315+
o -> {
1316+
o.get().startRecord("1");
1317+
o.get().startEntity("foo");
1318+
o.get().literal("a", "b");
1319+
o.get().endEntity();
1320+
o.get().startEntity("bar");
1321+
o.get().literal("a", "b");
1322+
o.get().endEntity();
1323+
o.get().literal("test", "ok");
1324+
o.get().endRecord();
1325+
o.get().startRecord("2");
1326+
o.get().startEntity("foo");
1327+
o.get().literal("a", "b");
1328+
o.get().endEntity();
1329+
o.get().startEntity("bar");
1330+
o.get().literal("a", "b");
1331+
o.get().literal("c", "d");
1332+
o.get().endEntity();
1333+
o.get().endRecord();
1334+
}
1335+
);
1336+
}
1337+
11371338
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test" : "cat",
3+
"animals" : [ "cat", "dog", "zebra" ],
4+
"this" : "works"
5+
}
6+
{
7+
"test" : "cat",
8+
"animal" : [ "parrot", "dog", "zebra" ]
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"test" : "cat",
3+
"animals" : [ "cat", "dog", "zebra" ]
4+
}
5+
{
6+
"test" : "cat",
7+
"animal" : [ "parrot", "dog", "zebra" ]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if in("test","animals[]")
2+
add_field("this","works")
3+
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+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test" : "cat",
3+
"animal" : "cat",
4+
"this" : "works"
5+
}
6+
{
7+
"test" : "cat",
8+
"animal" : "dog"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"test" : "cat",
3+
"animal" : "cat"
4+
}
5+
{
6+
"test" : "cat",
7+
"animal" : "dog"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if in("animal","test")
2+
add_field("this","works")
3+
end

0 commit comments

Comments
 (0)