Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5147: [clojure kernel] fix problem with result serialization (lazy sequence) #5149

Merged
merged 1 commit into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import java.util.Hashtable;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.lwhite1.tablesaw.api.Table;
import com.twosigma.beaker.easyform.DisplayEasyForm;
import com.twosigma.beaker.easyform.EasyForm;
import com.twosigma.beaker.easyform.formitem.*;
import com.twosigma.beaker.easyform.serializer.*;
import com.twosigma.beaker.fileloader.CsvPlotReader;
import com.twosigma.beaker.jvm.object.OutputContainer;
import com.twosigma.beaker.mimetype.MIMEContainer;
import com.twosigma.beaker.table.TableDisplay;
Expand Down Expand Up @@ -82,8 +81,6 @@
import com.twosigma.beaker.chart.xychart.plotitem.Text;
import com.twosigma.beaker.chart.xychart.plotitem.YAxis;
import com.twosigma.beaker.widgets.DisplayAnyWidget;
import com.twosigma.beaker.widgets.DisplayOutputContainer;
import com.twosigma.beaker.widgets.DisplayWidget;
import com.twosigma.beaker.widgets.Widget;
import com.twosigma.beaker.widgets.internal.InternalWidget;

Expand All @@ -92,6 +89,8 @@

public class SerializeToString {

private static ObjectMapper objectMapper = new ObjectMapper();

private static ObjectMapper mapper;
private static Map<Class<?>, JsonSerializer> serializerMap = new Hashtable<>();
private static Map<Class<?>, Object> internalWidgetMap = new Hashtable<>();
Expand Down Expand Up @@ -158,51 +157,56 @@ public class SerializeToString {
mapper = new ObjectMapper();
mapper.registerModule(module);
}

public static boolean isWidget(Object input) {
return (input instanceof EasyForm)
|| (input instanceof OutputContainer)
|| (input instanceof Table)
|| isInternalWidget(input)
|| (input instanceof Widget);
}


public static MIMEContainer doit(Object input) {
MIMEContainer ret = null;
if(input != null){
if (input != null) {
if (isWidget(input)) {
DisplayAnyWidget.display(input);
ret = Text("");
} else if(input instanceof MIMEContainer) {
} else if (input instanceof MIMEContainer) {
ret = (MIMEContainer) input;
} else{
ret = Text(input);
} else {
ret = asString(input);
}
}else{
} else {
ret = Text("null");
}
return ret;
}

public static boolean isInternalWidget(Object result){

private static MIMEContainer asString(Object input) {
if (input instanceof String) {
return Text(input);
}
try {
return Text(objectMapper.writeValueAsString(input));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private static boolean isWidget(Object input) {
return (input instanceof EasyForm)
|| (input instanceof OutputContainer)
|| (input instanceof Table)
|| isInternalWidget(input)
|| (input instanceof Widget);
}

public static boolean isInternalWidget(Object result) {
boolean ret = false;
if(result != null && result instanceof InternalWidget ){
if (result != null && result instanceof InternalWidget) {
for (Class<?> clazz : internalWidgetMap.keySet()) {
ret = clazz.isAssignableFrom(result.getClass());
if(ret){
if (ret) {
break;
}
}
}
return ret;
}

public static void showInternalWidget(Object result) {
InternalWidget widget = (InternalWidget) result;
widget.sendModel();
DisplayWidget.display(widget);
}

protected static Map<Class<?>, JsonSerializer> getSerializerMap() {
return serializerMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.twosigma.beaker.jvm.threads;

public interface BeakerOutputHandler {
public void write(int b);
public void write(byte[] b);
public void write(byte[] b, int off, int len);
void write(int b);
void write(byte[] b);
void write(byte[] b, int off, int len);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import com.twosigma.beaker.fileloader.CsvPlotReader;
import com.twosigma.beaker.jvm.object.OutputContainer;
import com.twosigma.beaker.table.TableDisplay;
import static com.twosigma.beaker.SerializeToString.showInternalWidget;
import com.twosigma.beaker.widgets.internal.InternalWidget;

import static com.twosigma.beaker.SerializeToString.isInternalWidget;

public class DisplayAnyWidget {
Expand All @@ -45,4 +46,10 @@ public static void display(Object input) {
}
}

private static void showInternalWidget(Object result) {
InternalWidget widget = (InternalWidget) result;
widget.sendModel();
DisplayWidget.display(widget);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static String toJson(Object result) {
return result != null ? result.toString() : null;
}

protected static boolean isBeakerChart(Object result){
private static boolean isBeakerChart(Object result){
boolean ret = false;
if(result != null){
for (Class<?> clazz : getSerializerMap().keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;

import static com.twosigma.beaker.jupyter.msg.MessageCreator.NULL_RESULT;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

public class MessageCreatorTest {
Expand Down Expand Up @@ -55,12 +56,23 @@ public void createMessageWithNullResult_shouldReturnNullStringForNull() throws E
@Test
public void createMessageWithNotNullResult_shouldReturnResult() throws Exception {
//given
seo.finished("1");
seo.finished("NotNullResult");
//when
List<MessageHolder> message = messageCreator.createMessage(seo);
//then
Map data = TestWidgetUtils.getData(message.get(0).getMessage());
assertThat(data.get(MessageCreator.TEXT_PLAIN)).isEqualTo("1");
assertThat(data.get(MessageCreator.TEXT_PLAIN)).isEqualTo("NotNullResult");
}

@Test
public void createMessageForCollection() throws Exception {
//given
seo.finished(asList("1","2"));
//when
List<MessageHolder> message = messageCreator.createMessage(seo);
//then
Map data = TestWidgetUtils.getData(message.get(0).getMessage());
assertThat(data.get(MessageCreator.TEXT_PLAIN)).isEqualTo("[\"1\",\"2\"]");
}

@Test
Expand Down