From 64254d5bde10e721612e5a7afe314b325bd57a41 Mon Sep 17 00:00:00 2001 From: sxl <1051783464@qq.com> Date: Tue, 27 Feb 2024 11:26:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20map=E8=BD=AC=E6=8D=A2=E5=80=BC=E6=97=B6?= =?UTF-8?q?=E5=8C=BA=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/anwen/mongo/convert/Converter.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java b/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java index 3dec645..a28dfc7 100644 --- a/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java +++ b/mongo-plus-core/src/main/java/com/anwen/mongo/convert/Converter.java @@ -1,6 +1,7 @@ package com.anwen.mongo.convert; import com.anwen.mongo.cache.global.PropertyCache; +import com.anwen.mongo.toolkit.InstantUtil; import com.anwen.mongo.toolkit.StringUtils; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCursor; @@ -8,6 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -18,11 +20,12 @@ public class Converter { /** * 将FindIterable转换为List>。 + * * @param iterable 待转换的FindIterable对象 - * @return java.util.List> 转换后的List>对象 + * @return java.util.List> 转换后的List>对象 * @author JiaChaoYang * @date 2023/6/29/029 - */ + */ public static List> convertDocumentToMap(FindIterable iterable) { List> resultList = new ArrayList<>(); try (MongoCursor cursor = iterable.iterator()) { @@ -41,24 +44,30 @@ public class Converter { return resultList; } - public static List> convertDocumentToMap(FindIterable iterable,Integer total) { + public static List> convertDocumentToMap(FindIterable iterable, Integer total) { List> resultList = new ArrayList<>(total); - for (Map map : iterable.batchSize(total)) { + for (Map map : iterable.batchSize(total)) { resultList.add(convertKeysToCamelCase(map)); } return resultList; } public static Map convertKeysToCamelCase(Map map) { - if (!PropertyCache.mapUnderscoreToCamelCase){ - return map; - } return map.entrySet().stream() .collect(Collectors.toMap( - entry -> StringUtils.convertToCamelCase(entry.getKey()), - Map.Entry::getValue) - ); + entry -> convertToCamelCaseIfNeeded(entry.getKey()), + entry -> convertValue(entry.getValue()) + )); } + private static String convertToCamelCaseIfNeeded(String key) { + return PropertyCache.mapUnderscoreToCamelCase ? StringUtils.convertToCamelCase(key) : key; + } + private static Object convertValue(Object value) { + if (value instanceof Date) { + return InstantUtil.convertTimestampToLocalDateTime(((Date) value).toInstant()); + } + return value; + } } -- Gitee