Cannot use CodingKey named "value"
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 3.2k
- Forks
- 106
- Avg merge
- 6d 13h
- Merged PRs (30d)
- 1
Description
The following swift:
public struct ValueHolder: Decodable {
public let x: Int
public let value: String?
public let z: Bool
}
transpiles into the kotlin:
class ValueHolder: Decodable {
val x: Int
val value: String?
val z: Boolean
constructor(x: Int, value: String? = null, z: Boolean) {
this.x = x
this.value = value
this.z = z
}
private enum class CodingKeys(override val rawValue: String, @Suppress("UNUSED_PARAMETER") unusedp: Nothing? = null): CodingKey, RawRepresentable<String> {
x("x"),
value("value"),
z("z");
}
private fun CodingKeys(rawValue: String): CodingKeys? {
return when (rawValue) {
"x" -> CodingKeys.x
"value" -> CodingKeys.value
"z" -> CodingKeys.z
else -> null
}
}
constructor(from: Decoder) {
val container = from.container(keyedBy = CodingKeys::class)
this.x = container.decode(Int::class, forKey = CodingKeys.x)
this.value = container.decodeIfPresent(String::class, forKey = CodingKeys.value)
this.z = container.decode(Boolean::class, forKey = CodingKeys.z)
}
companion object: DecodableCompanion<ValueHolder> {
override fun init(from: Decoder): ValueHolder = ValueHolder(from = from)
}
}
and yields the (rather inscrutable) compile error:
GRADLE> e: file://…Tests.kt:270:16 Expecting ';' after the last enum entry or '}' to close enum class body
GRADLE> e: file://…Tests.kt:271:14 Expecting member declaration
GRADLE> e: file://…Tests.kt:271:15 Expecting member declaration
GRADLE> e: file://…Tests.kt:271:16 Expecting member declaration
GRADLE> e: file://…Tests.kt:271:21 Expecting member declaration
GRADLE> e: file://…Tests.kt:271:22 Expecting member declaration
GRADLE> e: file://…Tests.kt:271:23 Expecting member declaration
GRADLE> e: file://…Tests.kt:272:9 Expecting member declaration
GRADLE> e: file://…Tests.kt:272:10 Expecting member declaration
GRADLE> e: file://…Tests.kt:272:11 Expecting member declaration
GRADLE> e: file://…Tests.kt:272:12 Expecting member declaration
GRADLE> e: file://…Tests.kt:272:13 Expecting member declaration
GRADLE> e: file://…Tests.kt:272:14 Expecting member declaration
GRADLE> e: file://…Tests.kt:278:35 Unresolved reference: value
GRADLE> e: file://…Tests.kt:279:31 Unresolved reference: z
GRADLE> e: file://…Tests.kt:287:83 Unresolved reference: value
GRADLE> e: file://…Tests.kt:288:71 Unresolved reference: z
GRADLE>
GRADLE> FAILURE: Build failed with an exception.
The workaround is to rename the value property to something else and assign the corresponding CodingKey to the name back to "value", like so:
public struct ValueHolder: Decodable {
public let x: Int
private let _value: String?
public var value: String? { _value }
public let z: Bool
public enum CodingKeys : String, CodingKey {
case x
case _value = "value"
case z
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Swift ValueHolder example and the generated Kotlin CodingKeys enum shown in the issue, then locate the transpiler path that emits CodingKey cases. Done means a property named value generates valid Kotlin and the decoding example compiles without the listed errors; add a regression case if the repository's test structure identifies an appropriate location.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin, swift
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100