Created
September 7, 2017 22:04
-
-
Save davidsantiago/e9fca4dbc4af2b68a0383731a492f3c4 to your computer and use it in GitHub Desktop.
Kotlin problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JsonConverter.kt | |
---------------- | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import io.requery.Converter | |
import org.postgresql.util.PGobject | |
open class JsonConverter<T>(private val clazz: Class<T>) : Converter<T, PGobject> { | |
private val objectMapper: ObjectMapper = ObjectMapper() | |
override fun getMappedType(): Class<T> { | |
return clazz | |
} | |
override fun getPersistedType(): Class<PGobject> { | |
return PGobject::class.java | |
} | |
override fun getPersistedSize(): Int? { | |
return null | |
} | |
override fun convertToPersisted(jsonObject: T?): PGobject? { | |
if (jsonObject == null) return null | |
val pgObject = PGobject() | |
pgObject.type = "json" | |
pgObject.value = objectMapper.writeValueAsString(jsonObject) | |
return pgObject | |
} | |
override fun convertToMapped(type: Class<out T?>, pgObj: PGobject?): T? { | |
if (pgObj == null) return null | |
return objectMapper.readValue(pgObj.value, type) | |
} | |
} | |
// Analytics.kt | |
--------------- | |
class AnalyticsPropertiesConverter : JsonConverter<Map<String, String>>(Map::class.java as Class<Map<String, String>>) | |
// At compile, error in auto-generated java file: | |
------------------------------------------------- | |
/Users/davidsantiago/Documents/Work/api/build/generated/source/kapt/main/co/bird/model/AnalyticsEventType.java:84: error: incompatible types: AnalyticsPropertiesConverter cannot be converted to Converter<Map<String,String>,?> | |
.setConverter(new co.bird.model.AnalyticsPropertiesConverter()) | |
// The code that has the compile error (AnalyticsEventType.java, lines 54-85): | |
------------------------------------------------------------------------------ | |
public static final AttributeDelegate<AnalyticsEvent, Map<String, String>> PROPERTIES = new AttributeDelegate( | |
new AttributeBuilder<AnalyticsEvent, Map<String, String>>("properties", (Class)Map.class) | |
.setProperty(new Property<AnalyticsEvent, Map<String, String>>() { | |
@Override | |
public Map<String, String> get(AnalyticsEvent entity) { | |
return entity.getProperties(); | |
} | |
@Override | |
public void set(AnalyticsEvent entity, Map<String, String> value) { | |
throw new UnsupportedOperationException(); | |
} | |
}) | |
.setPropertyName("getProperties") | |
.setBuilderProperty(new Property<AnalyticsEventType, Map<String, String>>() { | |
@Override | |
public Map<String, String> get(AnalyticsEventType entity) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void set(AnalyticsEventType entity, Map<String, String> value) { | |
entity.properties = value; | |
} | |
}) | |
.setGenerated(false) | |
.setReadOnly(false) | |
.setLazy(false) | |
.setNullable(true) | |
.setUnique(false) | |
.setConverter(new co.bird.model.AnalyticsPropertiesConverter()) | |
.build()); | |
^ | |
When I instead implement AnalyticsPropertiesConverter in Java, like this, it works: | |
// AnalyticsPropertiesConverter.java | |
------------------------------------ | |
package co.bird.model; | |
import co.bird.util.requery.converter.JsonConverter; | |
import java.util.Map; | |
public class AnalyticsPropertiesConverter extends JsonConverter<Map<String, String>> { | |
public AnalyticsPropertiesConverter() { | |
super((Class<Map<String, String>>)(Object)Map.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment