-
Notifications
You must be signed in to change notification settings - Fork 28
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
Cannot cast a spec to data class (in general issue in casting) #46
Comments
@Animeshz You cannot cast a spec to a data class and you don't have to. Spec is used as a schema for your HOCON source. We cannot use two schema to explain the same source. When you use Fortunately, there is a much simpler solution for your case. Konf use Jackson to support data class, thus you can use Jackson annotation to cast your key names. Add import com.fasterxml.jackson.annotation.JsonProperty
data class Configuration (val server: Server, val hardware: Hardware){
enum class SerialAddressConfiguration(val value: Int) {
GND(0),
VS(1),
SDA(2),
SCL(3)
}
data class Server(val port: Int, @JsonProperty("database file") val dbFile: String)
data class Hardware(
val A0: SerialAddressConfiguration,
val A1: SerialAddressConfiguration,
@JsonProperty("resistor shunt") val rShunt: Double,
@JsonProperty("max expected current") val maxExpectedCurrent: Double)
} And then you can cast the config to the data class directly: val configuration = config.at("configuration").toValue<Configuration>() |
Omg, why this moment is not in readme ... I spent several hours trying to understand why my config cause error. Please mark that you must write 'at' and why |
@uchuhimo also what i should write in 'at' if my config is this and i want to cast it to ApiConfig data class |
@uchuhimo For some reason i also get error
config.json:
|
1. To reproduce the error run the following code.:
Main.kt
ConfigurationSpec.kt:
Confguration.kt:
Sample hocon file:
2. Error occurred:
3. Expected behavior:
Should cast the spec to the Configuration data class. I did not cast to data class directly because configuration hocon file has spaces in between the keys which cannot be casted to standard kotlin as variables with spaces donot exist :(
The text was updated successfully, but these errors were encountered: