Skip to content

Commit cae161f

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-1175 - Allow direct mapping of Collection like structures like java.util.List via MappingRedisConverter.
It is now possible to hand a plain list to the converter that is then converted into a hash structure using index numbers as keys. Original pull request: spring-projects#546.
1 parent 1ceb7d4 commit cae161f

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ public MappingRedisConverter(@Nullable RedisMappingContext mappingContext, @Null
176176
*/
177177
@Override
178178
public <R> R read(Class<R> type, RedisData source) {
179+
180+
TypeInformation<?> readType = typeMapper.readType(source.getBucket().getPath(), ClassTypeInformation.from(type));
181+
if(readType.isCollectionLike()) {
182+
return (R) readCollectionOrArray("", ArrayList.class, Object.class, source.getBucket());
183+
}
179184
return readInternal("", type, source);
180185
}
181186

@@ -395,7 +400,11 @@ public void write(Object source, RedisData sink) {
395400

396401
sink.setKeyspace(entity.getKeySpace());
397402

398-
writeInternal(entity.getKeySpace(), "", source, entity.getTypeInformation(), sink);
403+
if(entity.getTypeInformation().isCollectionLike()) {
404+
writeCollection(entity.getKeySpace(), "", (List) source, entity.getTypeInformation().getComponentType(), sink);
405+
} else {
406+
writeInternal(entity.getKeySpace(), "", source, entity.getTypeInformation(), sink);
407+
}
399408

400409
Object identifier = entity.getIdentifierAccessor(source).getIdentifier();
401410

@@ -713,7 +722,7 @@ private void writeCollection(String keyspace, String path, @Nullable Iterable<?>
713722
break;
714723
}
715724

716-
String currentPath = path + ".[" + i + "]";
725+
String currentPath = path + (path.equals("") ? "" : ".") + "[" + i + "]";
717726

718727
if (!ClassUtils.isAssignable(typeHint.getType(), value.getClass())) {
719728
throw new MappingException(

src/test/java/org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Date;
3737
import java.util.HashMap;
3838
import java.util.LinkedHashMap;
39+
import java.util.List;
3940
import java.util.Map;
4041
import java.util.UUID;
4142

@@ -1884,6 +1885,38 @@ public void readEntityWithCustomConverter() {
18841885
assertThat(target.getAccountName()).isEqualTo("Golam Mazid Sajib");
18851886
}
18861887

1888+
@Test // DATAREDIS-1175
1889+
public void writePlainList() {
1890+
1891+
List<Object> source = Arrays.asList("Hello", "stream", "message", 100L);
1892+
RedisTestData target = write(source);
1893+
1894+
System.out.println(target.getBucket().toString());
1895+
1896+
assertThat(target).containsEntry("[0]", "Hello") //
1897+
.containsEntry("[1]", "stream") //
1898+
.containsEntry("[2]", "message") //
1899+
.containsEntry("[3]", "100");
1900+
}
1901+
1902+
@Test // DATAREDIS-1175
1903+
public void readPlainList() {
1904+
1905+
Map<String, String> source = new LinkedHashMap<>();
1906+
source.put("[0]._class", "java.lang.String");
1907+
source.put("[0]", "Hello");
1908+
source.put("[1]._class", "java.lang.String");
1909+
source.put("[1]", "stream");
1910+
source.put("[2]._class", "java.lang.String");
1911+
source.put("[2]", "message");
1912+
source.put("[3]._class", "java.lang.Long");
1913+
source.put("[3]", "100");
1914+
1915+
List target = read(List.class, source);
1916+
1917+
assertThat(target).containsExactly("Hello", "stream", "message", 100L);
1918+
}
1919+
18871920
private RedisTestData write(Object source) {
18881921

18891922
RedisData rdo = new RedisData();

src/test/java/org/springframework/data/redis/test/util/RedisTestData.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,20 @@ private String getString(String path) {
140140
return actual.get(path);
141141
}
142142

143-
private static Map<String, String> toStringMap(Map<String, byte[]> source) {
143+
}
144144

145-
Map<String, String> converted = new LinkedHashMap<>();
146145

147-
source.forEach((k, v) -> converted.put(k, new String(v, StandardCharsets.UTF_8)));
146+
private static Map<String, String> toStringMap(Map<String, byte[]> source) {
148147

149-
return converted;
150-
}
148+
Map<String, String> converted = new LinkedHashMap<>();
149+
150+
source.forEach((k, v) -> converted.put(k, new String(v, StandardCharsets.UTF_8)));
151+
152+
return converted;
153+
}
154+
155+
@Override
156+
public String toString() {
157+
return toStringMap(getBucket().asMap()).toString();
151158
}
152159
}

0 commit comments

Comments
 (0)