In my typescript class, I have a member that accepts any as the name:
interface ControlTagType {
type?: String | null;
[name: string]: any
}
class ControlTag {
tagSource: String | null = null;
tag: ControlTagType | null = null;
}
export { ControlTag };
With this setup, I can use the class in vuejs like this:
controlTag.push({ tagSource: 'USER', tag: { type: 'X', TAG_1: 'TAG_X' } });
controlTag.push({ tagSource: 'AUTO', tag: { type: 'Y', TAG_2: 'TAG_Y' } });
This solution worked for another problem I had.
However, I'm struggling to achieve the same generic member functionality in kotlin:
I have an enum class that I utilize during serialization:
@JsonValue
fun value(): X {
return X( this.name : this.name)
}
like so:
data class X(val [name: string]: any)
I can do it this way, but it's complex and needs to be repeated for each class:
@JsonValue
fun value(): Any? {
when (this.name) {
XX_XX.name -> {
return object {
var XX_XX: String = name
}
}
YY_YY.name -> {
return object {
var YY_YY: String = name
}
}
ZZ_ZZ.name -> {
return object {
var ZZ_ZZ: String = name
}
}
else -> return null
}
}
Furthermore, in this implementation, the parameter name is converted to lowercase:
{"type":"XX","xx_XX":"XX_XX"}