All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.giffing.bucket4j.spring.boot.starter.service.ExpressionService Maven / Gradle / Ivy

There is a newer version: 0.12.7
Show newest version
package com.giffing.bucket4j.spring.boot.starter.service;

import com.giffing.bucket4j.spring.boot.starter.context.ExpressionParams;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Map;

/**
 * The expression service wraps Springs {@link ExpressionParser} to execute SpEl expressions.
 */
@RequiredArgsConstructor
@Slf4j
public class ExpressionService {

    private final ExpressionParser expressionParser;

    private final ConfigurableBeanFactory beanFactory;

    public  String parseString(String expression, ExpressionParams params) {
        var context = getContext(params.getParams());
        var expr = expressionParser.parseExpression(expression);
        String result = expr.getValue(context, params.getRootObject(), String.class);
        log.info("parse-string-expression;result:{};expression:{};root:{};params:{}", result, expression, params.getRootObject(), params.getParams());
        return result;
    }

    public  Boolean parseBoolean(String expression, ExpressionParams params) {
        var context = getContext(params.getParams());
        var expr = expressionParser.parseExpression(expression);
        boolean result = Boolean.TRUE.equals(expr.getValue(context, params.getRootObject(), Boolean.class));
        log.info("parse-boolean-expression;result:{};expression:{};root:{};params:{}", result, expression, params.getRootObject(), params.getParams());
        return result;
    }

    private StandardEvaluationContext getContext(Map params) {
        var context = new StandardEvaluationContext();
        params.forEach(context::setVariable);
        context.setBeanResolver(new BeanFactoryResolver(beanFactory));
        return context;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy