-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
287 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
sugar_random_core/src/main/java/me/mikusugar/random/core/service/impl/RandomExpr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.