import kotlin.reflect.full.memberProperties
data class Abc(
val a: String,
val b: String,
val c: String,
val d: D,
val e: E
) {
data class D(
val e: String,
val f: String,
val g: String
)
data class E(
val h: List<String>
)
}
data class Xyz(
val a: String,
val b: String,
val c: String,
val d: D,
val e: E
) {
data class D(
val e: String,
val f: String,
val g: G
){
data class G(
val h: String,
val i: String
)
}
data class E(
val h: List<String>
)
}
fun hasContent(inVal: Any): Boolean {
if (inVal::class.simpleName == "String") {
if (inVal != "") {
return true
}
} else {
inVal::class.memberProperties.forEach { ele ->
if (ele.returnType.classifier == String::class) {
if (ele.call(inVal) != "") {
return true
}
}
else if (ele.returnType.classifier == List::class) {
val list = ele.call(inVal) as List<*>
val listCheck = list.map { hasContent(it!!) }
if (listCheck.contains(true)) {
return true
}
}
else {
if (hasContent(ele.call(inVal)!!)) {
return true
}
}
}
return false
}
return false
}
fun main() {
// aa has content, should print true
val aa = Abc("a1", "b2", "c3", Abc.D("e4", "f5", "g6"), Abc.E(listOf("h7", "i8", "j9")))
println(" aa has content -> ${hasContent(aa)}")
// bb has no content, should print false
val bb = Abc("", "", "", Abc.D("", "", ""), Abc.E(listOf("", "", "")))
println(" bb has content -> ${hasContent(bb)}")
// cc has content, should print true
val cc = Abc("", "", "", Abc.D("e4", "", ""), Abc.E(listOf("", "", "j9")))
println(" cc has content -> ${hasContent(cc)}")
// dd has content, should print true
val dd = Abc("", "", "", Abc.D("", "", ""), Abc.E(listOf("", "", "j9")))
println(" dd has content -> ${hasContent(dd)}")
// xx has no content, should print false.
val xx = Xyz("", "", "", Xyz.D("", "", Xyz.D.G("", "")), Xyz.E(listOf("", "", "")))
println(" xx has content -> ${hasContent(xx)}")
// yy has content, should print true.
val yy = Xyz("", "", "", Xyz.D("", "", Xyz.D.G("", "")), Xyz.E(listOf("", "", "blahblah")))
println(" yy has content -> ${hasContent(yy)}")
// zz has content, should print true.
val zz = Xyz("", "", "", Xyz.D("", "", Xyz.D.G("", "blahblah")), Xyz.E(listOf("", "", "")))
println(" zz has content -> ${hasContent(zz)}")
}
No comments:
Post a Comment
Any comments?