com.likethesalad.resource.serializer.android.internal.AndroidResourceMapper.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android-resource-serializer Show documentation
Show all versions of android-resource-serializer Show documentation
Plugin that gathers resources from Android projects for compilation purposes.
package com.likethesalad.resource.serializer.android.internal
import com.likethesalad.tools.resource.api.android.AndroidResource
import com.likethesalad.tools.resource.api.android.AndroidResourceScope
import com.likethesalad.tools.resource.api.android.BaseAndroidResource
import com.likethesalad.tools.resource.api.android.data.AndroidResourceType
import com.likethesalad.tools.resource.api.android.modules.integer.IntegerAndroidResource
import com.likethesalad.tools.resource.api.android.modules.string.StringAndroidResource
import com.likethesalad.tools.resource.api.data.ResourceType
internal object AndroidResourceMapper {
fun mapToAndroidResource(structure: AndroidResourceJsonStructure): AndroidResource {
val androidResourceType = AndroidResourceType.fromId(structure.type)
val scope = AndroidResourceScope.fromName(structure.scope)
val value = structure.value
val attributes = structure.attributes
return when (androidResourceType) {
AndroidResourceType.StringType -> StringAndroidResource(attributes, value, scope)
AndroidResourceType.IntegerType -> IntegerAndroidResource(attributes, value.toInt(), scope)
else -> createUnknownAndroidResource(attributes, value, scope, androidResourceType)
}
}
private fun createUnknownAndroidResource(
attributes: Map,
value: Any, scope: AndroidResourceScope,
type: AndroidResourceType
): AndroidResource {
return object : BaseAndroidResource(attributes, value, scope) {
override fun type(): ResourceType = type
}
}
fun mapToJson(resource: AndroidResource): AndroidResourceJsonStructure {
return AndroidResourceJsonStructure(
resource.attributes().asMap(),
resource.value().toString(),
resource.scope().getName(),
resource.type().getName()
)
}
}