Skip to content

Commit b822c0d

Browse files
authored
Merge pull request #280 from YannRobert/ViewsWithDosLineEndings
Support for View files using DOS End Of Line
2 parents b861c2a + cceb7d0 commit b822c0d

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

org.ektorp/src/main/java/org/ektorp/support/SimpleViewGenerator.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ public class SimpleViewGenerator {
2626
private final static String ITERABLE_PROPERTY_BODY = "for (var i in doc.%s) {emit(doc.%s[i], doc._id);}";
2727
private final static String REFERING_CHILDREN_AS_SET_W_ORDER_BY = "function(doc) { if(%s) { emit([doc.%s, '%s', doc.%s], null); } }";
2828
private final static String REFERING_CHILDREN_AS_SET = "function(doc) { if(%s) { emit([doc.%s, '%s'], null); } }";
29-
private final static String LINE_ENDING = String.format("%n");
29+
30+
private final static String LINE_ENDING_UNIX = "\n";
31+
private final static String LINE_ENDING_DOS = "\r\n";
3032

3133
private SoftReference<ObjectMapper> mapperRef;
3234

@@ -276,13 +278,17 @@ protected DesignDocument.View loadViewFromFile(
276278
try {
277279
String json = loadResourceFromClasspath(repositoryClass,
278280
input.file());
279-
return mapper().readValue(json.replaceAll(LINE_ENDING, ""),
280-
DesignDocument.View.class);
281+
return loadViewFromString(mapper(), json);
281282
} catch (Exception e) {
282283
throw Exceptions.propagate(e);
283284
}
284285
}
285286

287+
public static DesignDocument.View loadViewFromString(ObjectMapper objectMapper, String json) throws IOException {
288+
return objectMapper.readValue(json.replaceAll(LINE_ENDING_DOS, "").replaceAll(LINE_ENDING_UNIX, ""),
289+
DesignDocument.View.class);
290+
}
291+
286292
private boolean isIterable(Class<?> type) {
287293
return Iterable.class.isAssignableFrom(type);
288294
}

org.ektorp/src/test/java/org/ektorp/support/SimpleViewGeneratorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.Assert.assertTrue;
77
import static org.mockito.Mockito.mock;
88

9+
import java.io.IOException;
910
import java.io.Serializable;
1011
import java.util.List;
1112
import java.util.Map;
@@ -166,6 +167,20 @@ public void given_a_discriminator_is_declared_on_child_type_then_docrefs_view_sh
166167
Map<String, DesignDocument.View> result = gen.generateViews(repo);
167168
assertEquals(expectedDocrefsFunctionWhereChildHasDiscriminator, result.get("ektorp_docrefs_children").getMap());
168169
}
170+
171+
@Test
172+
public void given_json_view_file_with_unix_line_endings_then_should_parse_successfully() throws IOException {
173+
SimpleViewGenerator generator = new SimpleViewGenerator();
174+
DesignDocument.View view = generator.loadViewFromString(new ObjectMapper(), " {\"map\":\"function(doc) {\n emit(doc._id, doc);\n }\n\"}");
175+
assertEquals("function(doc) { emit(doc._id, doc); }", view.getMap());
176+
}
177+
178+
@Test
179+
public void given_json_view_file_with_dos_line_endings_then_should_parse_successfully() throws IOException {
180+
SimpleViewGenerator generator = new SimpleViewGenerator();
181+
DesignDocument.View view = generator.loadViewFromString(new ObjectMapper(), " {\"map\":\"function(doc) {\r\n emit(doc._id, doc);\r\n }\r\n\"}");
182+
assertEquals("function(doc) { emit(doc._id, doc); }", view.getMap());
183+
}
169184

170185
// ******************************* S U P P O R T T Y P E S B E L O W ********************************* //
171186

0 commit comments

Comments
 (0)