Skip to content

Commit

Permalink
add random exper
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuSugar committed Aug 11, 2021
1 parent 49a49b1 commit 7aab6be
Show file tree
Hide file tree
Showing 24 changed files with 287 additions and 167 deletions.
6 changes: 5 additions & 1 deletion docs/log.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# 更新日志

### 2021/08/09
### 2021/08/11

+ 添加表达式支持

### 2021/08/10
+ 添加随机生成浏览器代理

### 2021/08/09
Expand Down
6 changes: 6 additions & 0 deletions sugar_random_core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>

<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>5.2.6</version>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public class ServiceName {

public static final String USER_AGENT = "uagent";

public static final String EXPR = "expr";

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public NextEventServiceImpl(Map<String, AbstractRandomService> randomServiceMap)


@Override
public void check(String randomType, SugarJsonNode curNode, String randomInfo, String fieldName) throws Exception{
public void check(String randomType, SugarJsonNode curNode, String randomInfo, String fieldName) throws Exception {
if (!randomServiceMap
.get(randomType.trim())
.check(randomInfo.trim())) throw new Exception("输入有误!");
Expand All @@ -45,11 +45,11 @@ public void check(String randomType, SugarJsonNode curNode, String randomInfo, S
}

@Override
public void add(String name, String randomType, String randomInfo, SugarJsonNode curNode) throws Exception{
public void add(String name, String randomType, String randomInfo, SugarJsonNode curNode) throws Exception {
val node =
SugarJsonNode.builder()
.name(name.trim())
.type(randomServiceMap.get(randomType).getType())
.type(randomServiceMap.get(randomType).getType(randomInfo))
.randomServiceName(randomType)
.randomService(
randomServiceMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ public RandomCoreService<T> createRandomCoreService(String input){
* 获取数据类型
* @return SugarJsonNode.TYPE
*/
public abstract SugarJsonNode.TYPE getType();
public abstract SugarJsonNode.TYPE getType(String input);

/**
* 检查合法
* @param input 输入检查
* @return
*/
public abstract boolean check(String input);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public String helpText() {
return "随机生成国内城市,无需输入~ (北上广等概率大)";
}

@Override
public SugarJsonNode.TYPE getType() {
return SugarJsonNode.TYPE.STRING;
}
@Override
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.STRING;
}

@Override
@Override
public boolean check(String input) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String helpText() {
}

@Override
public SugarJsonNode.TYPE getType() {
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.ARRAY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public String helpText() {
}

@Override
public SugarJsonNode.TYPE getType() {
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.STRING;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@

@Service(ServiceName.RANDOM_CN_PHONE)
public class RandomCNPhone extends AbstractRandomService<String> {
@Override
protected RandomUtilInterface<String> createRandomUtilInterface(String input) {
return RandomCNPhone::getTel;
}

@Override
public String helpText() {
return "随机生成国内手机号,无需输入";
}

@Override
public SugarJsonNode.TYPE getType() {
return SugarJsonNode.TYPE.STRING;
}

@Override
public boolean check(String input) {
return true;
}

private static final String[] telFirst =
"134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

public static int getNum(int start, int end) {
return (int) (Math.random() * (end - start + 1) + start);
}

public static String getTel() {
int index = getNum(0, telFirst.length - 1);
String first = telFirst[index];
String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
return first + second + third;
}
@Override
protected RandomUtilInterface<String> createRandomUtilInterface(String input) {
return RandomCNPhone::getTel;
}

@Override
public String helpText() {
return "随机生成国内手机号,无需输入";
}

@Override
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.STRING;
}

@Override
public boolean check(String input) {
return true;
}

private static final String[] telFirst =
"134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

public static int getNum(int start, int end) {
return (int) (Math.random() * (end - start + 1) + start);
}

public static String getTel() {
int index = getNum(0, telFirst.length - 1);
String first = telFirst[index];
String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
return first + second + third;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@

@Service(ServiceName.RANDOM_EMAIL)
public class RandomEmail extends AbstractRandomService<String> {
@Override
protected RandomUtilInterface<String> createRandomUtilInterface(String input) {
return () -> getEmail(3,15);
}

@Override
public String helpText() {
return "返回随机生成的邮箱,无需输入";
}

@Override
public SugarJsonNode.TYPE getType() {
return SugarJsonNode.TYPE.STRING;
}

@Override
public boolean check(String input) {
return true;
}

private static final String[] email_suffix =
("@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.ne"
+ "t,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn")
.split(",");
public static String base = "abcdefghijklmnopqrstuvwxyz0123456789";

public static int getNum(int start, int end) {
return (int) (Math.random() * (end - start + 1) + start);
}

public static String getEmail(int lMin, int lMax) {
int length = getNum(lMin, lMax);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int number = (int) (Math.random() * base.length());
sb.append(base.charAt(number));
@Override
protected RandomUtilInterface<String> createRandomUtilInterface(String input) {
return () -> getEmail(3, 15);
}

@Override
public String helpText() {
return "返回随机生成的邮箱,无需输入";
}

@Override
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.STRING;
}

@Override
public boolean check(String input) {
return true;
}

private static final String[] email_suffix =
("@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.ne"
+ "t,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn")
.split(",");
public static String base = "abcdefghijklmnopqrstuvwxyz0123456789";

public static int getNum(int start, int end) {
return (int) (Math.random() * (end - start + 1) + start);
}

public static String getEmail(int lMin, int lMax) {
int length = getNum(lMin, lMax);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int number = (int) (Math.random() * base.length());
sb.append(base.charAt(number));
}
sb.append(email_suffix[(int) (Math.random() * email_suffix.length)]);
return sb.toString();
}
sb.append(email_suffix[(int) (Math.random() * email_suffix.length)]);
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.mikusugar.random.core.service.impl;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
import lombok.extern.slf4j.Slf4j;
import me.mikusugar.random.core.bean.SugarJsonNode;
import me.mikusugar.random.core.constant.ServiceName;
import me.mikusugar.random.core.service.AbstractRandomService;
import me.mikusugar.random.core.utils.GetAllService;
import me.mikusugar.random.core.utils.RandomUtilInterface;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* @author mikusugar
*/
@Service(ServiceName.EXPR)
@Slf4j
public class RandomExpr extends AbstractRandomService {

static Map<String, AbstractRandomService> map;

static {
try {
map = GetAllService.getAllService();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
log.error("SugarFunction 初始化失败");
}
}

//init
static {
AviatorEvaluator.addFunction(new SugarFunction());
}

static class SugarFunction extends AbstractFunction {

@Override
public String getName() {
return "sugar";
}

@Override
public AviatorObject call(Map<String, Object> env,
AviatorObject arg1, AviatorObject arg2) {
String type = FunctionUtils.getStringValue(arg1, env);
String input = FunctionUtils.getStringValue(arg2, env);
return new AviatorString(
map.get(type).createRandomCoreService(input).getRandomUtilInterface().next().toString());

}

}

@Override
protected RandomUtilInterface createRandomUtilInterface(String input) {
String in = input.substring(input.indexOf(",") + 1);
final Expression expression = AviatorEvaluator.compile(in);
return expression::execute;
}

@Override
public String helpText() {
return "Aviator 表达式,输入 type,expr 例如:STRING,{'hello SugarRandom'} 与 Aviator 官方相比 添加了 sugar 函数 " +
" sugar(random_name,input) ,参数为随机类型和输入。 Aviator 参考 https://www.yuque.com/boyan-avfmj/aviatorscript/cpow90 ";
}

@Override
public SugarJsonNode.TYPE getType(String input) {
String type = input.substring(0, input.indexOf(","));
return SugarJsonNode.TYPE.valueOf(type);
}

@Override
public boolean check(String input) {
try {
String type = input.substring(0, input.indexOf(",")).trim();
String in = input.substring(input.indexOf(",") + 1);
} catch (Exception e) {
e.printStackTrace();
log.error(input + e);
return false;
}
return true;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public String helpText() {
}

@Override
public SugarJsonNode.TYPE getType() {
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.INT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public String helpText() {
}

@Override
public SugarJsonNode.TYPE getType() {
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.INT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public String helpText() {
}

@Override
public SugarJsonNode.TYPE getType() {
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.STRING;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String helpText() {
}

@Override
public SugarJsonNode.TYPE getType() {
public SugarJsonNode.TYPE getType(String input) {
return SugarJsonNode.TYPE.STRING;
}

Expand Down
Loading

0 comments on commit 7aab6be

Please sign in to comment.