public class Chainr extends java.lang.Object implements Transform, ContextualTransform
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.
},
...
]Modifier and Type | Class and 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.
|
Modifier and Type | Field and Description |
---|---|
private java.util.List<ContextualTransform> |
actualContextualTransforms |
private java.util.List<ContextualTransform> |
transformsList |
Constructor and Description |
---|
Chainr(java.util.List<JoltTransform> joltTransforms) |
Modifier and Type | Method and 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.
|
private final java.util.List<ContextualTransform> transformsList
private final java.util.List<ContextualTransform> actualContextualTransforms
public Chainr(java.util.List<JoltTransform> joltTransforms)
public static Chainr fromSpec(java.lang.Object input)
public static Chainr fromSpec(java.lang.Object input, ChainrInstantiator instantiator)
public java.lang.Object transform(java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
transform
in interface ContextualTransform
input
- a JSON (Jackson-parsed) maps-of-maps object to transformcontext
- optional tweaks that the consumer of the transform would likeTransformException
- if the specification is malformed, an operation is not
found, or if one of the specified transforms throws an exception.public java.lang.Object transform(java.lang.Object input)
Transform
public java.lang.Object transform(int to, java.lang.Object input)
input
- the input data to transformto
- transform from the chainrSpec to end with: 0 based index exclusivepublic java.lang.Object transform(int to, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
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 likepublic java.lang.Object transform(int from, int to, java.lang.Object input)
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 exclusivepublic java.lang.Object transform(int from, int to, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
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 likeprivate static java.lang.Object doTransform(java.util.List<ContextualTransform> transforms, java.lang.Object input, java.util.Map<java.lang.String,java.lang.Object> context)
public boolean hasContextualTransforms()
public java.util.List<ContextualTransform> getContextualTransforms()