Skip to content

Commit

Permalink
review up: var
Browse files Browse the repository at this point in the history
  • Loading branch information
brig committed Feb 8, 2025
1 parent 24c71ab commit 131803f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -27,10 +27,10 @@ public class LazyEvalList extends AbstractList<Object> {
private final LazyExpressionEvaluator evaluator;
private final LazyEvalContext context;
private final Set<Integer> inflightKeys = new HashSet<>();
private final List<Object> originalValues;
private final List<?> originalValues;
private final Map<Integer, Object> evaluatedValues = new HashMap<>();

public LazyEvalList(LazyExpressionEvaluator evaluator, LazyEvalContext context, List<Object> src) {
public LazyEvalList(LazyExpressionEvaluator evaluator, LazyEvalContext context, List<?> src) {
this.evaluator = evaluator;
this.context = context;
this.originalValues = src;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public <T> T eval(EvalContext ctx, Object value, Class<T> expectedType) {
}

if (value instanceof Map) {
Map<String, Object> m = nestedToMap((Map<String, Object>)value);
var m = nestedToMap((Map<String, Object>) value);
value = mergeWithVariables(ctx, m, ((Map<String, Object>) value).keySet().stream().filter(ConfigurationUtils::isNestedKey).collect(Collectors.toSet()));
}

if (ctx.useIntermediateResults() && value instanceof Map) {
Map<String, Object> m = (Map<String, Object>) value;
var m = (Map<String, Object>) value;
if (m.isEmpty()) {
return expectedType.cast(m);
}
Expand All @@ -89,23 +89,20 @@ <T> T evalValue(LazyEvalContext ctx, Object value, Class<T> expectedType) {
}

if (value instanceof Map) {
Map<String, Object> m = (Map<String, Object>) value;
var m = (Map<String, Object>) value;
return expectedType.cast(new LazyEvalMap(this, ctx, m));
} else if (value instanceof List) {
List<Object> src = (List<Object>) value;
} else if (value instanceof List<?> src) {
return expectedType.cast(new LazyEvalList(this, ctx, src));
} else if (value instanceof Set) {
Set<Object> src = (Set<Object>) value;

} else if (value instanceof Set<?> src) {
// use LinkedHashSet to preserve the order of keys
Set<Object> dst = new LinkedHashSet<>(src.size());
for (Object vv : src) {
var dst = new LinkedHashSet<>(src.size());
for (var vv : src) {
dst.add(evalValue(ctx, vv, Object.class));
}

return expectedType.cast(dst);
} else if (value.getClass().isArray()) {
Object[] src = (Object[]) value;
var src = (Object[]) value;
if (src.length == 0) {
return expectedType.cast(src);
}
Expand All @@ -125,9 +122,9 @@ <T> T evalValue(LazyEvalContext ctx, Object value, Class<T> expectedType) {
}

private <T> T evalExpr(LazyEvalContext ctx, String expr, Class<T> type) {
ELResolver resolver = createResolver(ctx, expressionFactory);
var resolver = createResolver(ctx, expressionFactory);

StandardELContext sc = new StandardELContext(expressionFactory) {
var sc = new StandardELContext(expressionFactory) {
@Override
public ELResolver getELResolver() {
return resolver;
Expand All @@ -141,24 +138,19 @@ public FunctionMapper getFunctionMapper() {
sc.putContext(ExpressionFactory.class, expressionFactory);

try {
ValueExpression x = expressionFactory.createValueExpression(sc, expr, type);
Object v = withEvalContext(ctx, () -> x.getValue(sc));
var x = expressionFactory.createValueExpression(sc, expr, type);
var v = withEvalContext(ctx, () -> x.getValue(sc));
return type.cast(v);
} catch (PropertyNotFoundException e) {
if (ctx.undefinedVariableAsNull()) {
return null;
}

String errorMessage;

String propName = propertyNameFromException(e);
if (propName != null) {
errorMessage = String.format("Can't find a variable %s. " +
"Check if it is defined in the current scope. Details: %s", propName, e.getMessage());
} else {
errorMessage = String.format("Can't find the specified variable. " +
"Check if it is defined in the current scope. Details: %s", e.getMessage());
}
var errorMessage = propertyNameFromException(e)
.map(propName -> String.format("Can't find a variable %s. " +
"Check if it is defined in the current scope. Details: %s", propName, e.getMessage()))
.orElse(String.format("Can't find the specified variable. " +
"Check if it is defined in the current scope. Details: %s", e.getMessage()));

throw new UserDefinedException(exceptionPrefix(expr) + errorMessage);
} catch (MethodNotFoundException e) {
Expand Down Expand Up @@ -187,7 +179,7 @@ public FunctionMapper getFunctionMapper() {
private ELResolver createResolver(LazyEvalContext evalContext,
ExpressionFactory expressionFactory) {

CompositeELResolver r = new CompositeELResolver();
var r = new CompositeELResolver();
if (evalContext.scope() != null) {
r.add(new VariableResolver(evalContext.scope()));
}
Expand All @@ -210,7 +202,7 @@ private ELResolver createResolver(LazyEvalContext evalContext,
}

private static FunctionMapper createFunctionMapper() {
Map<String, Method> functions = new HashMap<>();
var functions = new HashMap<String, Method>();
functions.put("hasVariable", HasVariableFunction.getMethod());
functions.put("hasNonNullVariable", HasNonNullVariableFunction.getMethod());
functions.put("orDefault", OrDefaultFunction.getMethod());
Expand All @@ -230,10 +222,10 @@ private static boolean hasExpression(String s) {
}

private static Map<String, Object> nestedToMap(Map<String, Object> value) {
Map<String, Object> result = new LinkedHashMap<>();
for (Map.Entry<String, Object> e : value.entrySet()) {
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (var e : value.entrySet()) {
if (isNestedKey(e.getKey())) {
Map<String, Object> m = toNested(e.getKey(), e.getValue());
var m = toNested(e.getKey(), e.getValue());
result = deepMerge(result, m);
} else {
result.put(e.getKey(), e.getValue());
Expand All @@ -244,16 +236,16 @@ private static Map<String, Object> nestedToMap(Map<String, Object> value) {

@SuppressWarnings("unchecked")
private static Map<String, Object> mergeWithVariables(EvalContext ctx, Map<String, Object> m, Set<String> nestedKeys) {
Map<String, Object> result = new LinkedHashMap<>();
for (Map.Entry<String, Object> e : m.entrySet()) {
String key = e.getKey();
Object value = e.getValue();
boolean isNested = nestedKeys.stream().anyMatch(s -> s.startsWith(key + "."));
var result = new LinkedHashMap<String, Object>();
for (var e : m.entrySet()) {
var key = e.getKey();
var value = e.getValue();
var isNested = nestedKeys.stream().anyMatch(s -> s.startsWith(key + "."));
if (isNested && ctx.variables().has(key)) {
Object o = ctx.variables().get(key);
var o = ctx.variables().get(key);
if (o instanceof Map && e.getValue() instanceof Map) {
Map<String, Object> valuesFromVars = (Map<String, Object>)o;
value = deepMerge(valuesFromVars, (Map<String, Object>)value);
var valuesFromVars = (Map<String, Object>) o;
value = deepMerge(valuesFromVars, (Map<String, Object>) value);
}
}
result.put(key, value);
Expand All @@ -267,15 +259,15 @@ private static String exceptionPrefix(String expr) {

private static final String PROP_NOT_FOUND_EL_MESSAGE = "ELResolver cannot handle a null base Object with identifier ";

private static String propertyNameFromException(PropertyNotFoundException e) {
private static Optional<String> propertyNameFromException(PropertyNotFoundException e) {
if (e.getMessage() == null) {
return null;
return Optional.empty();
}

if (e.getMessage().startsWith(PROP_NOT_FOUND_EL_MESSAGE)) {
return e.getMessage().substring(PROP_NOT_FOUND_EL_MESSAGE.length());
return Optional.of(e.getMessage().substring(PROP_NOT_FOUND_EL_MESSAGE.length()));
}

return null;
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.walmartlabs.concord.svm.ThreadId;
import org.immutables.value.Value;

import javax.el.MethodNotFoundException;
import javax.inject.Inject;
import java.io.Serializable;
import java.lang.annotation.Annotation;
Expand All @@ -52,22 +51,22 @@ public TaskCallInterceptor(Set<TaskCallListener> listeners) {

public <T> T invoke(CallContext ctx, Method method, Callable<T> callable) throws TaskException {
// record the PRE event
TaskCallEvent preEvent = eventBuilder(Phase.PRE, method, ctx).build();
var preEvent = eventBuilder(Phase.PRE, method, ctx).build();
listeners.forEach(l -> l.onEvent(preEvent));

// call the callable and measure the duration
T result = null;
Exception error = null;
long startedAt = System.currentTimeMillis();
var startedAt = System.currentTimeMillis();
try {
result = callable.call();
} catch (Exception e) {
error = e;
}
long duration = System.currentTimeMillis() - startedAt;
var duration = System.currentTimeMillis() - startedAt;

// record the POST event
TaskCallEvent postEvent = eventBuilder(Phase.POST, method, ctx)
var postEvent = eventBuilder(Phase.POST, method, ctx)
.error(errorMessage(error))
.duration(duration)
.result(result instanceof Serializable ? (Serializable) result : null)
Expand Down Expand Up @@ -116,17 +115,17 @@ public interface Method {
@AllowNulls
@Value.Default
default List<Object> arguments() {
return Collections.emptyList();
return List.of();
}

@AllowNulls
@Value.Default
default List<List<Annotation>> annotations() {
return Collections.emptyList();
return List.of();
}

static Method of(Class<? extends Task> taskClass, String methodName, List<Object> params) throws javax.el.MethodNotFoundException {
List<List<Annotation>> annotations = Collections.emptyList();
List<List<Annotation>> annotations = List.of();
var m = ReflectionUtil.findMethod(taskClass, methodName, null, params.toArray());
if (m != null && !m.isVarArgs()) {
annotations = Arrays.stream(m.getParameterAnnotations())
Expand Down

0 comments on commit 131803f

Please sign in to comment.