Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Levenshtein distance calculation for the enhancement of the 'invalid enum' error #232

Merged
merged 2 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.akuleshov7.ktoml.exceptions

import com.akuleshov7.ktoml.utils.closestEnumName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.SerialDescriptor
Expand All @@ -28,14 +29,16 @@ internal class UnknownNameException(key: String, parent: String?) : TomlDecoding
" to true if you would like to skip unknown keys"
)


@OptIn(ExperimentalSerializationApi::class)
internal class InvalidEnumValueException(
value: String,
enumSerialDescriptor: SerialDescriptor,
lineNo: Int
) : TomlDecodingException(
"Value <$value> is not a valid enum option." +
" Permitted choices are: ${enumSerialDescriptor.elementNames.sorted().joinToString(", ")}"
"Line $lineNo: value <$value> is not a valid enum option." +
" Permitted choices are: ${enumSerialDescriptor.elementNames.sorted().joinToString(", ")}." +
" Did you mean <${enumSerialDescriptor.elementNames.closestEnumName(value)}>?"
)

internal class NullValueException(propertyName: String, lineNo: Int) : TomlDecodingException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,36 @@ public fun findPrimitiveTableInAstByName(children: List<TomlNode>, fullTableName

return findPrimitiveTableInAstByName(children.map { it.children }.flatten(), fullTableName)
}

/**
* Unfortunately Levenshtein method is implemented in jline and not ported to Kotlin Native.
* So we need to implement it (inspired by: https://pl.kotl.in/ifo0z0vMC)
*/
public fun levenshteinDistance(first: String, second: String): Int {
when {
first == second -> return 0
first.isEmpty() -> return second.length
second.isEmpty() -> return first.length
}

val firstLen = first.length + 1
val secondLen = second.length + 1
var distance = IntArray(firstLen) { it }
var newDistance = IntArray(firstLen) { 0 }

for (i in 1 until secondLen) {
newDistance[0] = i
for (j in 1 until firstLen) {
val costReplace = distance[j - 1] + (if (first[j - 1] == second[i - 1]) 0 else 1)
val costInsert = distance[j] + 1
val costDelete = newDistance[j - 1] + 1

newDistance[j] = minOf(costInsert, costDelete, costReplace)
}
distance = newDistance.also { newDistance = distance }
}
return distance[firstLen - 1]
}

public fun Iterable<String>.closestEnumName(enumValue: String): String? =
this.minByOrNull { levenshteinDistance(it, enumValue) }