Skip to content

Commit 3a36855

Browse files
committed
add support for serializing Transformer lambdas that take other transformers as parameters
1 parent 29a945f commit 3a36855

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,19 @@ public Transformer deserialize(ByteBuffer bytes) {
143143
int paramSize = bytes.getInt();
144144
byte[] paramBytes = new byte[paramSize];
145145
bytes.get(paramBytes);
146-
Object param = getSerializedObject(ByteBuffer.wrap(paramBytes));
146+
Object param;
147+
try {
148+
param = getSerializedObject(ByteBuffer.wrap(paramBytes));
149+
}
150+
catch (Exception e) {
151+
// Assume that the param used a different serialization
152+
// technique...Right now, the only alternative that is
153+
// supported is using this factory to serialize nested
154+
// Transformer params. In the future, we may want to support
155+
// more, in which case it'd be necessary to add a prefix to
156+
// the byte stream indicating the path we took.
157+
param = deserialize(ByteBuffer.wrap(paramBytes));
158+
}
147159
params.add(param);
148160
}
149161
return Reflection.callStatic(Transformers.class, name,
@@ -225,7 +237,9 @@ public ByteBuffer serialize(Transformer transformer) {
225237
* - objectSize (4 bytes)
226238
* - object (objectSize byes)
227239
*/
228-
ByteBuffer buf = getSerializedBytes(param);
240+
ByteBuffer buf = param instanceof Transformer
241+
? serialize((Transformer) param)
242+
: getSerializedBytes(param);
229243
baos.write(Ints.toByteArray(buf.remaining()));
230244
baos.write(ByteBuffers.getByteArray(buf));
231245
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,14 @@ public void testSerializationRoundTrips() {
186186
Assert.assertEquals(ImmutableMap.of("a", ImmutableMap.of("b", 1)),
187187
t.transform("a.b", 1));
188188
}
189+
190+
@Test
191+
public void testSerializeNestedTransformer() {
192+
Transformer t = Transformers.nullSafe(Transformers.explode());
193+
ByteBuffer bytes = Transformer.serialize(t);
194+
t = Transformer.deserialize(bytes);
195+
Assert.assertEquals(ImmutableMap.of("a", ImmutableMap.of("b", 1)),
196+
t.transform("a.b", 1));
197+
}
189198

190199
}

0 commit comments

Comments
 (0)