我具有嵌套扩展的以下配置
open class Config {
val api = ApiConfig()
fun api(action: Action<in ApiConfig>) = action.execute(api)
val uploadConfig = UploadConfig()
fun uploadConfig(action: Action<in UploadConfig>) = action.execute(uploadConfig)
}
data class ApiConfig(
var projectId: String = "",
var token: String = ""
)
open class UploadConfig {
var str: MutableList<String> = mutableListOf()
val uploadEntries: MutableList<UploadEntry> = mutableListOf()
fun uploadEntry(action: Action<in UploadEntry>) {
val newEntry = UploadEntry()
action.execute(newEntry)
uploadEntries.add(newEntry)
}
}
open class UploadEntry(
var lang: String = "",
var path: String = ""
)
这是用法:
myPlugin {
api {
projectId = "111"
token = "222"
}
stringsUploadConfig {
str = ["s1", "s2"] // this works good
uploadEntry {
lang = "en" // here I got error
path = "path-to-file"
}
}
}
对于Api配置,一切都很好,但是当我想访问uploadEntry{ lang
时遇到错误
> Could not set unknown property 'lang' for an object of type mypackage.UploadConfig.
如何访问第二个嵌套对象UploadEntry