jhc-systems / sqlest

Write SQL in Scala
https://jhc-systems.github.io/sqlest/latest/api/
Apache License 2.0
30 stars 17 forks source link

Implement extractor.flatMap #38

Closed brendanator closed 9 years ago

brendanator commented 9 years ago

This will allow the following sort of code

  val contactDetailsExtractor = contactTypeExtractor.flatMap { _ match {
    case "Email" => emailExtractor
    case "Post" => addressExtractor
  }

sqlest needs to know the extractors that are contained within another extractor so the flatMap method will have to be a macro that determines all the extractors used with the function

brendanator commented 9 years ago

After a long chat with @DavidGregory084 it would seem as though it is nigh on impossible to implement this and determine all the columns that are used in an extractor - the extractor would have to be run to determine this. The columns are needed so a select statement knows which columns to select

A compromise solution is to implement something like this

  def choice[B](choices: Tuple[A, Extractor[B]]*): Extractor[B]

  val contactDetailsExtractor = contactTypeExtractor.choice (
    "Email" -> emailExtractor,
    "Post" -> addressExtractor
  )

Maybe a better name than choice can be thought of

DavidGregory084 commented 9 years ago

Either that or we write a really gnarly macro that traverses the tree of extractors that's returned by our A => Extractor[B] looking for CellExtractors that are also AliasedColumns. This would probably have quite a few limitations and enforce a rigid syntax by aborting when the expressions in the function were not of the form case (a, b, c) => abcExtractor.