package com.ruoyi.common.core.accessor;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;

import java.util.Map;

public class MapAccessor implements PropertyAccessor {
    @Override
    public Class<?>[] getSpecificTargetClasses() {
        return new Class[]{Map.class};
    }

    @Override
    public boolean canRead(EvaluationContext context, Object target, String name) {
        return target instanceof Map && ((Map<?, ?>) target).containsKey(name);
    }

    @Override
    public TypedValue read(EvaluationContext context, Object target, String name) {
        return (TypedValue) ((Map<?, ?>) target).get(name);
    }

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

    @Override
    public void write(EvaluationContext context, Object target, String name, Object newValue) {
        ((Map<Object, Object>) target).put(name, newValue);
    }
}
