summaryrefslogtreecommitdiffstats
path: root/ms/controllerblueprints/modules
diff options
context:
space:
mode:
authorDan Timoney <dtimoney@att.com>2019-04-06 00:24:05 +0000
committerGerrit Code Review <gerrit@onap.org>2019-04-06 00:24:05 +0000
commit1e61a0a2800231d49611f610eb39815864a2ea66 (patch)
tree11f11be4d40fed9ef4b53b14880e0834cc286a44 /ms/controllerblueprints/modules
parent6e8706275241805e5fd9d2962c51a2c8f3ee3c62 (diff)
parent7468861e162ab94ad62f0a4abd1466778b993e3d (diff)
Merge "Improve data type handling"
Diffstat (limited to 'ms/controllerblueprints/modules')
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt53
1 files changed, 31 insertions, 22 deletions
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<String, *>.asJsonNode(): JsonNode {
+fun <T : Any?> 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<String, *>.asJsonNode(): JsonNode {
return JacksonUtils.jsonNodeFromObject(this)
}
-fun MutableMap<String, *>.asObjectNode(): ObjectNode {
+fun Map<String, *>.asObjectNode(): ObjectNode {
return JacksonUtils.objectNodeFromObject(this)
}
@@ -60,7 +81,7 @@ fun format(message: String, vararg args: Any?): String {
return message
}
-fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
+fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
if (containsKey(key)) {
return get(key) as? T
} else {
@@ -68,7 +89,7 @@ fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KC
}
}
-fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
+fun <T : Any> Map<String, *>.castValue(key: String, valueType: KClass<T>): T {
if (containsKey(key)) {
return get(key) as T
} else {
@@ -93,35 +114,23 @@ fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
fun MutableMap<String, JsonNode>.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<String, JsonNode>.getAsString(key: String): String {
+fun Map<String, JsonNode>.getAsString(key: String): String {
return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
}
-fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
+fun Map<String, JsonNode>.getAsBoolean(key: String): Boolean {
return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
}
-fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
+fun Map<String, JsonNode>.getAsInt(key: String): Int {
return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
}
-fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
+fun Map<String, JsonNode>.getAsDouble(key: String): Double {
return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
}