package cn.axzo.msg.center.common.template;

import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.TypedValue;
import org.springframework.lang.NonNull;

import java.util.Map;

/**
 * @author yanglin
 */
class AbsentKeyMapAccessor extends MapAccessor {

    static final AbsentKeyMapAccessor INSTANCE = new AbsentKeyMapAccessor();

    @Override
    public boolean canRead(@NonNull EvaluationContext context, Object target, @NonNull String name) {
        return target instanceof Map;
    }

    @Override
    @NonNull
    public TypedValue read(@NonNull EvaluationContext context, Object target, @NonNull String name) {
        Map<?, ?> map = (Map<?, ?>) target;
        Object value = map.get(name);
        if (value == null)
            return TypedValue.NULL;
        return new TypedValue(value);
    }

    @Override
    public boolean canWrite(@NonNull EvaluationContext context, Object target, @NonNull String name) {
        return false;
    }
}
