Skip to content

Commit

Permalink
CASL-108 allowed table name length changed to 44
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhead-pl committed Oct 18, 2024
1 parent 484df12 commit d3df611
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
31 changes: 24 additions & 7 deletions here-naksha-lib-model/src/commonMain/kotlin/naksha/model/Naksha.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class Naksha private constructor() {
@JsStatic
val VIRT_DICTIONARIES_QUOTED = quoteIdent(VIRT_DICTIONARIES)

/**
* Maximum collectionId name length allowed to give by clients. Rest of "free" characters are reserved for partitioning suffix.
*/
private const val MAX_COLLECTION_ID_NAME_LENGTH = 44

/**
* Tests if the given **id** is a valid identifier, so matches:
*
Expand All @@ -89,7 +94,7 @@ class Naksha private constructor() {
@JsStatic
@JvmStatic
fun isValidId(id: String?): Boolean {
if (id.isNullOrEmpty() || "naksha" == id || id.length > 32) return false
if (id.isNullOrEmpty() || "naksha" == id || id.length > MAX_COLLECTION_ID_NAME_LENGTH) return false
var i = 0
var c = id[i++]
// First character must be a-z
Expand All @@ -113,8 +118,12 @@ class Naksha private constructor() {
@JsStatic
@JvmStatic
fun verifyId(id: String?) {
if (id.isNullOrEmpty() || "naksha" == id || id.length > 32) {
throw NakshaException(ILLEGAL_ID, "The given identifier is null, empty or has more than 32 characters", id = id)
if (id.isNullOrEmpty() || "naksha" == id || id.length > MAX_COLLECTION_ID_NAME_LENGTH) {
throw NakshaException(
ILLEGAL_ID,
"The given identifier is null, empty or has more than $MAX_COLLECTION_ID_NAME_LENGTH characters",
id = id
)
}
var i = 0
var c = id[i++]
Expand Down Expand Up @@ -170,10 +179,18 @@ class Naksha private constructor() {
for (part in parts) {
for (c in part) {
when (c) {
in 'a'..'z', in 'A'..'Z', in '0'..'9','_' -> sb.append(c)
'"' -> { quoted = true; sb.append('"').append('"') }
'\\' -> { quoted = true; sb.append('\\').append('\\') }
else -> { quoted = true; sb.append(c) }
in 'a'..'z', in 'A'..'Z', in '0'..'9', '_' -> sb.append(c)
'"' -> {
quoted = true; sb.append('"').append('"')
}

'\\' -> {
quoted = true; sb.append('\\').append('\\')
}

else -> {
quoted = true; sb.append(c)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package naksha.model

import naksha.base.PlatformUtil.PlatformUtilCompanion.randomString
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class NakshaTest {

@Test
fun shouldLimitCollectionIdLength() {
// expect
var collectionId = collectionIdOf(1)
assertTrue(collectionId) { Naksha.isValidId(collectionId) }
collectionId = collectionIdOf(44)
assertTrue(collectionId) { Naksha.isValidId(collectionId) }

collectionId = collectionIdOf(45)
assertFalse(collectionId) { Naksha.isValidId(collectionId) }
assertFalse(collectionId) { Naksha.isValidId("") }
}

@Test
fun shouldOnlyAllowCharacterAsFirstChar() {
// expect
assertTrue{ Naksha.isValidId("c1232_name") }
assertFalse{ Naksha.isValidId("11232_name") }
}

@Test
fun shouldNotAllowCapitalLettersOrUnsupportedCharacters() {
// expect
assertFalse{ Naksha.isValidId("C1232_name") }
assertFalse{ Naksha.isValidId("name\$a") }
assertFalse{ Naksha.isValidId("name&a") }
assertFalse{ Naksha.isValidId("name*a") }
assertFalse{ Naksha.isValidId("name#a") }
assertFalse{ Naksha.isValidId("name@a") }
assertFalse{ Naksha.isValidId("name!a") }

assertTrue{ Naksha.isValidId("name_a") }
assertTrue{ Naksha.isValidId("name-a") }
assertTrue{ Naksha.isValidId("name:a") }
}

private fun collectionIdOf(length: Int): String = "c" + randomString(length - 1).lowercase()
}

0 comments on commit d3df611

Please sign in to comment.