Skip to content

Commit

Permalink
fixes kirksl/karate-runner#68 (work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
Iván García Sainz-Aja committed Dec 14, 2020
1 parent 39ad70c commit f1bced2
Showing 1 changed file with 63 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.*;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.intuit.karate.core.Variable.Type.LIST;
import static com.intuit.karate.core.Variable.Type.MAP;

/**
*
* @author pthomas3
Expand All @@ -59,12 +62,14 @@ public class DapServerHandler extends SimpleChannelInboundHandler<DapMessage> im
private Channel channel;
private int nextSeq;
private long nextFrameId;
private long nextVariablesReference = 1000; // setting to 100 to avoid conflict with nextFrameId
private long focusedFrameId;
private Thread runnerThread;

private final Map<String, SourceBreakpoints> BREAKPOINTS = new ConcurrentHashMap();
protected final Map<Long, DebugThread> THREADS = new ConcurrentHashMap();
protected final Map<Long, ScenarioRuntime> FRAMES = new ConcurrentHashMap();
protected final Map<Long, Entry<String, Variable>> VARIABLES = new ConcurrentHashMap();

private boolean singleFeature;
private String launchCommand;
Expand All @@ -90,7 +95,7 @@ private static int findPos(String path) {
}

private SourceBreakpoints lookup(String pathEnd) {
for (Map.Entry<String, SourceBreakpoints> entry : BREAKPOINTS.entrySet()) {
for (Entry<String, SourceBreakpoints> entry : BREAKPOINTS.entrySet()) {
if (entry.getKey().endsWith(pathEnd)) {
return entry.getValue();
}
Expand Down Expand Up @@ -139,17 +144,39 @@ private List<Map<String, Object>> frames(Number threadId) {
return list;
}

private List<Map<String, Object>> variables(Number frameId) {
private List<Map<String, Object>> variables(Long frameId) {
if (frameId == null) {
return Collections.EMPTY_LIST;
}
focusedFrameId = frameId.longValue();
ScenarioRuntime context = FRAMES.get(frameId.longValue());
if (context == null) {

String parentExpression = "";
Map<String, Variable> vars = null;
if(FRAMES.containsKey(frameId)) {
focusedFrameId = frameId;
vars = FRAMES.get(frameId).engine.vars;
} else if (VARIABLES.containsKey(frameId)) {
vars = new HashMap<>();
Entry<String, Variable> varEntry = VARIABLES.get(frameId);
parentExpression = varEntry.getKey();
Variable var = varEntry.getValue();
if(var.type == LIST) {
List<Object> list = ((List) var.getValue());
for (int i = 0; i < list.size(); i++) {
vars.put(String.valueOf(i), new Variable(list.get(i)));
}
} else if(var.type == MAP) {
Map<String, Object> map = ((Map) var.getValue());
for (Entry<String, Object> entry : map.entrySet()) {
vars.put(entry.getKey(), new Variable(entry.getValue()));
}
}
} else {
return Collections.EMPTY_LIST;
}

String finalParentExpression = parentExpression;
List<Map<String, Object>> list = new ArrayList();
context.engine.vars.forEach((k, v) -> {
vars.forEach((k, v) -> {
if (v != null) {
Map<String, Object> map = new HashMap();
map.put("name", k);
Expand All @@ -160,17 +187,14 @@ private List<Map<String, Object>> variables(Number frameId) {
map.put("value", "(unknown)");
}
map.put("type", v.type.name());
switch (v.type) {
case LIST:
case MAP:
map.put("presentationHint", "data");
break;
default:
// do nothing
if(v.type == LIST || v.type == MAP) {
VARIABLES.put(++nextVariablesReference, new SimpleEntry(finalParentExpression + k + ".", v));
map.put("presentationHint", "data");
map.put("variablesReference", nextVariablesReference);
} else {
map.put("variablesReference", 0);
}
map.put("evaluateName", k);
// if > 0 , this can be used by client to request more info
map.put("variablesReference", 0);
map.put("evaluateName", finalParentExpression + k);
list.add(map);
}
});
Expand Down Expand Up @@ -256,8 +280,8 @@ private void handleRequest(DapMessage req, ChannelHandlerContext ctx) {
ctx.write(response(req).body("scopes", Collections.singletonList(scope)));
break;
case "variables":
Number variablesReference = req.getArgument("variablesReference", Number.class);
ctx.write(response(req).body("variables", variables(variablesReference)));
Integer variablesReference = req.getArgument("variablesReference", Integer.class);
ctx.write(response(req).body("variables", variables(variablesReference.longValue())));
break;
case "next":
thread(req).next().resume();
Expand Down Expand Up @@ -291,12 +315,7 @@ private void handleRequest(DapMessage req, ChannelHandlerContext ctx) {
ScenarioRuntime evalContext = FRAMES.get(evalFrameId.longValue());
String result;
if ("clipboard".equals(reqContext) || "hover".equals(reqContext)) {
try {
Variable v = evalContext.engine.vars.get(expression);
result = v.getAsPrettyString();
} catch (Exception e) {
result = "[error] " + e.getMessage();
}
result = evaluateVarExpression(evalContext.engine.vars, expression);
} else {
evaluatePreStep(evalContext);
Result evalResult = evalContext.evalAsStep(expression);
Expand Down Expand Up @@ -335,6 +354,21 @@ private void handleRequest(DapMessage req, ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}

protected String evaluateVarExpression(Map<String, Variable> vars, String expression) {
String result = "";
if(expression.contains(".")) {
// TODO get nested variables
} else {
try {
Variable v = vars.get(expression);
result = v.getAsPrettyString();
} catch (Exception e) {
result = "[error] " + e.getMessage();
}
}
return result;
}

protected void evaluatePreStep(ScenarioRuntime context) {
if (preStep == null) {
return;
Expand Down

0 comments on commit f1bced2

Please sign in to comment.