Skip to content

Instantly share code, notes, and snippets.

@inmyth
Created June 12, 2018 16:21
Show Gist options
  • Select an option

  • Save inmyth/7754797521d4318b89539ef72668af6b to your computer and use it in GitHub Desktop.

Select an option

Save inmyth/7754797521d4318b89539ef72668af6b to your computer and use it in GitHub Desktop.
Convert Object to Map of its fields
val p = OkexParameters(Some(signed), apiKey, Some(symbol), None, None, None, None, Some(status), Some(currentPage), Some(pageLength))
import reflect.runtime.universe._
import reflect.runtime.currentMirror
val m = currentMirror.reflect(p)
m.symbol.typeSignature.members.toStream
.collect{case s : TermSymbol if !s.isMethod => m.reflectField(s)}
.filter(r => r.get != None) // filter out None
.map(r => r.get match {
case Some(v) => r.symbol.name.toString.trim -> v.toString
case _ => r.symbol.name.toString.trim -> r.get.toString
})
// .map(r => s"${r._1}=${r._2}")
// .mkString("&")
.toMap
// Map(current_page -> 1, sign -> 4343GG2302394, symbol -> trx_eth, api_key -> aaaa, status -> unfilled, page_length -> 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment