반응형
- 액션Path,파라미터 ...
- init 이란? 일종의 초기화 블록코틀린 문법 -> 객체 생성시 딱 한번 실행된다.
class Rq(command:String){
val actionPath: String
val paramMap: Map<String, String>
init {
val commandBits = command.split("?", limit = 2)
actionPath = commandBits[0].trim()
val queryStr = if (commandBits.lastIndex == 1 && commandBits[1].isNotEmpty()) {
commandBits[1].trim()
} else {
""
}
paramMap = if (queryStr.isEmpty()) {
mapOf()
} else {
val paramMapTemp = mutableMapOf<String, String>()
// queryStr = id=1&body=2&title=3&age
val queryStrBits = queryStr.split("&")
for (queryStrBit in queryStrBits) {
// queryStrBit = id=1
val queryStrBitBits = queryStrBit.split("=", limit = 2)
val paramName = queryStrBitBits[0]
val paramValue = if (queryStrBitBits.lastIndex == 1 && queryStrBitBits[1].isNotEmpty()) {
queryStrBitBits[1].trim()
} else {
""
}
if (paramValue.isNotEmpty()) {
paramMapTemp[paramName] = paramValue
}
}
paramMapTemp.toMap()
}
}
- mapOf() : 읽기 전용으로 만든다.
- toMap() : 리스트를 Map 으로 바꾼다.
paramMap = if (queryStr.isEmpty()) {
mapOf()
} else {
val paramMapTemp = mutableMapOf<String, String>()
// queryStr = id=1&body=2&title=3&age
val queryStrBits = queryStr.split("&")
for (queryStrBit in queryStrBits) {
// queryStrBit = id=1
val queryStrBitBits = queryStrBit.split("=", limit = 2)
val paramName = queryStrBitBits[0]
val paramValue = if (queryStrBitBits.lastIndex == 1 && queryStrBitBits[1].isNotEmpty()) {
queryStrBitBits[1].trim()
} else {
""
}
if (paramValue.isNotEmpty()) {
paramMapTemp[paramName] = paramValue
}
}
paramMapTemp.toMap()
}
전체 코드
fun Day05_P(){
println("==== Simple SSG 시작 =====")
while (true){
print("명령어) ")
val command = readLineTrim()
val rq = Rq(command)
println("rq.actionPath : ${rq.actionPath}, rq.paramMap : ${rq.paramMap}")
println(rq.getStringParam("title", "1") == "제목1")
println(rq.getIntParam("id",-1)==1)
}
}
class Rq(command:String){
val actionPath: String
val paramMap: Map<String, String>
init {
val commandBits = command.split("?", limit = 2)
actionPath = commandBits[0].trim()
val queryStr = if (commandBits.lastIndex == 1 && commandBits[1].isNotEmpty()) {
commandBits[1].trim()
} else {
""
}
paramMap = if (queryStr.isEmpty()) {
mapOf()
} else {
val paramMapTemp = mutableMapOf<String, String>()
// queryStr = id=1&body=2&title=3&age
val queryStrBits = queryStr.split("&")
for (queryStrBit in queryStrBits) {
// queryStrBit = id=1
val queryStrBitBits = queryStrBit.split("=", limit = 2)
val paramName = queryStrBitBits[0]
val paramValue = if (queryStrBitBits.lastIndex == 1 && queryStrBitBits[1].isNotEmpty()) {
queryStrBitBits[1].trim()
} else {
""
}
if (paramValue.isNotEmpty()) {
paramMapTemp[paramName] = paramValue
}
}
paramMapTemp.toMap()
}
}
fun getStringParam(key:String, default:String): String {
return try {
paramMap[key]!!
}catch (e:NullPointerException){
default
}
}
fun getIntParam(key:String, default: Int): Int {
return try{
paramMap[key]!!.trim().toInt()
}catch (e:NullPointerException){
default
}
반응형
'Another-Develop > kotlin' 카테고리의 다른 글
06 코틀린 - Jackson (0) | 2021.05.06 |
---|---|
04 코틀린 - When, downTo , .. , until (0) | 2021.04.21 |
03 코틀린 - MutableList (0) | 2021.04.20 |
02 코틀린 - readLine (자료형변환) , 배열 (0) | 2021.04.16 |
01 코틀린 - 클래스, 데이터 클래스, 타입추론 , Nullable (0) | 2021.04.16 |