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.
The newest version!
package com.likethesalad.resource.serializer.android.internal
import com.likethesalad.tools.resource.api.Resource
import com.likethesalad.tools.resource.api.android.AndroidResource
import com.likethesalad.tools.resource.api.android.BaseAndroidResource
import com.likethesalad.tools.resource.api.android.attributes.AndroidAttributeKey
import com.likethesalad.tools.resource.api.android.impl.AndroidResourceScope
import com.likethesalad.tools.resource.api.android.impl.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.attributes.AttributeKey
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: Map =
structure.attributes.mapKeys { AndroidAttributeKey.fromName(it.key) }
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(): Resource.Type = type
}
}
fun mapToJson(resource: AndroidResource): AndroidResourceJsonStructure {
return AndroidResourceJsonStructure(
resource.attributes().asMap().mapKeys { it.key.getName() },
resource.value().toString(),
resource.scope().getName(),
resource.type().getName()
)
}
}