Skip to content

Instantly share code, notes, and snippets.

@jidolstar
Created July 9, 2021 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jidolstar/c2735dba7fd7baa12f5734dc3262de49 to your computer and use it in GitHub Desktop.
Save jidolstar/c2735dba7fd7baa12f5734dc3262de49 to your computer and use it in GitHub Desktop.
package entity
import ein2a.core.entity.*
import ein2a.core.json.eJSON
import ein2a.core.log.log
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
// 아래와 같은 필드를 처리함
//{
// "stocks":{
// "210408000000001":{"common":20, "bluebasket":30},
// "210408000000002":{"common":10, "bluebasket":20}
// }
//}
inline fun <T:Any> eEntity.valuemapMap(prop: KProperty<*>, type:KClass<T>, block: ValueMapMapField<T>.()->Unit = {}) = ValueMapMapField(type).also{
props[prop] = it
it.block()
}
class ValueMapMapField<T:Any>(val type: KClass<T>): ValueField<MutableMap<String, MutableMap<String,T>>>(type){
override fun fromJSON(key: String, json: eJSON, report: Report): Boolean {
return if(json is eJSON.Object){
val map = hashMapOf<String, Any>()
if(json.data.all {(jKey, el)->
el as eJSON.Object
map[jKey] = el.data.mapValues{(k, el)->
val result = value(k, el, report) ?: let{
report.add("Field value is wrong format", "key" to key, "jKey" to jKey, "field" to k, "json" to json)
return false
}
result
}
true
}){
isActive = true
v = map
true
}else false
}else{
report.add("field is value map but json is not Object", "key" to key, "field" to this, "json" to json)
false
}
}
override fun toJSON(convString: Boolean): String {
return "{${
(v as? MutableMap<*, *>)?.let{
if(it.isEmpty()) "" else{
var acc = ""
it.forEach {(k, v)->
log("acc1 = $acc")
(v as? MutableMap<*, *>)?.let {subV->
var subAcc = ""
subV.forEach {(k, v)->subAcc = "$subAcc,\"$k\":${valueToString(v, convString)}"}
acc = "$acc,\"$k\":{${subAcc.substring(1)}}"
log("acc2 = $acc")
}
}
acc.substring(1)
}
} ?: ""
}}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment