Class Chainr

java.lang.Object
com.bazaarvoice.jolt.Chainr
All Implemented Interfaces:
ContextualTransform, JoltTransform, Transform

public class Chainr extends Object implements Transform, ContextualTransform
Chainr is the JOLT mechanism for chaining JoltTransforms 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. }, ... ]
  • Field Details

  • Constructor Details

  • Method Details

    • fromSpec

      public static Chainr fromSpec(Object input)
    • fromSpec

      public static Chainr fromSpec(Object input, ChainrInstantiator instantiator)
    • transform

      public Object transform(Object input, Map<String,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 interface ContextualTransform
      Parameters:
      input - a JSON (Jackson-parsed) maps-of-maps object to transform
      context - 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 Object transform(Object input)
      Description copied from interface: Transform
      Execute a transform on some input JSON with optionally provided "context" and return the result.
      Specified by:
      transform in interface Transform
      Parameters:
      input - the JSON object to transform in plain vanilla Jackson Mapinvalid input: '<'String, Object> style
      Returns:
      the results of the transformation
    • transform

      public Object transform(int to, Object input)
      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 exclusive
      input - the input data to transform
    • transform

      public Object transform(int to, Object input, Map<String,Object> context)
      Useful for testing and debugging.
      Parameters:
      to - transform from the chainrSpec to end with: 0 based index exclusive
      input - the input data to transform
      context - optional tweaks that the consumer of the transform would like
    • transform

      public Object transform(int from, int to, Object input)
      Useful for testing and debugging.
      Parameters:
      from - transform from the chainrSpec to start with: 0 based index
      to - transform from the chainrSpec to end with: 0 based index exclusive
      input - the input data to transform
    • transform

      public Object transform(int from, int to, Object input, Map<String,Object> context)
      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 index
      to - transform from the chainrSpec to end with: 0 based index exclusive
      input - the input data to transform
      context - optional tweaks that the consumer of the transform would like
    • doTransform

      private static Object doTransform(List<ContextualTransform> transforms, Object input, Map<String,Object> context)
    • hasContextualTransforms

      public boolean hasContextualTransforms()
      Returns:
      true if this Chainr instance has any ContextualTransforms
    • getContextualTransforms

      public 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