Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add common func for mimeter #796

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jcommon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<module>match</module>
<module>infra-common</module>
<module>mockjs</module>
<module>sysFunc</module>
</modules>


Expand Down
25 changes: 25 additions & 0 deletions jcommon/sysFunc/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jcommon</artifactId>
<groupId>run.mone</groupId>
<version>1.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sysFunc</artifactId>

<dependencies>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

</dependencies>


</project>
10 changes: 10 additions & 0 deletions jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncConst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package run.mone.sysFunc;

public class SysFuncConst {

public static final String FUNC_NAME_SUBSTRING = "java.substring";

public static final String FUNC_NAME_UUID = "java.uuid";

public static final String FUNC_NAME_RANDOM_NUMBER = "java.randomNumber";
}
28 changes: 28 additions & 0 deletions jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package run.mone.sysFunc;

import static run.mone.sysFunc.SysFuncConst.*;

public enum SysFuncEnum {

//报警级别
SUBSTRING(FUNC_NAME_SUBSTRING, "common", "截取字符串", "${java.substring(ceshi, 1, 3)}",
"返回从指定开始索引到结束索引之间的子字符串,但不包括结束索引位置的字符"),
UUID(FUNC_NAME_UUID, "common", "唯一标识符", "{java.uuid()}",
"标准化的唯一标识符"),
RANDOM_NUMBER(FUNC_NAME_RANDOM_NUMBER, "common", "随机数", "${java.randomNumber(2, 11)}",
"生成指定范围的整型数字");

public String cname;
public String name;
public String type;
public String desc;
public String example;

SysFuncEnum(String name, String type, String cname, String example, String desc) {
this.name = name;
this.type = type;
this.cname = cname;
this.desc = desc;
this.example = example;
}
}
84 changes: 84 additions & 0 deletions jcommon/sysFunc/src/main/java/run/mone/sysFunc/SysFuncUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package run.mone.sysFunc;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

import static run.mone.sysFunc.SysFuncConst.*;

public class SysFuncUtils {

/**
* eg. ${java.substring("ceshi", 1, 3)}
*
* @param funcDesc
* @return
*/
public static String gen(String funcDesc) {
if (StringUtils.isEmpty(funcDesc)
|| !funcDesc.startsWith("${")
|| !funcDesc.endsWith("}")) {
//不做任何处理,直接返回
return funcDesc;
}

String func = funcDesc.substring(2, funcDesc.length() - 1);
int index = func.indexOf("(");
String funcName = func.substring(0, index);
String funcParams = func.substring(index + 1, func.length() - 1);
String[] funcParamArr = funcParams.split(",");
List<String> funcParamList = Arrays.asList(funcParamArr).stream().map(it -> it.trim()).collect(Collectors.toList());

switch (funcName) {
case FUNC_NAME_SUBSTRING: {
return subString(funcParamList, funcParams);
}
case FUNC_NAME_UUID: {
return uuid();
}
case FUNC_NAME_RANDOM_NUMBER: {
return randomNumber(funcParamList, funcParams);
}
default:
return funcDesc;
}
}

private static String subString(List<String> funcParamList, String defaultStr) {

//substring(int beginIndex)
if (funcParamList.size() == 2) {
return funcParamList.get(0).substring(Integer.valueOf(funcParamList.get(1)));
}

//substring(int beginIndex, int endIndex)
if (funcParamList.size() == 3) {
return funcParamList.get(0).substring(Integer.valueOf(funcParamList.get(1)), Integer.valueOf(funcParamList.get(2)));
}

return defaultStr;
}

private static String uuid() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
}

private static String randomNumber(List<String> funcParamList, String defaultStr) {
if (funcParamList.size() == 2) {
int randomNumberInRange = ThreadLocalRandom.current().nextInt(Integer.valueOf(funcParamList.get(0)), Integer.valueOf(funcParamList.get(1)));
return String.valueOf(randomNumberInRange);
}

if (funcParamList.size() == 1) {
int randomNumberInRange = ThreadLocalRandom.current().nextInt(Integer.valueOf(funcParamList.get(0)));
return String.valueOf(randomNumberInRange);
}

return defaultStr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package run.mone.sysFunc.test;

import org.junit.Ignore;
import org.junit.Test;
import run.mone.sysFunc.SysFuncUtils;

@Ignore
public class SysFuncTest {

@Test
public void testSubstring() {
String res = SysFuncUtils.gen("${java.substring(ceshi, 1, 3)}");
System.out.println(res);
}

@Test
public void testUuid() {
String res = SysFuncUtils.gen("${java.uuid()}");
System.out.println(res);
}

@Test
public void testRandomNumber() {
String res = SysFuncUtils.gen("${java.randomNumber(2,11)}");
System.out.println(res);
}



}
Loading