diff --git a/konfy-properties/src/main/kotlin/tanvd/konfy/properties/PropertiesProvider.kt b/konfy-properties/src/main/kotlin/tanvd/konfy/properties/PropertiesProvider.kt index d61f626..7a07328 100644 --- a/konfy-properties/src/main/kotlin/tanvd/konfy/properties/PropertiesProvider.kt +++ b/konfy-properties/src/main/kotlin/tanvd/konfy/properties/PropertiesProvider.kt @@ -1,29 +1,20 @@ package tanvd.konfy.properties -import tanvd.konfy.conversion.ConversionService +import tanvd.konfy.properties.utils.Conversion import tanvd.konfy.provider.ConfigProvider -import tanvd.konfy.utils.toTypedArray import java.lang.reflect.Type -import java.util.Properties +import java.util.* class PropertiesProvider(private val properties: Properties) : ConfigProvider() { override fun fetch(key: String, type: Type): N? { val value = properties[key]?.toString() ?: return null - when { - (type is Class<*> && type.isArray) -> { - val values = value.split(",").map { it.trim() } - val converted = values.map { ConversionService.convert(it, type.componentType) } - return converted.toTypedArray(type.componentType) as N? - } - - else -> { - val converted = ConversionService.convert(value, type) - return converted as N? - } - } + return Conversion.convert(value, type) as N? } companion object { + /** + * Read properties from [value] string and return corresponding [PropertiesProvider] + */ fun from(value: String): PropertiesProvider { val properties = Properties() properties.load(value.byteInputStream()) diff --git a/konfy-properties/src/main/kotlin/tanvd/konfy/properties/utils/Conversion.kt b/konfy-properties/src/main/kotlin/tanvd/konfy/properties/utils/Conversion.kt new file mode 100644 index 0000000..a5e8776 --- /dev/null +++ b/konfy-properties/src/main/kotlin/tanvd/konfy/properties/utils/Conversion.kt @@ -0,0 +1,21 @@ +package tanvd.konfy.properties.utils + +import tanvd.konfy.conversion.ConversionService +import tanvd.konfy.utils.toTypedArray +import java.lang.reflect.Type + +object Conversion { + fun convert(value: String, type: Type): N? { + when { + (type is Class<*> && type.isArray) -> { + val values = value.split(",").map { it.trim() } + val converted = values.map { ConversionService.convert(it, type.componentType) } + return converted.toTypedArray(type.componentType) as N? + } + + else -> { + return ConversionService.convert(value, type) as N? + } + } + } +} \ No newline at end of file