Skip to content

Commit 64b0ef2

Browse files
committed
Add missing transformation to ensure String
1 parent 90322f1 commit 64b0ef2

File tree

3 files changed

+83
-39
lines changed

3 files changed

+83
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Version 1.3.1 (TBD)
44
* Fixed some bugs related to serializing and deserializing some built-in `Transformers`.
5+
* Added missing transformers to ensure values are handled as Strings.
56

67
#### Version 1.3.0 (November 23, 2018)
78
* Added the `ScriptedTransformer` framework. `ScriptedTransformer` is abstract class that can be extended to provide transformation logic via a script that is compatible with the Java script engine platform. Right now, javascript is the supported language for writing transformer scripts.

src/main/java/com/cinchapi/etl/Transformers.java

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -202,26 +202,6 @@ public static Transformer keyMap(Map<String, String> map) {
202202
};
203203
}
204204

205-
/**
206-
* Return a {@link Transformer} that replaces whitespace characters with an
207-
* underscore in keys.
208-
*
209-
* @return the {@link Transformer}
210-
*/
211-
public static Transformer keyWhitespaceToUnderscore() {
212-
return keyReplaceChars(ImmutableMap.of(' ', '_'));
213-
}
214-
215-
/**
216-
* Return a {@link Transformer} that removes all whitespace characters from
217-
* a key.
218-
*
219-
* @return the {@link Transformer}
220-
*/
221-
public static Transformer keyRemoveWhitespace() {
222-
return keyRemoveInvalidChars(Character::isWhitespace);
223-
}
224-
225205
/**
226206
* Return a {@link Transformer} that removes all the character in the
227207
* {@code invalid} collection.
@@ -269,6 +249,16 @@ public static Transformer keyRemoveInvalidChars(
269249
};
270250
}
271251

252+
/**
253+
* Return a {@link Transformer} that removes all whitespace characters from
254+
* a key.
255+
*
256+
* @return the {@link Transformer}
257+
*/
258+
public static Transformer keyRemoveWhitespace() {
259+
return keyRemoveInvalidChars(Character::isWhitespace);
260+
}
261+
272262
/**
273263
* Return a {@link Transformer} that replaces all the character keys in the
274264
* {@code replacements} mapping with their associated character values in
@@ -366,6 +356,16 @@ public static Transformer keyValueStripQuotes() {
366356
return keyValueRemoveQuotes();
367357
}
368358

359+
/**
360+
* Return a {@link Transformer} that replaces whitespace characters with an
361+
* underscore in keys.
362+
*
363+
* @return the {@link Transformer}
364+
*/
365+
public static Transformer keyWhitespaceToUnderscore() {
366+
return keyReplaceChars(ImmutableMap.of(' ', '_'));
367+
}
368+
369369
/**
370370
* Return a {@link Transformer} that does not perform any key or value
371371
* transformations.
@@ -388,10 +388,6 @@ public static Transformer nullSafe(Transformer transformer) {
388388
: null;
389389
}
390390

391-
public static Transformer valueRemoveIfEmpty() {
392-
return valueRemoveIf(Empty.ness());
393-
}
394-
395391
/**
396392
* Return a {@link Transformer} that will cause a key/value pair to be
397393
* "removed" if the value is described by the provided {@code adjective}.
@@ -408,21 +404,6 @@ public static Transformer removeValuesThatAre(Adjective adjective) {
408404
return valueRemoveIf(adjective);
409405
}
410406

411-
/**
412-
* Return a {@link Transformer} that will cause a key/value pair to be
413-
* "removed" if the value is described by the provided {@code adjective}.
414-
* <p>
415-
* Removal is accomplished by returning an empty map for the transformation.
416-
* </p>
417-
*
418-
* @param adjective
419-
* @return the {@link Transformer}
420-
*/
421-
public static Transformer valueRemoveIf(Adjective adjective) {
422-
return (key, value) -> adjective.describes(value) ? ImmutableMap.of()
423-
: null;
424-
}
425-
426407
/**
427408
* Return a {@link Transformer} that, For EVERY key, transform values to a
428409
* {@link Boolean} if possible. If the value cannot be transformed, an
@@ -564,6 +545,41 @@ public static Transformer valueAsResolvableLinkInstruction(String... keys) {
564545
}
565546
}
566547

548+
/**
549+
* Return a {@link Transformer} that, for EVERY key, transforms values to a
550+
* {@link String}.
551+
*
552+
* @return the {@link Transformer}
553+
*/
554+
public static Transformer valueAsString() {
555+
return (key, value) -> value instanceof String ? null
556+
: Transformation.to(key, value.toString());
557+
}
558+
559+
/**
560+
* Return a {@link Transformer} that, for the specified {@code key}as,
561+
* transforms values to a {@link String}.
562+
*
563+
* @param keys
564+
* @return the {@link Transformer}
565+
*/
566+
public static Transformer valueAsString(String... keys) {
567+
if(keys.length == 0) {
568+
return valueAsString();
569+
}
570+
else {
571+
Set<String> _keys = Arrays.stream(keys).collect(Collectors.toSet());
572+
return (key, value) -> {
573+
if(_keys.contains(key)) {
574+
return valueAsString().transform(key, value);
575+
}
576+
else {
577+
return null;
578+
}
579+
};
580+
}
581+
}
582+
567583
/**
568584
* Return a {@link Transformer} that, For EVERY key, transform values to a
569585
* {@link Number} if possible. If the value cannot be transformed, an
@@ -687,6 +703,25 @@ public static Transformer valueNullifyIfEmpty(Empty empty) {
687703
};
688704
}
689705

706+
/**
707+
* Return a {@link Transformer} that will cause a key/value pair to be
708+
* "removed" if the value is described by the provided {@code adjective}.
709+
* <p>
710+
* Removal is accomplished by returning an empty map for the transformation.
711+
* </p>
712+
*
713+
* @param adjective
714+
* @return the {@link Transformer}
715+
*/
716+
public static Transformer valueRemoveIf(Adjective adjective) {
717+
return (key, value) -> adjective.describes(value) ? ImmutableMap.of()
718+
: null;
719+
}
720+
721+
public static Transformer valueRemoveIfEmpty() {
722+
return valueRemoveIf(Empty.ness());
723+
}
724+
690725
/**
691726
* Return a {@link Transformer} that splits a String value into multiple
692727
* strings that are all mapped from the original key.

src/test/java/com/cinchapi/etl/TransformersTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ public void testValueAsTag() {
127127
Object value = Iterables.getOnlyElement(transformed.values());
128128
Assert.assertTrue(value instanceof Tag);
129129
}
130+
131+
@Test
132+
public void testValueAsString() {
133+
Map<String, Object> transformed = Transformers.valueAsString()
134+
.transform("foo", (Object) 1);
135+
Object value = Iterables.getOnlyElement(transformed.values());
136+
Assert.assertTrue(value instanceof String);
137+
}
130138

131139
@Test
132140
public void testValueNullifyIfEmpty() {

0 commit comments

Comments
 (0)