package me.hfox.morphix.mapping.field;

import com.mongodb.BasicDBList;
import me.hfox.morphix.Morphix;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class MapMapper extends FieldMapper<Map> {

    protected Type[] types;

    public MapMapper(Class<?> parent, Field field, Morphix morphix) {
        super(Map.class, parent, field, morphix);
    }

    public MapMapper(MapMapper parent, ParameterizedType type) {
        super(Map.class, parent.parent, null, parent.morphix);
        types = type.getActualTypeArguments();
    }

    public MapMapper(CollectionMapper parent) {
        super(Map.class, parent.parent, null, parent.morphix);
        types = ((ParameterizedType) parent.type).getActualTypeArguments();
    }

    @Override
    protected void discover() {
        super.discover();
        if (field != null) {
            types = ((ParameterizedType) field.getGenericType()).getActualTypeArguments();
        }
    }

    @Override
    public Map marshal(Object obj) {
        if (obj == null) {
            return null;
        }

        return null;
    }

    @Override
    public BasicDBList unmarshal(Object obj) {
        return null;
    }

}

@@ -65,16 +65,79 @@
 DBList;%0A
+import com.mongodb.BasicDBObject;%0Aimport com.mongodb.DBObject;%0A
 import m
@@ -264,24 +264,52 @@
 flect.Type;%0A
+import java.util.ArrayList;%0A
 import java.
@@ -372,16 +372,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0Apublic
@@ -480,16 +480,53 @@
 %5D types;
+%0A    protected FieldMapper%5B%5D mappers;
 %0A%0A    pu
@@ -1228,237 +1228,2255 @@
   %7D%0A
-    %7D%0A%0A    @Override%0A    public Map marshal(Object obj) %7B%0A        if (obj == null) %7B%0A            return null;%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    @Override%0A    public BasicDBList unmarshal(Object obj) %7B%0A        return null
+%0A        mappers = new FieldMapper%5B%5D%7BgetMapper(types%5B0%5D), getMapper(types%5B1%5D)%7D;%0A    %7D%0A%0A    @Override%0A    @SuppressWarnings(%22unchecked%22)%0A    public Map marshal(Object obj) %7B%0A        if (obj == null) %7B%0A            return null;%0A        %7D%0A%0A        Map map = new HashMap%3C%3E();%0A        if (obj instanceof BasicDBList) %7B%0A            BasicDBList list = (BasicDBList) obj;%0A            for (Object object : list) %7B%0A                if (object instanceof DBObject) %7B%0A                    DBObject dbObject = (DBObject) object;%0A                    Object key = dbObject.get(%22key%22);%0A                    Object value = dbObject.get(%22value%22);%0A                    map.put(key, value);%0A                %7D%0A            %7D%0A        %7D%0A%0A        return map;%0A    %7D%0A%0A    @Override%0A    public BasicDBList unmarshal(Object obj) %7B%0A        BasicDBList list = new BasicDBList();%0A%0A        if (obj instanceof Map) %7B%0A            Map map = (Map) obj;%0A            for (Object obj2 : map.entrySet()) %7B%0A                if (obj2 instanceof Entry) %7B%0A                    Entry entry = (Entry) obj2;%0A                    BasicDBObject dbObject = new BasicDBObject();%0A                    dbObject.put(%22key%22, entry.getKey());%0A                    dbObject.put(%22value%22, entry.getValue());%0A                    list.add(dbObject);%0A                %7D%0A            %7D%0A        %7D%0A%0A        return list;%0A    %7D%0A%0A    private FieldMapper getMapper(Type type) %7B%0A        FieldMapper mapper = null;%0A        if (type instanceof Class) %7B%0A            Class%3C?%3E cls = (Class) type;%0A            mapper = FieldMapper.create(cls, parent, null, morphix);%0A        %7D else if (type instanceof ParameterizedType) %7B%0A            ParameterizedType param = (ParameterizedType) type;%0A            if (param.getRawType() instanceof Class) %7B%0A                Class%3C?%3E cls = (Class) param.getRawType();%0A                if (Collection.class.isAssignableFrom(cls)) %7B%0A                    mapper = new CollectionMapper(this, param);%0A                %7D else if (Map.class.isAssignableFrom(cls)) %7B%0A                    mapper = new MapMapper(this, param);%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (mapper == null) %7B%0A            throw new RuntimeException(%22Could not find suitable Mapper for %22 + type);%0A        %7D%0A%0A        return mapper
 ;%0A  

Complete MapMapper