Skip to content

Commit

Permalink
refactor(expressions): Remove deprecated versioning code (#2131)
Browse files Browse the repository at this point in the history
- removed unused spelEvaluator version code
- small cosmetic changes
  • Loading branch information
jeyrschabu committed Apr 9, 2018
1 parent fcb55a3 commit 9d3fb8c
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ public <T> T transform(T source, EvaluationContext evaluationContext, Expression
/**
* Traverses and attempts to evaluate expressions
* Failures can either be INFO (for a simple unresolved expression) or ERROR when an exception is thrown
*
* @param source
* @param evaluationContext
* @param summary
* @return the transformed source object
*/
public <T> T transform(T source, EvaluationContext evaluationContext, ExpressionEvaluationSummary summary, Map<String, ?> additionalContext) {
Expand Down Expand Up @@ -213,7 +209,7 @@ static String escapeSimpleExpression(String expression) {
/**
* Lazily include the execution object (#root.execution) for Stage locating functions & aliases
*
* @param expression #stage('property') becomes #stage(#root.execution, 'property')
* @param e #stage('property') becomes #stage(#root.execution, 'property')
* @return an execution aware helper function
*/
private static String includeExecutionParameter(String e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,111 +17,30 @@
package com.netflix.spinnaker.orca.pipeline.expressions;

import java.util.*;
import com.netflix.spinnaker.orca.pipeline.model.Execution;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import com.netflix.spinnaker.orca.pipeline.util.ContextFunctionConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import static com.netflix.spinnaker.orca.pipeline.expressions.PipelineExpressionEvaluator.ExpressionEvaluationVersion.V1;
import static com.netflix.spinnaker.orca.pipeline.expressions.PipelineExpressionEvaluator.ExpressionEvaluationVersion.V2;

public class PipelineExpressionEvaluator extends ExpressionsSupport implements ExpressionEvaluator {
public static final String SUMMARY = "expressionEvaluationSummary";
private static final String SPEL_EVALUATOR = "spelEvaluator";
public static final String ERROR = "Failed Expression Evaluation";

private final Logger log = LoggerFactory.getLogger(PipelineExpressionEvaluator.class);

private final ExpressionParser parser = new SpelExpressionParser();
private final String spelEvaluator;

public interface ExpressionEvaluationVersion {
String V2 = "v2";
String V1 = "v1";
}

public PipelineExpressionEvaluator(final ContextFunctionConfiguration contextFunctionConfiguration) {
super(contextFunctionConfiguration);
spelEvaluator = contextFunctionConfiguration.getSpelEvaluator();
}

@Override
public Map<String, Object> evaluate(Map<String, Object> source, Object rootObject, ExpressionEvaluationSummary summary, boolean ignoreUnknownProperties) {
StandardEvaluationContext evaluationContext = newEvaluationContext(rootObject, ignoreUnknownProperties);
return new ExpressionTransform(parserContext, parser).transform(source, evaluationContext, summary);
}

@Deprecated // V2 is default, v1 is not available anymore.
public boolean shouldUseV2Evaluator(Object obj) {
try {
String versionInPipeline = getSpelVersion(obj);
if (Arrays.asList(V1, V2).contains(versionInPipeline) && obj instanceof Map) {
updateSpelEvaluatorVersion((Map) obj, versionInPipeline);
}

return !V1.equals(versionInPipeline) && (V2.equals(spelEvaluator) || V2.equals(versionInPipeline));
} catch (Exception e) {
log.error("Failed to determine whether to use v2 expression evaluator. using V1.", e);
}

return false;
}

private static boolean hasVersionInContext(Object obj) {
return obj instanceof Stage && ((Stage) obj).getContext().containsKey(SPEL_EVALUATOR);
}

private static String getSpelVersion(Object obj) {
if (obj instanceof Map) {
Map pipelineConfig = (Map) obj;
if (pipelineConfig.containsKey(SPEL_EVALUATOR)) {
return (String) pipelineConfig.get(SPEL_EVALUATOR);
}

List<Map> stages = (List<Map>) Optional.ofNullable(pipelineConfig.get("stages")).orElse(Collections.emptyList());
Map stage = stages
.stream()
.filter(i -> i.containsKey(SPEL_EVALUATOR))
.findFirst()
.orElse(null);

return (stage != null) ? (String) stage.get(SPEL_EVALUATOR) : null;
} else if (obj instanceof Execution) {
Execution pipeline = (Execution) obj;
Stage stage = pipeline.getStages()
.stream()
.filter(PipelineExpressionEvaluator::hasVersionInContext)
.findFirst()
.orElse(null);

return (stage != null) ? (String) stage.getContext().get(SPEL_EVALUATOR) : null;

} else if (obj instanceof Stage) {
Stage stage = (Stage) obj;
if (hasVersionInContext(obj)) {
return (String) stage.getContext().get(SPEL_EVALUATOR);
}

// if any using v2
List stages = stage.getExecution().getStages();
Stage withVersion = (Stage) stages.stream()
.filter(PipelineExpressionEvaluator::hasVersionInContext)
.findFirst()
.orElse(null);

return (withVersion != null) ? (String) withVersion.getContext().get(SPEL_EVALUATOR) : null;
}

return null;
}

private static void updateSpelEvaluatorVersion(Map rawPipeline, String versionInPipeline) {
Optional.ofNullable((List<Map>) rawPipeline.get("stages")).orElse(Collections.emptyList())
.forEach(i -> i.put(SPEL_EVALUATOR, versionInPipeline));
}
}


Expand Down

0 comments on commit 9d3fb8c

Please sign in to comment.