package com.brilliance.isc.common.validator.es.support.impl;

import com.brilliance.isc.common.validator.es.support.IJavaCallback;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.internal.runtime.Undefined;
import lombok.AllArgsConstructor;

import java.util.Map;

/**
 * 用于实现callback校验入参的回调
 * @author fukai
 */
@AllArgsConstructor
public class MessageCallback implements IJavaCallback {

    private Map<String,String> messageMap;

    private String path;

    @Override
    public Object callback(Object... args) {
        if (args.length > 0) {
            if (args[0] instanceof ScriptObjectMirror) {
                messageMap.put(path, (String) ((ScriptObjectMirror) args[0]).get("message"));
            } else if (args[0] instanceof String) {
                messageMap.put(path, (String) args[0]);
            } else if (args[0] instanceof Undefined) {
                //通过校验
            }
        }
        return null;
    }
}
