Skip to content

Commit

Permalink
feat: 增加aviator多个FunctionLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Dec 6, 2024
1 parent d241119 commit b349f3d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2021-2024 spring-boot-extension the original author or authors.
* Licensed 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
* https://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 com.livk.commons.expression.aviator;

import com.google.common.collect.Lists;
import com.googlecode.aviator.FunctionLoader;
import com.googlecode.aviator.runtime.type.AviatorFunction;

import java.util.ArrayList;
import java.util.List;

/**
* @author livk
*/
public class PrioritizedFunctionLoader implements FunctionLoader {

private final List<FunctionLoader> loaders;

public PrioritizedFunctionLoader() {
this(new ArrayList<>());
}

public PrioritizedFunctionLoader(List<FunctionLoader> loaders) {
this.loaders = loaders;
}

public PrioritizedFunctionLoader(FunctionLoader... loaders) {
this(Lists.newArrayList(loaders));
}

@Override
public AviatorFunction onFunctionNotFound(String name) {
for (FunctionLoader loader : loaders) {
AviatorFunction function = loader.onFunctionNotFound(name);
if (function != null) {
return function;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2021-2024 spring-boot-extension the original author or authors.
* Licensed 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
* https://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 com.livk.commons.expression.aviator;

import com.googlecode.aviator.FunctionLoader;
import com.googlecode.aviator.runtime.type.AviatorFunction;

import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

/**
* <p>
* ServiceFunctionLoader
* </p>
*
* @author livk
*/
public class ServiceFunctionLoader implements FunctionLoader {

private final Map<String, AviatorFunction> functions = new ConcurrentHashMap<>();

public ServiceFunctionLoader() {
load();
}

@Override
public AviatorFunction onFunctionNotFound(String name) {
return functions.get(name);
}

public void load() {
for (AviatorFunction function : ServiceLoader.load(AviatorFunction.class)) {
this.functions.put(function.getName(), function);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class SpringContextFunctionLoader implements FunctionLoader, Initializing
@Override
public AviatorFunction onFunctionNotFound(String name) {
try {
for (AviatorFunction function : this.applicationContext.getBeanProvider(AviatorFunction.class)) {
if (name.equals(function.getName())) {
return function;
}
}
return this.applicationContext.getBean(name, AviatorFunction.class);
}
catch (NoSuchBeanDefinitionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ void evaluate() {
assertEquals("livk:" + System.getProperty("user.dir"),
resolver.evaluate("#username+':'+System.getProperty(\"user.dir\")", map));

assertEquals("livk123", resolver.evaluate("springStrAdd(x,y)", Map.of("x", "livk", "y", "123")));
assertEquals("livk123", resolver.evaluate("add(x,y)", Map.of("x", "livk", "y", "123")));

assertEquals(4, resolver.evaluate("ifElse(a==c,b,d)", Map.of("a", 1, "b", 2, "c", 3, "d", 4), Integer.class));
}

@TestConfiguration
Expand All @@ -97,6 +99,25 @@ public SpringContextFunctionLoader springContextFunctionLoader(ApplicationContex
return new SpringContextFunctionLoader(applicationContext);
}

@Bean
public IfElseFunction ifElse() {
return new IfElseFunction();
}

}

static class IfElseFunction extends AbstractFunction {

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

@Override
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
return FunctionUtils.getBooleanValue(arg1, env) ? arg2 : arg3;
}

}

static class AddFunction extends AbstractFunction {
Expand Down

0 comments on commit b349f3d

Please sign in to comment.