package com.github.aurae.retrofit;

import com.bluelinelabs.logansquare.LoganSquare;
import com.squareup.okhttp.ResponseBody;
import retrofit.Converter;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

final class LoganSquareResponseBodyConverter implements Converter<ResponseBody, Object> {

    private final Type type;

    LoganSquareResponseBodyConverter(Type type) {
        this.type = type;
    }

    @Override
    public Object convert(ResponseBody value) throws IOException {
        InputStream is = value.byteStream();
        if (type instanceof Class) {
            // Plain object conversion
            return LoganSquare.parse(is, (Class<?>) type);

        } else if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type[] typeArguments = parameterizedType.getActualTypeArguments();
            Type firstType = typeArguments[0];

            // Check for Map arguments
            if (parameterizedType.getRawType() == Map.class) {
                Type secondType = typeArguments[1];

                // Perform validity checks on the type arguments, since LoganSquare works only on String keys
                if (firstType == String.class && secondType instanceof Class) {
                    // Map conversion
                    return LoganSquare.parseMap(is, (Class<?>) secondType);
                }

            } else if (firstType instanceof Class) {
                // List conversion
                return LoganSquare.parseList(is, (Class<?>) firstType);
            }
        }
        return null;
    }
}

@@ -274,24 +274,47 @@
 flect.Type;%0A
+import java.util.List;%0A
 import java.
@@ -323,16 +323,16 @@
 il.Map;%0A
-
 %0Afinal c
@@ -1100,20 +1100,31 @@
         
-if (
+Type rawType = 
 paramete
@@ -1145,16 +1145,41 @@
 awType()
+;%0A            if (rawType
  == Map.
@@ -1581,24 +1581,69 @@
   %7D else if 
+(rawType == List.class) %7B%0A                if 
 (firstType i
@@ -1677,16 +1677,20 @@
         
+  
+  
 // List 
@@ -1692,32 +1692,36 @@
 List conversion%0A
+    
                 
@@ -1772,24 +1772,158 @@
 firstType);%0A
+                %7D%0A            %7D else %7B%0A                // TODO Parametrized types%0A                // ...%0A                return null;%0A
             

Prepare for non-List parametrized types