import com.fasterxml.jackson.databind.node.*
import org.apache.commons.lang3.ObjectUtils
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils
+import org.slf4j.LoggerFactory
import org.slf4j.helpers.MessageFormatter
import kotlin.reflect.KClass
* @author Brinda Santh
*/
+fun <T : Any> logger(clazz: T) = LoggerFactory.getLogger(clazz.javaClass)!!
+
+fun <T : KClass<*>> logger(clazz: T) = LoggerFactory.getLogger(clazz.java)!!
+
+
fun <T : Any> T.bpClone(): T {
return ObjectUtils.clone(this)
}
fun String.isJson(): Boolean {
- return ((this.startsWith("{") && this.endsWith("}"))
- || (this.startsWith("[") && this.endsWith("]")))
+ return ((this.trim().startsWith("{") && this.trim().endsWith("}"))
+ || (this.trim().startsWith("[") && this.trim().endsWith("]")))
}
fun Any.asJsonString(intend: Boolean? = false): String {
return TextNode(this)
}
+inline fun <reified T : Any> String.jsonAsType(): T {
+ return JacksonUtils.readValue<T>(this.trim())
+}
+
// If you know the string is json content, then use the function directly
fun String.jsonAsJsonType(): JsonNode {
return JacksonUtils.jsonNode(this.trim())
return DoubleNode.valueOf(this)
}
+/**
+ * Utility to convert Primitive object to Json Type Primitive.
+ */
+fun <T : Any?> T.asJsonPrimitive(): JsonNode {
+ return if (this == null || this is MissingNode || this is NullNode) {
+ NullNode.instance
+ } else {
+ when (this) {
+ is String ->
+ this.asJsonPrimitive()
+ is Boolean ->
+ this.asJsonPrimitive()
+ is Int ->
+ this.asJsonPrimitive()
+ is Double ->
+ this.asJsonPrimitive()
+ else ->
+ throw BluePrintException("$this type is not supported")
+ }
+ }
+}
+
+/** Based on Blueprint DataType Convert string value to JsonNode Type **/
+fun String.asJsonType(bpDataType: String): JsonNode {
+ return when (bpDataType.toLowerCase()) {
+ BluePrintConstants.DATA_TYPE_STRING -> this.asJsonPrimitive()
+ BluePrintConstants.DATA_TYPE_BOOLEAN -> this.toBoolean().asJsonPrimitive()
+ BluePrintConstants.DATA_TYPE_INTEGER -> this.toInt().asJsonPrimitive()
+ BluePrintConstants.DATA_TYPE_FLOAT -> this.toFloat().asJsonPrimitive()
+ BluePrintConstants.DATA_TYPE_DOUBLE -> this.toDouble().asJsonPrimitive()
+ // For List, Map and Complex Types.
+ else -> this.jsonAsJsonType()
+ }
+}
+
+/**
+ * Utility to convert Complex or Primitive object to Json Type.
+ */
fun <T : Any?> T.asJsonType(): JsonNode {
- return if (this == null) {
+ return if (this == null || this is MissingNode || this is NullNode) {
NullNode.instance
} else {
when (this) {
return JacksonUtils.getListFromJsonNode(this, String::class.java)
}
-fun JsonNode.returnNullIfMissing(): JsonNode? {
- return if (this is NullNode || this is MissingNode) {
- null
- } else this
+fun <T> JsonNode.asType(clazzType: Class<T>): T {
+ return JacksonUtils.readValue(this, clazzType)
+ ?: throw BluePrintException("couldn't convert JsonNode of type $clazzType")
}
-fun <T : JsonNode> T?.isNull(): Boolean {
- return if (this == null || this is NullNode || this is MissingNode) {
- true
- } else false
+fun JsonNode.asListOfString(): List<String> {
+ check(this is ArrayNode) { "JsonNode is not of type ArrayNode" }
+ return this.asListOfString()
}
-fun <T : JsonNode> T?.isNotNull(): Boolean {
+fun <T : JsonNode> T?.returnNullIfMissing(): JsonNode? {
return if (this == null || this is NullNode || this is MissingNode) {
- false
- } else true
+ null
+ }
+ else this
+}
+
+fun <T : JsonNode> T?.isNullOrMissing(): Boolean {
+ return this == null || this is NullNode || this is MissingNode
}
/**
return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
}
+fun Map<String, JsonNode>.getOptionalAsString(key: String): String? {
+ return if (this.containsKey(key)) this[key]!!.asText() else null
+}
+
+fun Map<String, JsonNode>.getOptionalAsBoolean(key: String): Boolean? {
+ return if (this.containsKey(key)) this[key]!!.asBoolean() else null
+}
+
+fun Map<String, JsonNode>.getOptionalAsInt(key: String): Int? {
+ return if (this.containsKey(key)) this[key]!!.asInt() else null
+}
+
+fun Map<String, JsonNode>.getOptionalAsDouble(key: String): Double? {
+ return if (this.containsKey(key)) this[key]!!.asDouble() else null
+}
+
// Checks
inline fun checkEquals(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
return value != null && value.isNotBlank()
}
+fun <T : String> T?.emptyTONull(): String? {
+ return if (this == null || this.isEmpty()) null else this
+}
fun nullToEmpty(value: String?): String {
return if (isNotEmpty(value)) value!! else ""
}
+inline fun <reified T : JsonNode> T.isComplexType(): Boolean {
+ return this is ObjectNode || this is ArrayNode
+}
+
+// Json Parsing Extensions
+fun JsonNode.jsonPathParse(expression: String): JsonNode {
+ check(this.isComplexType()) { "$this is not complex or array node to apply expression" }
+ return JsonParserUtils.parse(this, expression)
+}
+
+// Json Path Extensions
+fun JsonNode.jsonPaths(expression: String): List<String> {
+ check(this.isComplexType()) { "$this is not complex or array node to apply expression" }
+ return JsonParserUtils.paths(this, expression)
+}
+