Class InsertKeyTransformer

java.lang.Object
com.invirgance.convirgance.transform.InsertKeyTransformer
All Implemented Interfaces:
IdentityTransformer, Transformer

@Wiring public class InsertKeyTransformer extends Object implements IdentityTransformer
A transformer that inserts or updates a key-value pair in a JSON object. If the key already exists, its value will be replaced. If the key doesn't exist, a new key-value pair will be added.
Author:
jbanes
  • Constructor Details

    • InsertKeyTransformer

      public InsertKeyTransformer()
      Prepares a new InsertKeyTransformer. The setKey(String) and setValue(Object) methods must be called before attempting a transform.
    • InsertKeyTransformer

      public InsertKeyTransformer(String key, Object value)
      Creates a new InsertKeyTransformer with the specified key-value pair.

      Example usage:

       // Create transformer to add/update a "status" field
       InsertKeyTransformer transformer = new InsertKeyTransformer("status", "active");
       
       // Input:  {"id": 123, "name": "John"}
       // Output: {"id": 123, "name": "John", "status": "active"}
       
       // Input:  {"id": 456, "status": "inactive", "name": "Jane"}
       // Output: {"id": 456, "status": "active", "name": "Jane"}
       
      Parameters:
      key - The key to insert or update.
      value - The value to associate with the key. Supports the use of a ValueGenerator
  • Method Details

    • getKey

      public String getKey()
      Gets the key that will be inserted or updated.
      Returns:
      The key name.
      Throws:
      NullPointerException - if the key has not been set.
    • setKey

      public void setKey(String key)
      Sets the key that will be inserted or updated.
      Parameters:
      key - The key name.
    • getValue

      public Object getValue()
      Gets the value that will be associated with the key.
      Returns:
      The value to be inserted or used for update.
      Throws:
      NullPointerException - if the value has not been set.
    • setValue

      public void setValue(Object value)
      Sets the value that will be associated with the key. If the value is a ValueGenerator, the result of the generator will be inserted rather than the generator itself.
      Parameters:
      value - The value to be inserted or used for update.
    • transform

      public JSONObject transform(JSONObject record) throws ConvirganceException
      Transforms a JSON object by inserting or updating the specified key-value pair. This operation modifies the input object directly.

      If the key already exists in the input object, its value will be replaced. If the key doesn't exist, a new key-value pair will be added to the object.

      If the value is a ValueGenerator, the result of generation will be set rather than the generator itself.

      Specified by:
      transform in interface IdentityTransformer
      Parameters:
      record - The JSON object to modify.
      Returns:
      The modified JSON object (same instance as input).
      Throws:
      ConvirganceException - if the key is null, or if there's an error during the modification operation.