package cn.skcks.matrix.v2.model.jwt.convert;

import cn.skcks.matrix.v2.model.jwt.dto.Claims;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.cglib.beans.BeanMap;

import java.util.HashMap;
import java.util.Map;

@Mapper
@SuppressWarnings({"unused"})
public abstract class ClaimConvertor {
    public final static ClaimConvertor INSTANCE = Mappers.getMapper(ClaimConvertor.class);

    public Map<String,Object> toMap(Claims claims) {
        BeanMap beanMap = BeanMap.create(claims);
        Map<String,Object> map = new HashMap<>(beanMap.size());
        for(Object key: beanMap.keySet()){
            map.put(String.valueOf(key),beanMap.get(key));
        }
        return map;
    }

    public Claims toClaims(Map<String,Object> map) {
        Claims claims = new Claims();
        if(map == null){
            return claims;
        }

        BeanMap.create(claims).putAll(map);
        return claims;

    }
}
