Skip to content

Commit

Permalink
feat(cdk): Fix issues with Kotlin conversion (#36688)
Browse files Browse the repository at this point in the history
Fixes a potential compile issue relate to the use of `addLast`.  I also noticed that the `convertTo` method had incorrect logic based on the implementation in the Java version of the class.
  • Loading branch information
jdpgrailsdev authored Mar 29, 2024
1 parent 670771d commit e32e58d
Showing 1 changed file with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package io.airbyte.commons.enums
import com.google.common.base.Preconditions
import com.google.common.collect.Maps
import com.google.common.collect.Sets
import java.util.*
import java.util.Arrays
import java.util.Locale
import java.util.Optional
import java.util.concurrent.ConcurrentMap
import java.util.stream.Collectors

Expand All @@ -18,20 +20,14 @@ class Enums {
return null
}

return enumValueOf<T2>(ie.name)
return java.lang.Enum.valueOf(oe, ie.name)
}

private fun normalizeName(name: String): String {
return name.lowercase(Locale.getDefault()).replace("[^a-zA-Z0-9]".toRegex(), "")
}

fun <T1 : Enum<T1>?> valuesAsStrings(e: Class<T1>): Set<String> {
Preconditions.checkArgument(e.isEnum)
return Arrays.stream(e.enumConstants)
.map { obj: T1 -> obj!!.name }
.collect(Collectors.toSet())
}

@Suppress("UNCHECKED_CAST")
fun <T : Enum<T>> toEnum(value: String, enumClass: Class<T>): Optional<T> {
Preconditions.checkArgument(enumClass.isEnum)

Expand All @@ -41,11 +37,11 @@ class Enums {
for (t in values) {
mappings[normalizeName(t!!.name)] = t
}
NORMALIZED_ENUMS.put(enumClass, mappings)
NORMALIZED_ENUMS[enumClass] = mappings
}

return Optional.ofNullable<T>(
NORMALIZED_ENUMS.getValue(enumClass)[normalizeName(value)] as T?
NORMALIZED_ENUMS.getValue(enumClass)[normalizeName(value)] as T?,
)
}

Expand All @@ -62,21 +58,19 @@ class Enums {
.collect(Collectors.toSet()),
Arrays.stream(c2.enumConstants)
.map { obj: T2 -> obj!!.name }
.collect(Collectors.toSet())
.collect(Collectors.toSet()),
)
.isEmpty())
}

inline fun <T1 : Enum<T1>, reified T2 : Enum<T2>> convertListTo(
ies: List<T1>,
oe: Class<T2>
): List<T2> {
val retVal: List<T2> = mutableListOf()
for (ie in ies) {
val convertedValue = convertTo(ie, oe)
retVal.addLast(convertedValue)
}
return retVal
): List<T2?> {
return ies.stream()
.map { ie -> convertTo(ie, oe) }
.collect(Collectors.toList())
.toList()
}
}
}

0 comments on commit e32e58d

Please sign in to comment.