You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@linkis.apache.org by GitBox <gi...@apache.org> on 2022/07/06 03:57:34 UTC

[GitHub] [incubator-linkis] peacewong commented on a diff in pull request #2415: support Variable Operation

peacewong commented on code in PR #2415:
URL: https://github.com/apache/incubator-linkis/pull/2415#discussion_r914388428


##########
linkis-commons/linkis-common/src/main/java/org/apache/linkis/common/utils/VariableOperationUtils.java:
##########
@@ -0,0 +1,292 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.linkis.common.utils;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.commons.lang3.StringUtils;
+
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ *
+ * support variable operation
+ * ${yyyyMMdd%-1d}/${yyyy-MM-01%-2M}
+ * Date: 2021/5/7 11:10
+ */
+public class VariableOperationUtils {
+
+    private static final String DOLLAR = "$";
+    private static final String PLACEHOLDER_SPLIT = "%";
+    private static final String PLACEHOLDER_LEFT = "{";
+    private static final String PLACEHOLDER_RIGHT = "}";
+    private static final String CYCLE_YEAR = "y";
+    private static final String CYCLE_MONTH = "M";
+    private static final String CYCLE_DAY = "d";
+    private static final String CYCLE_HOUR = "H";
+    private static final String CYCLE_MINUTE = "m";
+    private static final String CYCLE_SECOND = "s";
+    private static final String[] CYCLES  = new String[]{CYCLE_YEAR, CYCLE_MONTH, CYCLE_DAY, CYCLE_HOUR, CYCLE_MINUTE, CYCLE_SECOND};
+
+    /**
+     * yyyy-MM-dd HH:mm:ss
+     * @param date
+     * @return
+     */
+    public static ZonedDateTime toZonedDateTime(Date date) {
+        Instant instant = date.toInstant();
+        ZoneId zoneId = ZoneId.systemDefault();
+        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
+        return ZonedDateTime.of(localDateTime,zoneId);
+    }
+
+    /**
+     * json support variable operation
+     * @param dateTime
+     * @param str
+     * @return
+     */
+    public static String replaces(ZonedDateTime dateTime,String str){
+        return replaces(dateTime,str,true);
+    }
+
+
+    /**
+     * json support variable operation
+     * @param dateTime
+     * @param str
+     * @param format
+     * @return
+     */
+    public static String replaces(ZonedDateTime dateTime,String str,boolean format){
+        try {
+            JsonNode rootNode = JsonUtils.jackson().readTree(str);
+            if (rootNode.isArray() || rootNode.isObject()){
+                replaceJson(dateTime,rootNode);
+                return rootNode.toString();
+            }
+        }catch (Exception e){}
+        return replace(dateTime,str);
+    }
+
+    /**
+     *
+     * @param dateTime
+     * @param str
+     * @return
+     */
+    private static String replace(ZonedDateTime dateTime, String str){
+        StringBuilder buffer = new StringBuilder(str);
+        int startIndex = str.indexOf(PLACEHOLDER_LEFT);
+
+        while (startIndex != -1) {
+            int endIndex = buffer.indexOf(PLACEHOLDER_RIGHT, startIndex);
+            if (endIndex != -1) {
+                String placeHolder = buffer.substring(startIndex, endIndex + 1);
+                String content = placeHolder.replace(PLACEHOLDER_LEFT, "").replace(PLACEHOLDER_RIGHT, "").trim();
+                String[] parts = content.split(PLACEHOLDER_SPLIT);
+                try{
+                    ZonedDateTime ndt = dateTime;
+                    for(int i = 1;i< parts.length;i++){
+                        ndt = changeDateTime(ndt,parts[i]);
+                    }
+
+                    String newContent = ndt.format(DateTimeFormatter.ofPattern(parts[0]));
+                    if (buffer.substring(startIndex -1 ,endIndex + 1).contains(DOLLAR)){
+                        buffer.replace(startIndex - 1, endIndex + 1, newContent);
+                    }else {
+                        buffer.replace(startIndex, endIndex + 1, newContent);
+                    }
+                    startIndex = buffer.indexOf(PLACEHOLDER_LEFT, startIndex + newContent.length());
+                }catch (IllegalArgumentException e1){
+                    startIndex = buffer.indexOf(PLACEHOLDER_LEFT, endIndex);
+
+                } catch (Exception e2){
+                    throw new RuntimeException(e2);

Review Comment:
   Linkis exception should be thrown, you can refer to https://linkis.apache.org/zh-CN/community/development_specification/exception_catch



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org