Skip to content

Commit 55e5989

Browse files
authored
Merge pull request #154 from metafacture/153-makeSubstringCompatible
Change `substring()` Fix function to be compatible with Catmandu.
2 parents 750f7a0 + d3d0840 commit 55e5989

File tree

23 files changed

+93
-10
lines changed

23 files changed

+93
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,10 @@ split_field("<sourceField>", "<separator>")
485485

486486
#### `substring`
487487

488-
Replaces a string with its substring as defined by the start and end positions.
488+
Replaces a string with its substring as defined by the start position (offset) and length.
489489

490490
```perl
491-
substring("<sourceField>", "<startPosition>", "<endPosition>")
491+
substring("<sourceField>", "<startPosition>", "<length>")
492492
```
493493

494494
#### `sum`

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,14 @@ public void apply(final Metafix metafix, final Record record, final List<String>
458458
substring {
459459
@Override
460460
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
461-
new FixPath(params.get(0)).transformIn(record, s -> s.substring(getInteger(params, 1), getInteger(params, 2) - 1));
461+
final int offset = getInteger(params, 1);
462+
final Integer end = params.size() > 2 ? offset + getInteger(params, 2) : null;
463+
// TODO: final String replacement = params.size() > 3 ? params.get(3) : null;
464+
465+
new FixPath(params.get(0)).transformIn(record, s -> {
466+
final int length = s.length();
467+
return offset > length ? s : end == null || end > length ? s.substring(offset) : s.substring(offset, end);
468+
});
462469
}
463470
},
464471
sum {

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

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,70 @@ public void shouldNotCapitalizeArray() {
179179
@Test
180180
public void shouldGetSubstringOfString() {
181181
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
182-
"substring('title', '0', '2')"
182+
"substring('rel', '5', '3')"
183183
),
184184
i -> {
185185
i.startRecord("1");
186-
i.literal("title", "marc");
186+
i.literal("rel", "grandson");
187+
i.endRecord();
188+
},
189+
o -> {
190+
o.get().startRecord("1");
191+
o.get().literal("rel", "son");
192+
o.get().endRecord();
193+
}
194+
);
195+
}
196+
197+
@Test
198+
public void shouldGetSubstringOfStringWithoutLength() {
199+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
200+
"substring('rel', '5')"
201+
),
202+
i -> {
203+
i.startRecord("1");
204+
i.literal("rel", "grandson");
205+
i.endRecord();
206+
},
207+
o -> {
208+
o.get().startRecord("1");
209+
o.get().literal("rel", "son");
210+
o.get().endRecord();
211+
}
212+
);
213+
}
214+
215+
@Test
216+
public void shouldGetSubstringOfTruncatedString() {
217+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
218+
"substring('rel', '5', '6')"
219+
),
220+
i -> {
221+
i.startRecord("1");
222+
i.literal("rel", "grandson");
223+
i.endRecord();
224+
},
225+
o -> {
226+
o.get().startRecord("1");
227+
o.get().literal("rel", "son");
228+
o.get().endRecord();
229+
}
230+
);
231+
}
232+
233+
@Test
234+
public void shouldNotGetSubstringOutsideOfString() {
235+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
236+
"substring('rel', '9', '3')"
237+
),
238+
i -> {
239+
i.startRecord("1");
240+
i.literal("rel", "grandson");
187241
i.endRecord();
188242
},
189243
o -> {
190244
o.get().startRecord("1");
191-
o.get().literal("title", "m");
245+
o.get().literal("rel", "grandson");
192246
o.get().endRecord();
193247
}
194248
);
@@ -197,17 +251,17 @@ public void shouldGetSubstringOfString() {
197251
@Test
198252
public void shouldGetSubstringWithVar() {
199253
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
200-
"substring('title', '0', '$[end]')"
254+
"substring('rel', '0', '$[length]')"
201255
),
202-
ImmutableMap.of("end", "3"),
256+
ImmutableMap.of("length", "5"),
203257
i -> {
204258
i.startRecord("1");
205-
i.literal("title", "marc");
259+
i.literal("rel", "grandson");
206260
i.endRecord();
207261
},
208262
o -> {
209263
o.get().startRecord("1");
210-
o.get().literal("title", "ma");
264+
o.get().literal("rel", "grand");
211265
o.get().endRecord();
212266
}
213267
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"key":"cat"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"key":"tacocat"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
substring("key", "4")
Lines changed: 8 additions & 0 deletions
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
7+
|write(FLUX_DIR + "output-metafix.json")
8+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"key":"taco"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"key":"tacocat"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
substring("key", "0", "4")

0 commit comments

Comments
 (0)