Permalink
Browse files

Merge pull request #280 from YannRobert/ViewsWithDosLineEndings

Support for View files using DOS End Of Line
  • Loading branch information...
YannRobert committed Nov 8, 2017
2 parents b861c2a + cceb7d0 commit b822c0d656aefb90a5e0cb5ec2de3daa969e3eaa
@@ -26,7 +26,9 @@
private final static String ITERABLE_PROPERTY_BODY = "for (var i in doc.%s) {emit(doc.%s[i], doc._id);}";
private final static String REFERING_CHILDREN_AS_SET_W_ORDER_BY = "function(doc) { if(%s) { emit([doc.%s, '%s', doc.%s], null); } }";
private final static String REFERING_CHILDREN_AS_SET = "function(doc) { if(%s) { emit([doc.%s, '%s'], null); } }";
- private final static String LINE_ENDING = String.format("%n");
+
+ private final static String LINE_ENDING_UNIX = "\n";
+ private final static String LINE_ENDING_DOS = "\r\n";
private SoftReference<ObjectMapper> mapperRef;
@@ -276,13 +278,17 @@ protected String loadResourceFromClasspath(Class<?> repositoryClass,
try {
String json = loadResourceFromClasspath(repositoryClass,
input.file());
- return mapper().readValue(json.replaceAll(LINE_ENDING, ""),
- DesignDocument.View.class);
+ return loadViewFromString(mapper(), json);
} catch (Exception e) {
throw Exceptions.propagate(e);
}
}
+ public static DesignDocument.View loadViewFromString(ObjectMapper objectMapper, String json) throws IOException {
+ return objectMapper.readValue(json.replaceAll(LINE_ENDING_DOS, "").replaceAll(LINE_ENDING_UNIX, ""),
+ DesignDocument.View.class);
+ }
+
private boolean isIterable(Class<?> type) {
return Iterable.class.isAssignableFrom(type);
}
@@ -6,6 +6,7 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
+import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
@@ -166,6 +167,20 @@ public void given_a_discriminator_is_declared_on_child_type_then_docrefs_view_sh
Map<String, DesignDocument.View> result = gen.generateViews(repo);
assertEquals(expectedDocrefsFunctionWhereChildHasDiscriminator, result.get("ektorp_docrefs_children").getMap());
}
+
+ @Test
+ public void given_json_view_file_with_unix_line_endings_then_should_parse_successfully() throws IOException {
+ SimpleViewGenerator generator = new SimpleViewGenerator();
+ DesignDocument.View view = generator.loadViewFromString(new ObjectMapper(), " {\"map\":\"function(doc) {\n emit(doc._id, doc);\n }\n\"}");
+ assertEquals("function(doc) { emit(doc._id, doc); }", view.getMap());
+ }
+
+ @Test
+ public void given_json_view_file_with_dos_line_endings_then_should_parse_successfully() throws IOException {
+ SimpleViewGenerator generator = new SimpleViewGenerator();
+ DesignDocument.View view = generator.loadViewFromString(new ObjectMapper(), " {\"map\":\"function(doc) {\r\n emit(doc._id, doc);\r\n }\r\n\"}");
+ assertEquals("function(doc) { emit(doc._id, doc); }", view.getMap());
+ }
// ******************************* S U P P O R T T Y P E S B E L O W ********************************* //

0 comments on commit b822c0d

Please sign in to comment.