Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/kotlin/com/cosmotech/api/utils/StringExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ fun String.shaHash(): String {
}

fun String.toRedisMetaDataKey() = "${this}MetaData"

fun String.formatQuery(map: Map<String, String>): String {
var newValue = this
map.forEach { (key, value) ->
var sanitizedValue =
if (value.isNullOrBlank()) {
"null"
} else {
"\"${value.replace("\"","\\\"")}\""
}
newValue = newValue.replace("$$key", sanitizedValue)
}
return newValue
}
23 changes: 23 additions & 0 deletions src/test/kotlin/com/cosmotech/api/utils/StringExtensionsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,27 @@ class StringExtensionsTests {
val actual = input.toRedisMetaDataKey()
assertEquals(expected, actual)
}

@Test
fun `should format Cypher Query with variables`() {
val map =
mapOf(
"name" to "Joe Doe",
"id" to "100",
"object" to "{id:\"id\"}",
"array" to "{id:['one', 'two']}")
val actual = "CREATE (:Person {name: \$name, id: \$id, object: \$object, array: \$array)"
val expected =
"CREATE (:Person {name: \"Joe Doe\", id: \"100\", object: \"{id:\\\"id\\\"}\", " +
"array: \"{id:['one', 'two']}\")"
assertEquals(expected, actual.formatQuery(map))
}

@Test
fun `should format Cypher Query & replace empty value with null`() {
val map = mapOf("name" to "Joe Doe", "id" to " ")
val actual = "CREATE (:Person {name: \$name, id: \$id})"
val expected = "CREATE (:Person {name: \"Joe Doe\", id: null})"
assertEquals(expected, actual.formatQuery(map))
}
}