Skip to content

Commit

Permalink
Merge pull request #228 from bivasdas/moreOps
Browse files Browse the repository at this point in the history
Good stuff.
  • Loading branch information
Milo Simpson authored Jul 12, 2016
2 parents a78b209 + ccfd40e commit c7f3cec
Show file tree
Hide file tree
Showing 62 changed files with 2,015 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.bazaarvoice.jolt;

import com.bazaarvoice.jolt.cardinality.CardinalityCompositeSpec;
import com.bazaarvoice.jolt.common.Optional;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.exception.SpecException;

Expand Down Expand Up @@ -229,7 +230,7 @@ public CardinalityTransform( Object spec ) {
@Override
public Object transform( Object input ) {

rootSpec.apply( ROOT_KEY, input, new WalkedPath(), null, null );
rootSpec.apply( ROOT_KEY, Optional.of( input ), new WalkedPath(), null, null );

return input;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

package com.bazaarvoice.jolt;

import com.bazaarvoice.jolt.common.Optional;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.templatr.OpMode;
import com.bazaarvoice.jolt.templatr.TemplatrSpecBuilder;
import com.bazaarvoice.jolt.templatr.function.Function;
import com.bazaarvoice.jolt.templatr.function.Math;
import com.bazaarvoice.jolt.templatr.function.Strings;
import com.bazaarvoice.jolt.templatr.spec.TemplatrCompositeSpec;
import com.bazaarvoice.jolt.modifier.OpMode;
import com.bazaarvoice.jolt.modifier.TemplatrSpecBuilder;
import com.bazaarvoice.jolt.modifier.function.Function;
import com.bazaarvoice.jolt.modifier.function.Lists;
import com.bazaarvoice.jolt.modifier.function.Math;
import com.bazaarvoice.jolt.modifier.function.Strings;
import com.bazaarvoice.jolt.modifier.spec.ModifierCompositeSpec;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -33,7 +35,7 @@
/**
* Base Templatr transform that to behave differently based on provided opMode
*/
public abstract class Templatr implements SpecDriven, ContextualTransform {
public abstract class Modifier implements SpecDriven, ContextualTransform {

private static final Map<String, Function> STOCK_FUNCTIONS = new HashMap<>( );

Expand All @@ -42,18 +44,28 @@ public abstract class Templatr implements SpecDriven, ContextualTransform {
STOCK_FUNCTIONS.put( "toUpper", new Strings.toUpperCase() );
STOCK_FUNCTIONS.put( "concat", new Strings.concat() );

STOCK_FUNCTIONS.put( "minOf", new com.bazaarvoice.jolt.templatr.function.Math.MinOf() );
STOCK_FUNCTIONS.put( "maxOf", new Math.MaxOf() );
STOCK_FUNCTIONS.put( "abs", new Math.Abs() );
STOCK_FUNCTIONS.put( "min", new Math.min() );
STOCK_FUNCTIONS.put( "max", new Math.max() );
STOCK_FUNCTIONS.put( "abs", new Math.abs() );
STOCK_FUNCTIONS.put( "toInteger", new Math.toInteger() );
STOCK_FUNCTIONS.put( "toDouble", new Math.toDouble() );
STOCK_FUNCTIONS.put( "toLong", new Math.toLong() );

STOCK_FUNCTIONS.put( "noop", Function.noop );
STOCK_FUNCTIONS.put( "isPresent", Function.isPresent );
STOCK_FUNCTIONS.put( "notNull", Function.notNull );
STOCK_FUNCTIONS.put( "isNull", Function.isNull );

STOCK_FUNCTIONS.put( "firstElement", new Lists.firstElement() );
STOCK_FUNCTIONS.put( "lastElement", new Lists.lastElement() );
STOCK_FUNCTIONS.put( "elementAt", new Lists.elementAt() );
STOCK_FUNCTIONS.put( "toList", new Lists.toList() );
}

private final TemplatrCompositeSpec rootSpec;
private final ModifierCompositeSpec rootSpec;

@SuppressWarnings( "unchecked" )
private Templatr(Object spec, OpMode opMode, Map<String, Function> functionsMap) {
private Modifier( Object spec, OpMode opMode, Map<String, Function> functionsMap ) {
if ( spec == null ){
throw new SpecException( opMode.name() + " expected a spec of Map type, got 'null'." );
}
Expand All @@ -67,7 +79,7 @@ private Templatr(Object spec, OpMode opMode, Map<String, Function> functionsMap)

functionsMap = Collections.unmodifiableMap( functionsMap );
TemplatrSpecBuilder templatrSpecBuilder = new TemplatrSpecBuilder( opMode, functionsMap );
rootSpec = new TemplatrCompositeSpec( ROOT_KEY, (Map<String, Object>) spec, opMode, templatrSpecBuilder );
rootSpec = new ModifierCompositeSpec( ROOT_KEY, (Map<String, Object>) spec, opMode, templatrSpecBuilder );
}

@Override
Expand All @@ -80,15 +92,15 @@ public Object transform( final Object input, final Map<String, Object> context )
WalkedPath walkedPath = new WalkedPath();
walkedPath.add( input, rootLpe );

rootSpec.apply( ROOT_KEY, input, walkedPath, null, contextWrapper );
rootSpec.apply( ROOT_KEY, Optional.of( input), walkedPath, null, contextWrapper );
return input;
}

/**
* This variant of templatr creates the key/index is missing,
* This variant of modifier creates the key/index is missing,
* and overwrites the value if present
*/
public static final class Overwritr extends Templatr {
public static final class Overwritr extends Modifier {

public Overwritr( Object spec ) {
this( spec, STOCK_FUNCTIONS );
Expand All @@ -100,9 +112,9 @@ public Overwritr( Object spec, Map<String, Function> functionsMap ) {
}

/**
* This variant of templatr only writes when the key/index is missing
* This variant of modifier only writes when the key/index is missing
*/
public static final class Definr extends Templatr {
public static final class Definr extends Modifier {

public Definr( final Object spec ) {
this( spec, STOCK_FUNCTIONS );
Expand All @@ -114,9 +126,9 @@ public Definr( Object spec, Map<String, Function> functionsMap ) {
}

/**
* This variant of templatr only writes when the key/index is missing or the value is null
* This variant of modifier only writes when the key/index is missing or the value is null
*/
public static class Defaultr extends Templatr {
public static class Defaultr extends Modifier {

public Defaultr( final Object spec ) {
this( spec, STOCK_FUNCTIONS );
Expand Down
3 changes: 2 additions & 1 deletion jolt-core/src/main/java/com/bazaarvoice/jolt/Shiftr.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.bazaarvoice.jolt;

import com.bazaarvoice.jolt.common.Optional;
import com.bazaarvoice.jolt.common.tree.MatchedElement;
import com.bazaarvoice.jolt.common.tree.WalkedPath;
import com.bazaarvoice.jolt.exception.SpecException;
Expand Down Expand Up @@ -501,7 +502,7 @@ public Object transform( Object input ) {
WalkedPath walkedPath = new WalkedPath();
walkedPath.add( input, rootLpe );

rootSpec.apply( ROOT_KEY, input, walkedPath, output, null );
rootSpec.apply( ROOT_KEY, Optional.of( input ), walkedPath, output, null );

return output.get( ROOT_KEY );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.bazaarvoice.jolt.cardinality;

import com.bazaarvoice.jolt.common.Optional;
import com.bazaarvoice.jolt.common.pathelement.AtPathElement;
import com.bazaarvoice.jolt.common.pathelement.LiteralPathElement;
import com.bazaarvoice.jolt.common.pathelement.MatchablePathElement;
Expand Down Expand Up @@ -107,8 +108,8 @@ else if ( key.contains(STAR) ) {
public abstract boolean applyCardinality( String inputKey, Object input, WalkedPath walkedPath, Object parentContainer );

@Override
public boolean apply( final String inputKey, final Object input, final WalkedPath walkedPath, final Map<String, Object> output, final Map<String, Object> context ) {
return applyCardinality( inputKey, input, walkedPath, output );
public boolean apply( final String inputKey, final Optional<Object> inputOptional, final WalkedPath walkedPath, final Map<String, Object> output, final Map<String, Object> context ) {
return applyCardinality( inputKey, inputOptional.get(), walkedPath, output );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

import com.bazaarvoice.jolt.CardinalityTransform;
import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.Defaultr;
import com.bazaarvoice.jolt.JoltTransform;
import com.bazaarvoice.jolt.Modifier;
import com.bazaarvoice.jolt.Removr;
import com.bazaarvoice.jolt.Shiftr;
import com.bazaarvoice.jolt.Sortr;
import com.bazaarvoice.jolt.SpecDriven;
import com.bazaarvoice.jolt.Templatr;
import com.bazaarvoice.jolt.exception.SpecException;
import com.bazaarvoice.jolt.utils.StringTools;

Expand All @@ -48,10 +49,10 @@ public class ChainrEntry {
static {
HashMap<String, String> temp = new HashMap<>();
temp.put( "shift", Shiftr.class.getName() );
temp.put( "default", com.bazaarvoice.jolt.Defaultr.class.getName() );
temp.put( "overwrite-beta", Templatr.Overwritr.class.getName() );
temp.put( "default-beta", Templatr.Defaultr.class.getName() );
temp.put( "define-beta", Templatr.Definr.class.getName() );
temp.put( "default", Defaultr.class.getName() );
temp.put( "modify-overwrite-beta", Modifier.Overwritr.class.getName() );
temp.put( "modify-default-beta", Modifier.Defaultr.class.getName() );
temp.put( "modify-define-beta", Modifier.Definr.class.getName() );
temp.put( "remove", Removr.class.getName() );
temp.put( "sort", Sortr.class.getName() );
temp.put( "cardinality", CardinalityTransform.class.getName() );
Expand Down
Loading

0 comments on commit c7f3cec

Please sign in to comment.