Package com.bazaarvoice.jolt
Class Chainr
- java.lang.Object
-
- com.bazaarvoice.jolt.Chainr
-
- All Implemented Interfaces:
ContextualTransform
,JoltTransform
,Transform
public class Chainr extends java.lang.Object implements Transform, ContextualTransform
Chainr is the JOLT mechanism for chainingJoltTransform
s together. Any of the built-in JOLT transform types can be called directly from Chainr. Any custom-written Java transforms can be adapted in by implementing theTransform
orSpecDriven
interfaces. A Chainr spec should be an array of objects in order that look like this: [ { "operation": "[operation-name]", // stuff that the specific transform needs go here }, ... ] Each operation is called in the order that it is specified within the array. The original input to Chainr is passed into the first operation, with its output passed into the next, and so on. The output of the final operation is returned from Chainr. Currently, [operation-name] can be any of the following: - shift: (Shiftr
) a tool for moving parts of an input JSON document to a new output document - default: (Defaultr
) a tool for applying default values to the provided JSON document - remove: (Removr
) a tool for removing specific values from the provided JSON document - sort: (Sortr
) sort the JSON document - java: passes control to whatever Java class you specify as long as it implements theTransform
interface Shift, default, and remove operation all require a "spec", while sort does not. [ { "operation": "shift", "spec" : { // shiftr spec } }, { "operation": "sort" // sort does not need a spec }, ... ] Custom Java classes that implementTransform
and/orSpecDriven
can be loaded by specifying the full className to load. Additionally, if upon reflection of the class we see that it is an instance of aSpecDriven
, then we will construct it with a the supplied "spec" object. [ { "operation": "com.bazaarvoice.tuna.CustomTransform", "spec" : { ... } // optional spec to use to construct a customTransform
if it has theSpecDriven
marker interface. }, ... ]
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Chainr.ContextualTransformAdapter
Adapt "normal" Transforms to look like ContextualTransforms, so that Chainr can just maintain a single list of "JoltTransforms" to run.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<ContextualTransform>
actualContextualTransforms
private java.util.List<ContextualTransform>
transformsList
-
Constructor Summary
Constructors Constructor Description Chainr(java.util.List<JoltTransform> joltTransforms)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.Object
doTransform(java.util.List<ContextualTransform> transforms, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
static Chainr
fromSpec(java.lang.Object input)
static Chainr
fromSpec(java.lang.Object input, ChainrInstantiator instantiator)
java.util.List<ContextualTransform>
getContextualTransforms()
This method allows Chainr clients to examine the ContextualTransforms in this Chainr instance.boolean
hasContextualTransforms()
java.lang.Object
transform(int from, int to, java.lang.Object input)
Useful for testing and debugging.java.lang.Object
transform(int from, int to, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
Have Chainr run a subset of the transforms in it's spec.java.lang.Object
transform(int to, java.lang.Object input)
Have Chainr run a subset of the transforms in it's spec.java.lang.Object
transform(int to, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
Useful for testing and debugging.java.lang.Object
transform(java.lang.Object input)
Execute a transform on some input JSON with optionally provided "context" and return the result.java.lang.Object
transform(java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
Runs a series of Transforms on the input, piping the inputs and outputs of the Transforms together.
-
-
-
Field Detail
-
transformsList
private final java.util.List<ContextualTransform> transformsList
-
actualContextualTransforms
private final java.util.List<ContextualTransform> actualContextualTransforms
-
-
Constructor Detail
-
Chainr
public Chainr(java.util.List<JoltTransform> joltTransforms)
-
-
Method Detail
-
fromSpec
public static Chainr fromSpec(java.lang.Object input)
-
fromSpec
public static Chainr fromSpec(java.lang.Object input, ChainrInstantiator instantiator)
-
transform
public java.lang.Object transform(java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
Runs a series of Transforms on the input, piping the inputs and outputs of the Transforms together. Chainr instances are meant to be immutable once they are created so that they can be used many times. The notion of passing "context" to the transforms allows chainr instances to be reused, even in situations were you need to slightly vary.- Specified by:
transform
in interfaceContextualTransform
- Parameters:
input
- a JSON (Jackson-parsed) maps-of-maps object to transformcontext
- optional tweaks that the consumer of the transform would like- Returns:
- an object representing the JSON resulting from the transform
- Throws:
TransformException
- if the specification is malformed, an operation is not found, or if one of the specified transforms throws an exception.
-
transform
public java.lang.Object transform(java.lang.Object input)
Description copied from interface:Transform
Execute a transform on some input JSON with optionally provided "context" and return the result.
-
transform
public java.lang.Object transform(int to, java.lang.Object input)
Have Chainr run a subset of the transforms in it's spec. Useful for testing and debugging.- Parameters:
input
- the input data to transformto
- transform from the chainrSpec to end with: 0 based index exclusive
-
transform
public java.lang.Object transform(int to, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
Useful for testing and debugging.- Parameters:
input
- the input data to transformto
- transform from the chainrSpec to end with: 0 based index exclusivecontext
- optional tweaks that the consumer of the transform would like
-
transform
public java.lang.Object transform(int from, int to, java.lang.Object input)
Useful for testing and debugging.- Parameters:
input
- the input data to transformfrom
- transform from the chainrSpec to start with: 0 based indexto
- transform from the chainrSpec to end with: 0 based index exclusive
-
transform
public java.lang.Object transform(int from, int to, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
Have Chainr run a subset of the transforms in it's spec. Useful for testing and debugging.- Parameters:
input
- the input data to transformfrom
- transform from the chainrSpec to start with: 0 based indexto
- transform from the chainrSpec to end with: 0 based index exclusivecontext
- optional tweaks that the consumer of the transform would like
-
doTransform
private static java.lang.Object doTransform(java.util.List<ContextualTransform> transforms, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
-
hasContextualTransforms
public boolean hasContextualTransforms()
- Returns:
- true if this Chainr instance has any ContextualTransforms
-
getContextualTransforms
public java.util.List<ContextualTransform> getContextualTransforms()
This method allows Chainr clients to examine the ContextualTransforms in this Chainr instance. This may be helpful when building the "context".- Returns:
- List of ContextualTransforms used by this Chainr instance
-
-