Package com.bazaarvoice.jolt
Class Chainr
java.lang.Object
com.bazaarvoice.jolt.Chainr
- All Implemented Interfaces:
ContextualTransform
,JoltTransform
,Transform
Chainr is the JOLT mechanism for chaining
JoltTransform
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 the Transform
or SpecDriven
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 the Transform
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 implement Transform
and/or SpecDriven
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 a
SpecDriven
, then we will construct it with a the supplied "spec" object.
[
{
"operation": "com.bazaarvoice.tuna.CustomTransform",
"spec" : { ... } // optional spec to use to construct a custom Transform
if it has the SpecDriven
marker interface.
},
...
]-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Adapt "normal" Transforms to look like ContextualTransforms, so that Chainr can just maintain a single list of "JoltTransforms" to run. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List
<ContextualTransform> private final List
<ContextualTransform> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static Object
doTransform
(List<ContextualTransform> transforms, Object input, Map<String, Object> context) static Chainr
static Chainr
fromSpec
(Object input, ChainrInstantiator instantiator) This method allows Chainr clients to examine the ContextualTransforms in this Chainr instance.boolean
Useful for testing and debugging.Have Chainr run a subset of the transforms in it's spec.Have Chainr run a subset of the transforms in it's spec.Useful for testing and debugging.Execute a transform on some input JSON with optionally provided "context" and return the result.Runs a series of Transforms on the input, piping the inputs and outputs of the Transforms together.
-
Field Details
-
transformsList
-
actualContextualTransforms
-
-
Constructor Details
-
Chainr
-
-
Method Details
-
fromSpec
-
fromSpec
-
transform
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
Description copied from interface:Transform
Execute a transform on some input JSON with optionally provided "context" and return the result. -
transform
Have Chainr run a subset of the transforms in it's spec. Useful for testing and debugging.- Parameters:
to
- transform from the chainrSpec to end with: 0 based index exclusiveinput
- the input data to transform
-
transform
Useful for testing and debugging.- Parameters:
to
- transform from the chainrSpec to end with: 0 based index exclusiveinput
- the input data to transformcontext
- optional tweaks that the consumer of the transform would like
-
transform
Useful for testing and debugging.- Parameters:
from
- transform from the chainrSpec to start with: 0 based indexto
- transform from the chainrSpec to end with: 0 based index exclusiveinput
- the input data to transform
-
transform
Have Chainr run a subset of the transforms in it's spec. Useful for testing and debugging.- Parameters:
from
- transform from the chainrSpec to start with: 0 based indexto
- transform from the chainrSpec to end with: 0 based index exclusiveinput
- the input data to transformcontext
- optional tweaks that the consumer of the transform would like
-
doTransform
-
hasContextualTransforms
public boolean hasContextualTransforms()- Returns:
- true if this Chainr instance has any ContextualTransforms
-
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
-