次の設定をHOCON(Typesafe Config)ファイルからKotlinに読み込みたいと思います。KotlinでのHOCONの読み込みと処理
tablename: {
columns: [
{ item: { type: integer, key: true, null: false } }
{ desc: { type: varchar, length: 64 } }
{ quantity: { type: integer, null: false } }
{ price: { type: decimal, precision: 14, scale: 3 } }
]
}
実際にキー列を抽出したいと思います。私はこれまでに以下のことを試みました。
val metadata = ConfigFactory.parseFile(metafile)
val keys = metadata.getObjectList("${tablename.toLowerCase()}.columns")
.filter { it.unwrapped().values.first().get("key") == true }
ただし、次のエラーで失敗します。
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@kotlin.internal.InlineOnly public operator inline fun <@kotlin.internal.OnlyInputTypes K, V> kotlin.collections.Map<out kotlin.String, ???>.get(key: kotlin.String): ??? defined in kotlin.collections
Kotlinが地図に「値」フィールドのデータ型を理解することができないことは明らかです。それを宣言するか、コトリンに知らせるには?
また、このマップには異なるタイプとオプションのキーがあります。
PS:私は、KonfigやKlutterのようなKotlinに利用可能ないくつかのラッパーがあることを知っています。私はこれが書くのが簡単ならば別の図書館を避けることができると期待していました。
UPDATE 1:
私は次のことを試してみました。
it.unwrapped().values.first().get<String, Boolean>("key")
次のコンパイラエラーが発生します。出力
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@kotlin.internal.InlineOnly public operator inline fun <@kotlin.internal.OnlyInputTypes K, V> kotlin.collections.Map<out kotlin.String, kotlin.Boolean?>.get(key: kotlin.String): kotlin.Boolean? defined in kotlin.collections
と
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@kotlin.internal.InlineOnly public operator inline fun <@kotlin.internal.OnlyInputTypes K, V> kotlin.collections.Map<out kotlin.String, kotlin.Boolean>.get(key: kotlin.String): kotlin.Boolean? defined in kotlin.collections
そしてこの
it.unwrapped().values.first().get<String, Boolean?>("key")
UPDATE 2:それは別の場所で処理される方法を見てみると
、私はおそらく使用する必要があると思います反射。私の限られた露出でそれを試してみてください。今まで運がない。明らかである問題以上から
val keys = metadata.getObjectList("tablename.columns")
.filter {
val item:ConfigObject = it
val unwrapped:Map<String,Any?> = item.unwrapped()
val values:Collection<Any?> = unwrapped.values
val firstValue:Any? = values.first()
firstValue.get("key") == true // does not compile
}
:
おそらく設定オブジェクトのラップを解除する必要はありません。しかし、それをそのまま扱うと結果は得られませんでしたが、これは私が何かを "プリント"するためにもっとも近いものでした。 –