kvToMap(String key, String value, ConfigValueTypeEnum valueTypeEnum) {
+ if (isEmpty(key)) {
+ return null;
+ }
+ return cacheCompute("kvToMap", key, value, () -> propertiesToMap(kvToProperties(key, value, valueTypeEnum)));
+ }
+
+ public String kvToProperties(String key, String value, ConfigValueTypeEnum valueTypeEnum) {
+ return cacheCompute("kvToProperties", key, value, () -> kvToProperties(key, value, null, valueTypeEnum));
+ }
+
+ /**
+ * k-v的String类型转properties
+ *
+ * 其中key可能是a.b.c这种,而value可能是各种各样的类型,我们这里通过valueType进行区分
+ *
+ * @param key 主键
+ * @param value 待转换的值
+ * @param desc 注释
+ * @param valueTypeEnum 值的类型,0:yaml,1:properties,2:json,3:string
+ * @return 转换之后的yaml类型
+ * @throws RuntimeException 转换异常
+ */
+ public String kvToProperties(String key, String value, String desc, ConfigValueTypeEnum valueTypeEnum) {
+ if (isEmpty(key)) {
+ return null;
+ }
+ return cacheCompute("kvToProperties", key, value, () -> {
+ try {
+ // 将value对应的值先转换为properties类型,然后对key进行拼接,最后再统一转化为yaml格式
+ StringBuilder propertiesResult = new StringBuilder();
+ if (null != desc && !"".equals(desc)) {
+ propertiesResult.append("# ").append(desc).append("\n");
+ }
+
+ String propertiesContent;
+ switch (valueTypeEnum) {
+ case YAML:
+ propertiesContent = yamlToProperties(key, value);
+ if (!isEmpty(propertiesContent)) {
+ propertiesResult.append(propertiesContent);
+ }
+ return propertiesResult.toString();
+ case JSON:
+ propertiesContent = yamlToProperties(key, jsonToYaml(value));
+ if (!isEmpty(propertiesContent)) {
+ propertiesResult.append(propertiesContent);
+ }
+ return propertiesResult.toString();
+ case PROPERTIES:
+ propertiesContent = propertiesAppendPrefixKey(key, value);
+ if (!isEmpty(propertiesContent)) {
+ propertiesResult.append(propertiesContent);
+ }
+ return propertiesResult.toString();
+ case STRING:
+ propertiesResult.append(key).append("=").append(appendSpaceForArrayValue(value));
+ return propertiesResult.toString();
+ default:
+ break;
+ }
+
+ return propertiesResult.toString();
+ } catch (Throwable e) {
+ throw new RuntimeException("kv 转换到 properties 异常: " + e.getMessage());
+ }
+ });
+ }
+
+ public List getPropertiesItemLineList(String content) {
+ if (isEmpty(content)) {
+ return Collections.emptyList();
+ }
+ return cacheCompute("getPropertiesItemLineList", content, () -> {
+ if (!content.contains("=")) {
+ return Collections.emptyList();
+ }
+
+ String[] lineList = content.split(NEW_LINE);
+ List itemLineList = new ArrayList<>();
+ StringBuilder stringBuilder = new StringBuilder();
+ for (String line : lineList) {
+ // 处理多行数据
+ if (line.endsWith("\\")) {
+ stringBuilder.append(line).append("\n");
+ } else {
+ stringBuilder.append(line);
+ itemLineList.add(stringBuilder.toString());
+ stringBuilder.delete(0, stringBuilder.length());
+ }
+ }
+ return itemLineList;
+ });
+ }
+
+ private String propertiesAppendPrefixKey(String key, String propertiesContent) {
+ return getPropertiesItemLineList(propertiesContent).stream().filter(e -> e.contains("=")).map(e -> {
+ int index = e.indexOf("=");
+ if (index > -1) {
+ String keyTem = e.substring(0, index).trim();
+ String valueTem = e.substring(index + 1).trim();
+ return key + DOT + keyTem + "=" + valueTem;
+ }
+ return null;
+ }).filter(Objects::nonNull).reduce((a, b) -> a + NEW_LINE + b).orElse(null);
+ }
+
+ /**
+ * 针对有些yaml格式不严格,这里做不严格向严格的eo-yaml解析的转换
+ *
+ * 对{@code
+ * test:
+ * - k1: 12
+ * - k2: 22
+ * }
+ * 这种做一层缩进,由于snake的map转yaml后有缩进问题
+ */
+ private String yamlFormatForMap(String content) {
+ if (isEmpty(content)) {
+ return null;
+ }
+ return cacheCompute("yamlFormatForMap", content, () -> {
+ if (!content.contains(":") && !content.contains("-")) {
+ return null;
+ }
+
+ StringBuilder stringBuilder = new StringBuilder();
+ String[] items = content.split("\n");
+ Integer blankSize = null;
+ // 判断是否在数组中
+ boolean inArray = false;
+ for (String item : items) {
+ int innerBlankSize = item.substring(0, item.indexOf(item.trim())).length();
+ // 数组
+ if (item.trim().startsWith("- ")) {
+ if (inArray) {
+ // 多重数组,则多层嵌套
+ if (innerBlankSize > blankSize) {
+ stringBuilder.append(INDENT_BLANKS).append(INDENT_BLANKS).append(item).append("\n");
+ continue;
+ }
+ }
+ inArray = true;
+ blankSize = innerBlankSize;
+ } else {
+ // 其他的字符
+ if (null != blankSize) {
+ if (innerBlankSize <= blankSize) {
+ inArray = false;
+ }
+ }
+ }
+
+ if (inArray) {
+ stringBuilder.append(INDENT_BLANKS).append(item).append("\n");
+ } else {
+ stringBuilder.append(item).append("\n");
+ }
+ }
+ return stringBuilder.toString();
+ });
+ }
+
+ /**
+ * 将key转换为yaml节点
+ *
+ * @param lineWordList 待转换的key,比如{@code k1.k2.k3=123}
+ * @param nodeList 已经保存的节点数据
+ * @param lastNodeArrayFlag 上一个节点是否数组类型
+ * @param index 索引下标
+ * @param value 解析的值
+ * @param remark 当前value对应的注释
+ */
+ private void wordToNode(List lineWordList, List nodeList, YamlNode parentNode, Boolean lastNodeArrayFlag, Integer index, String value, String projectRemark,
+ String remark) {
+ if (lineWordList.isEmpty()) {
+ if (lastNodeArrayFlag) {
+ YamlNode node = new YamlNode();
+ node.setValue(value);
+ node.setRemark(remark);
+ nodeList.add(node);
+ }
+ } else {
+ String nodeName = lineWordList.get(0);
+
+ Pair nameAndIndex = peelArray(nodeName);
+ nodeName = nameAndIndex.getKey();
+ Integer nextIndex = nameAndIndex.getValue();
+
+ YamlNode node = new YamlNode();
+ node.setName(nodeName);
+ node.setProjectRemark(projectRemark);
+ node.setParent(parentNode);
+ node.setRemark(remark);
+ node.setLastNodeIndex(index);
+ lineWordList.remove(0);
+
+ //如果节点下面的子节点数量为0,则为终端节点,也就是赋值节点
+ if (lineWordList.size() == 0) {
+ if (null == nextIndex) {
+ node.setRemark(remark);
+ node.setValue(value);
+ }
+ }
+
+ // nextIndex 不空,表示当前节点为数组,则之后的数据为他的节点数据
+ if (null != nextIndex) {
+ node.setArrayFlag(true);
+ boolean hasEqualsName = false;
+ //遍历查询节点是否存在
+ for (YamlNode YamlNode : nodeList) {
+ //如果节点名称已存在,则递归添加剩下的数据节点
+ if (nodeName.equals(YamlNode.getName()) && YamlNode.getArrayFlag()) {
+ Integer yamlNodeIndex = YamlNode.getLastNodeIndex();
+ if (null == yamlNodeIndex || index.equals(yamlNodeIndex)) {
+ hasEqualsName = true;
+ wordToNode(lineWordList, YamlNode.getValueList(), node.getParent(), true, nextIndex, appendSpaceForArrayValue(value), null, remark);
+ }
+ }
+ }
+ //如果遍历结果为节点名称不存在,则递归添加剩下的数据节点,并把新节点添加到上级yamlTree的子节点中
+ if (!hasEqualsName) {
+ wordToNode(lineWordList, node.getValueList(), node.getParent(), true, nextIndex, appendSpaceForArrayValue(value), null, remark);
+ nodeList.add(node);
+ }
+ } else {
+ boolean hasEqualsName = false;
+ //遍历查询节点是否存在
+ for (YamlNode YamlNode : nodeList) {
+ if (!lastNodeArrayFlag) {
+ //如果节点名称已存在,则递归添加剩下的数据节点
+ if (nodeName.equals(YamlNode.getName())) {
+ hasEqualsName = true;
+ wordToNode(lineWordList, YamlNode.getChildren(), YamlNode, false, nextIndex, appendSpaceForArrayValue(value), null, remark);
+ }
+ } else {
+ //如果节点名称已存在,则递归添加剩下的数据节点
+ if (nodeName.equals(YamlNode.getName())) {
+ Integer yamlNodeIndex = YamlNode.getLastNodeIndex();
+ if (null == yamlNodeIndex || index.equals(yamlNodeIndex)) {
+ hasEqualsName = true;
+ wordToNode(lineWordList, YamlNode.getChildren(), YamlNode, true, nextIndex, appendSpaceForArrayValue(value), null, remark);
+ }
+ }
+ }
+ }
+ //如果遍历结果为节点名称不存在,则递归添加剩下的数据节点,并把新节点添加到上级yamlTree的子节点中
+ if (!hasEqualsName) {
+ wordToNode(lineWordList, node.getChildren(), node, false, nextIndex, appendSpaceForArrayValue(value), null, remark);
+ nodeList.add(node);
+ }
+ }
+ }
+ }
+
+ /**
+ * 获取yaml中的注释
+ *
+ * @param remarkMap 解析后填充的注释map:key为a.b.c.d,value为对应的注释,去除掉前缀#后的数据
+ * @param mapping yaml解析后数据
+ * @param prefix 前缀
+ */
+ private void yamlToRemarkMap(Map remarkMap, YamlMapping mapping, String prefix) {
+ if (null == mapping) {
+ return;
+ }
+ for (com.amihaiemil.eoyaml.YamlNode node : mapping.keys()) {
+ String nodeName = node.asScalar().value();
+ String remark = mapping.value(node).comment().value();
+
+ if (null != remark && !"".equals(remark)) {
+ remarkMap.put(wrapKey(prefix, nodeName), remark);
+ }
+
+ yamlToRemarkMap(remarkMap, mapping.yamlMapping(node), wrapKey(prefix, nodeName));
+ }
+ }
+
+ private String wrapKey(String prefix, String value) {
+ if (isEmpty(prefix)) {
+ return prefix + "." + value;
+ }
+ return value;
+ }
+
+ /**
+ * 解析节点名字,为数组则返回数组名和节点下标
+ *
+ * name.test[0] 将test和0进行返回
+ *
+ * @param nodeName 界面的名字
+ * @return 如果是数组,则将数组名和解析后的下标返回
+ */
+ private Pair peelArray(String nodeName) {
+ String name = nodeName;
+ Integer index = null;
+ Matcher matcher = rangePattern.matcher(nodeName);
+ if (matcher.find()) {
+ String indexStr = matcher.group(2);
+ if (null != indexStr) {
+ index = Integer.valueOf(indexStr);
+ }
+ name = matcher.group(1);
+ }
+
+ return new Pair<>(name, index);
+ }
+
+ /**
+ * 将yaml对应的这种value进行添加前缀空格,其中value为key1对应的value
+ * {@code
+ * test:
+ * key1: |
+ * value1
+ * value2
+ * value3
+ * }
+ * 对应的值
+ * {@code
+ * |
+ * value1
+ * value2
+ * value3
+ * }
+ *
+ * @param value 待转换的值比如{@code
+ * test:
+ * key1: |
+ * value1
+ * value2
+ * value3
+ * }
+ * @return 添加前缀空格之后的处理
+ * {@code
+ * |
+ * value1
+ * value2
+ * value3
+ * }
+ */
+ private String appendSpaceForArrayValue(String value) {
+ if (!value.startsWith(yaml_NEW_LINE_DOM)) {
+ return value;
+ }
+ String valueTem = value.substring(yaml_NEW_LINE_DOM.length());
+ return yaml_NEW_LINE_DOM + Arrays.stream(valueTem.split("\\n")).map(e -> {
+ String tem = e;
+ if (e.endsWith("\\")) {
+ tem = e.substring(0, e.length() - 1);
+ }
+ return INDENT_BLANKS + tem;
+ }).reduce((a, b) -> a + "\n" + b).orElse(valueTem);
+ }
+
+ private void formatPropertiesToYaml(List yamlLineList, List YamlNodes, Boolean lastNodeArrayFlag, String blanks) {
+ Integer beforeNodeIndex = null;
+ String equalSign;
+ for (YamlNode YamlNode : YamlNodes) {
+ String value = YamlNode.getValue();
+ String remark = YamlNode.getRemark();
+
+ equalSign = SIGN_SEMICOLON;
+ if (null == value || "".equals(value)) {
+ value = "";
+ } else {
+ equalSign = SIGN_SEMICOLON + " ";
+ }
+ YamlNode.resortValue();
+
+ String name = YamlNode.getName();
+ if (lastNodeArrayFlag) {
+ if (null == name) {
+ yamlLineList.add(blanks + ARRAY_BLANKS + stringValueWrap(value));
+ } else {
+ if (null != beforeNodeIndex && beforeNodeIndex.equals(YamlNode.getLastNodeIndex())) {
+ yamlLineList.add(blanks + INDENT_BLANKS + name + equalSign + stringValueWrap(value));
+ } else {
+ yamlLineList.add(blanks + ARRAY_BLANKS + name + equalSign + stringValueWrap(value));
+ }
+ }
+ beforeNodeIndex = YamlNode.getLastNodeIndex();
+ } else {
+ // 父节点为空,表示,当前为顶层
+ if (null == YamlNode.getParent()) {
+ String remarkTem = getRemarkProject(YamlNode.getProjectRemark());
+ if (!"".equals(remarkTem)) {
+ yamlLineList.add(blanks + getRemarkProject(YamlNode.getProjectRemark()));
+ }
+ }
+
+ // 自己节点为数组,则添加对应的注释
+ if (YamlNode.getArrayFlag()) {
+ if (null != remark && !"".equals(remark)) {
+ yamlLineList.add(blanks + remark);
+ }
+ }
+ yamlLineList.add(blanks + name + equalSign + stringValueWrap(value, remark));
+ }
+
+ if (YamlNode.getArrayFlag()) {
+ if (lastNodeArrayFlag) {
+ formatPropertiesToYaml(yamlLineList, YamlNode.getValueList(), true, INDENT_BLANKS + INDENT_BLANKS + blanks);
+ } else {
+ formatPropertiesToYaml(yamlLineList, YamlNode.getValueList(), true, INDENT_BLANKS + blanks);
+ }
+ } else {
+ if (lastNodeArrayFlag) {
+ formatPropertiesToYaml(yamlLineList, YamlNode.getChildren(), false, INDENT_BLANKS + INDENT_BLANKS + blanks);
+ } else {
+ formatPropertiesToYaml(yamlLineList, YamlNode.getChildren(), false, INDENT_BLANKS + blanks);
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private void formatYamlToPropertiesValue(Properties properties, Map remarkMap, Object object, String prefix) {
+ if (null == object) {
+ return;
+ }
+ if (object instanceof Map) {
+ Map map = (Map) object;
+ Set> set = map.keySet();
+ for (Object key : set) {
+ Object value = map.get(key);
+ if (null == value) {
+ value = "";
+ }
+ if (value instanceof Map) {
+ formatYamlToPropertiesValue(properties, remarkMap, value, prefixWithDOT(prefix) + key);
+ } else if (value instanceof Collection) {
+ Collection collection = (Collection) value;
+ if (!collection.isEmpty()) {
+ Iterator> iterator = collection.iterator();
+ int index = 0;
+ while (iterator.hasNext()) {
+ Object valueObject = iterator.next();
+ formatYamlToPropertiesValue(properties, remarkMap, valueObject, prefixWithDOT(prefix) + key + "[" + index + "]");
+ index = index + 1;
+ }
+ }
+ } else if (value instanceof String) {
+ String valueStr = (String) value;
+ valueStr = valueStr.trim();
+ valueStr = valueStr.replace("\n", "\\\n");
+ properties.put(prefixWithDOT(prefix) + key, valueStr);
+ } else {
+ properties.put(prefixWithDOT(prefix) + key, value);
+ }
+ }
+ } else if (object instanceof Collection) {
+ Collection collection = (Collection) object;
+ if (!collection.isEmpty()) {
+ Iterator> iterator = collection.iterator();
+ int index = 0;
+ while (iterator.hasNext()) {
+ Object valueObject = iterator.next();
+ formatYamlToPropertiesValue(properties, remarkMap, valueObject, prefix + "[" + index + "]");
+ index = index + 1;
+ }
+ }
+ } else if (object.getClass().isArray()) {
+ Object[] array = (Object[]) object;
+ for (int index = 0; index < array.length; index++) {
+ formatYamlToPropertiesValue(properties, remarkMap, array[index], prefix + "[" + index + "]");
+ }
+ } else if (object instanceof String) {
+ String valueObject = (String) object;
+ valueObject = valueObject.replace("\n", "\\\n");
+ properties.put(prefix, valueObject);
+ } else {
+ properties.put(prefix, object);
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private void formatYamlToProperties(List propertiesLineList, Map remarkMap, Object object, String prefix) {
+ if (null == object) {
+ return;
+ }
+ if (object instanceof Map) {
+ // 填充注释
+ if (remarkMap.containsKey(prefix)) {
+ propertiesLineList.add(REMARK_PRE + remarkMap.get(prefix));
+ }
+
+ Map map = (Map) object;
+ Set> set = map.keySet();
+ for (Object key : set) {
+ Object value = map.get(key);
+ if (null == value) {
+ value = "";
+ }
+ if (value instanceof Map) {
+ formatYamlToProperties(propertiesLineList, remarkMap, value, prefixWithDOT(prefix) + key);
+ } else if (value instanceof Collection) {
+ Collection collection = (Collection) value;
+ if (!collection.isEmpty()) {
+ // 填充注释
+ if (remarkMap.containsKey(prefixWithDOT(prefix) + key)) {
+ propertiesLineList.add(REMARK_PRE + remarkMap.get(prefixWithDOT(prefix) + key));
+ }
+
+ Iterator> iterator = collection.iterator();
+ int index = 0;
+ while (iterator.hasNext()) {
+ Object valueObject = iterator.next();
+ formatYamlToProperties(propertiesLineList, remarkMap, valueObject, prefixWithDOT(prefix) + key + "[" + index + "]");
+ index = index + 1;
+ }
+ }
+ } else if (value instanceof String) {
+ String valueStr = (String) value;
+ valueStr = valueStr.trim();
+ valueStr = valueStr.replace("\n", "\\\n");
+ // 填充注释
+ if (remarkMap.containsKey(prefixWithDOT(prefix) + key)) {
+ propertiesLineList.add(REMARK_PRE + remarkMap.get(prefixWithDOT(prefix) + key));
+ }
+
+ propertiesLineList.add(prefixWithDOT(prefix) + key + SIGN_EQUAL + valueStr);
+ } else {
+ // 填充注释
+ if (remarkMap.containsKey(prefixWithDOT(prefix) + key)) {
+ propertiesLineList.add(REMARK_PRE + remarkMap.get(prefixWithDOT(prefix) + key));
+ }
+
+ propertiesLineList.add(prefixWithDOT(prefix) + key + SIGN_EQUAL + value);
+ }
+ }
+ } else if (object instanceof Collection) {
+ Collection collection = (Collection) object;
+ if (!collection.isEmpty()) {
+ // 填充注释
+ if (remarkMap.containsKey(prefix)) {
+ propertiesLineList.add(REMARK_PRE + remarkMap.get(prefix));
+ }
+
+ Iterator> iterator = collection.iterator();
+ int index = 0;
+ while (iterator.hasNext()) {
+ Object valueObject = iterator.next();
+ formatYamlToProperties(propertiesLineList, remarkMap, valueObject, prefix + "[" + index + "]");
+ index = index + 1;
+ }
+ }
+ } else if (object.getClass().isArray()) {
+ Object[] array = (Object[]) object;
+ for (int index = 0; index < array.length; index++) {
+ formatYamlToProperties(propertiesLineList, remarkMap, array[index], prefix + "[" + index + "]");
+ }
+ } else if (object instanceof String) {
+ String valueObject = (String) object;
+ valueObject = valueObject.replace("\n", "\\\n");
+ // 填充注释
+ if (remarkMap.containsKey(prefix)) {
+ propertiesLineList.add(REMARK_PRE + remarkMap.get(prefix));
+ }
+
+ propertiesLineList.add(prefix + SIGN_EQUAL + valueObject);
+ } else {
+ // 填充注释
+ if (remarkMap.containsKey(prefix)) {
+ propertiesLineList.add(REMARK_PRE + remarkMap.get(prefix));
+ }
+
+ propertiesLineList.add(prefix + SIGN_EQUAL + object);
+ }
+ }
+
+ private String prefixWithDOT(String prefix) {
+ if ("".equals(prefix)) {
+ return prefix;
+ }
+ return prefix + DOT;
+ }
+
+ private String stringValueWrap(String value) {
+ if (isEmpty(value)) {
+ return "";
+ }
+ // 对数组的数据进行特殊处理
+ if (value.startsWith("[") && value.endsWith("]")) {
+ return "'" + value + "'";
+ }
+ return value;
+ }
+
+ private String stringValueWrap(String value, String remark) {
+ if (isEmpty(value)) {
+ return "";
+ }
+ // 对数组的数据进行特殊处理
+ if (value.startsWith("[") && value.endsWith("]")) {
+ return "'" + value + "'" + getRemark(remark);
+ }
+
+ return value + getRemark(remark);
+ }
+
+ private String getRemark(String remark) {
+ if (null != remark && !"".endsWith(remark) && remark.startsWith("#")) {
+ return " # " + remark.substring(1).trim();
+ } else {
+ return "";
+ }
+ }
+
+ private String getRemarkProject(String remark) {
+ if (null != remark && !"".endsWith(remark) && remark.startsWith("#")) {
+ return " # " + remark.substring(1).trim();
+ } else {
+ return "";
+ }
+ }
+
+ /**
+ * 数据存在则返回,不存在则计算后添加到缓存中
+ */
+ @SuppressWarnings("unchecked")
+ private T cacheCompute(String funName, Object key, Supplier biFunction) {
+ String cacheKey = buildCacheKey(funName, key);
+ if (typeContentMap.containsKey(cacheKey)) {
+ return (T) typeContentMap.get(cacheKey);
+ }
+ T result = biFunction.get();
+ if (null != result) {
+ typeContentMap.put(cacheKey, result);
+ }
+ return result;
+ }
+
+ @SuppressWarnings("unchecked")
+ private T cacheCompute(String funName, Object key, Object value, Supplier biFunction) {
+ String cacheKey = buildCacheKey(funName, key, value);
+ if (typeContentMap.containsKey(cacheKey)) {
+ return (T) typeContentMap.get(cacheKey);
+ }
+ T result = biFunction.get();
+ if (null != result) {
+ typeContentMap.put(cacheKey, result);
+ }
+ return result;
+ }
+
+ private String buildCacheKey(String funName, Object... parameters) {
+ StringBuilder stringBuilder = new StringBuilder(funName);
+ for (Object parameter : parameters) {
+ if (null != parameter) {
+ stringBuilder.append(":").append(parameter.toString());
+ }
+ }
+ return stringBuilder.toString();
+ }
+
+ private boolean isEmpty(String string) {
+ return null == string || "".endsWith(string);
+ }
+
+ private boolean isEmpty(Collection> collection) {
+ return null == collection || collection.isEmpty();
+ }
+
+ private boolean isEmpty(Map, ?> map) {
+ return null == map || map.isEmpty();
+ }
+
+ @Data
+ class YamlNode {
+
+ private YamlNode parent;
+ /**
+ * 只有parent为null时候,该值才可能有值
+ */
+ private String projectRemark;
+ /**
+ * name
+ */
+ private String name;
+ /**
+ * value
+ */
+ private String value;
+ /**
+ * 注释
+ */
+ private String remark;
+
+ /**
+ * 子节点
+ */
+ private List children = new ArrayList<>();
+
+ /**
+ * 数组标示:
+ */
+ private Boolean arrayFlag = false;
+ /**
+ * 存储的数组中的前一个节点的下标
+ */
+ private Integer lastNodeIndex;
+ /**
+ * 只有数组标示为true,下面的value才有值
+ */
+ private List valueList = new ArrayList<>();
+
+ /**
+ * 将其中的value按照index下标顺序进行重拍
+ */
+ public void resortValue() {
+ if (!arrayFlag || valueList.isEmpty()) {
+ return;
+ }
+
+ // 升序
+ valueList.sort((a, b) -> {
+ if (null == a.getLastNodeIndex() || null == b.getLastNodeIndex()) {
+ return 0;
+ }
+
+ return a.getLastNodeIndex() - b.getLastNodeIndex();
+ });
+
+ // 是数组的节点也循环下
+ valueList.forEach(YamlNode::resortValue);
+ }
+ }
+}