From 7468861e162ab94ad62f0a4abd1466778b993e3d Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Thu, 4 Apr 2019 17:11:01 -0400 Subject: Improve data type handling Change-Id: I5ebcfcecdf1781e30be1ca929b4bf9e1526691a3 Issue-ID: CCSDK-1127 Signed-off-by: Muthuramalingam, Brinda Santh --- .../controllerblueprints/core/CustomFunctions.kt | 53 +++++++++++++--------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'ms/controllerblueprints/modules') diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index 0493deb5e..d45571cdf 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -45,11 +45,32 @@ fun Double.asJsonPrimitive(): DoubleNode { return DoubleNode.valueOf(this) } -fun MutableMap.asJsonNode(): JsonNode { +fun T.asJsonType(): JsonNode { + return if (this == null) { + NullNode.instance + } else { + when (this) { + is JsonNode -> + this + is String -> + TextNode(this) + is Boolean -> + BooleanNode.valueOf(this) + is Int -> + IntNode.valueOf(this.toInt()) + is Double -> + DoubleNode.valueOf(this.toDouble()) + else -> + JacksonUtils.jsonNodeFromObject(this) + } + } +} + +fun Map.asJsonNode(): JsonNode { return JacksonUtils.jsonNodeFromObject(this) } -fun MutableMap.asObjectNode(): ObjectNode { +fun Map.asObjectNode(): ObjectNode { return JacksonUtils.objectNodeFromObject(this) } @@ -60,7 +81,7 @@ fun format(message: String, vararg args: Any?): String { return message } -fun MutableMap.castOptionalValue(key: String, valueType: KClass): T? { +fun Map.castOptionalValue(key: String, valueType: KClass): T? { if (containsKey(key)) { return get(key) as? T } else { @@ -68,7 +89,7 @@ fun MutableMap.castOptionalValue(key: String, valueType: KC } } -fun MutableMap.castValue(key: String, valueType: KClass): T { +fun Map.castValue(key: String, valueType: KClass): T { if (containsKey(key)) { return get(key) as T } else { @@ -93,35 +114,23 @@ fun JsonNode.rootFieldsToMap(): MutableMap { fun MutableMap.putJsonElement(key: String, value: Any) { - when (value) { - is JsonNode -> - this[key] = value - is String -> - this[key] = TextNode(value) - is Boolean -> - this[key] = BooleanNode.valueOf(value) - is Int -> - this[key] = IntNode.valueOf(value.toInt()) - is Double -> - this[key] = DoubleNode.valueOf(value.toDouble()) - else -> - this[key] = JacksonUtils.jsonNodeFromObject(value) - } + val convertedValue = value.asJsonType() + this[key] = convertedValue } -fun MutableMap.getAsString(key: String): String { +fun Map.getAsString(key: String): String { return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)") } -fun MutableMap.getAsBoolean(key: String): Boolean { +fun Map.getAsBoolean(key: String): Boolean { return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)") } -fun MutableMap.getAsInt(key: String): Int { +fun Map.getAsInt(key: String): Int { return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)") } -fun MutableMap.getAsDouble(key: String): Double { +fun Map.getAsDouble(key: String): Double { return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)") } -- cgit 1.2.3-korg