marklister / product-collections

A very simple, strongly typed, scala framework for tabular data. A collection of tuples. A strongly typed scala csv reader and writer. A lightweight idiomatic dataframe / datatable alternative.
BSD 2-Clause "Simplified" License
144 stars 19 forks source link

added case class extraction #32

Closed antonkulaga closed 7 years ago

antonkulaga commented 9 years ago

I added creation from case classes. Here how it works

@extract case class User(name:String,surname:String)
  val tests = TestSuite{
    "Should create Data Frames from case classes" -{

      val (ann,bob) = (User("Ann","Cute"),User("Bob","Ugly"))
      val frame = User.asFrame(ann,bob)
      val names:Seq[String] = frame.names
      val surnames:Seq[String] = frame.surnames
      assert(names.size==2)
      assert(surnames.size==2)
      assert(names==Seq("Ann","Bob"))
      assert(surnames==Seq("Cute","Ugly"))
    }
  }

Macro annotations automaticly add asFrame method to companion object of a case class. In case of User it will be User.asFrame(someusers:_*) that will return User.Columns class where you can get Columns by case accesors names+"s", in our case: User.names, User.surnames

marklister commented 9 years ago

This looks very promising. And it looks like it will work with Scala js which I think is very important.

antonkulaga commented 9 years ago

Scalajs is not a problem, the problem is Scala 2.10.4, there there is no blackbox contexts for macroses. I personally do not use 2.10.4 but I see that your lib does.

antonkulaga commented 9 years ago

Regarding ScalaJS: it works ok for me there, but scalajs test adaptor in 0.6.2 contains one bug in test adapter that leads to some memory issues in tests only (like weird Boxed errors in tests)

marklister commented 9 years ago

Yeah, I'm happy to terminate scala 2.10 support if pc is macroized. Old users can still use 1.4.2 and if necessary -- highly unlikely -- I'll backport any bugfixes.

antonkulaga commented 9 years ago

We can create a separate branch to work on this feature.

marklister commented 9 years ago

This is a very worthwhile feature and something I've wanted p-c to do for a long time. Here's more or less how I originally envisaged it would work:

val ccSeq = CaseCollSeq(ann,bob)  // It's a wrapper for a Seq[Case class]

scala> ccSeq.names
res0: Seq[String] = ArrayBuffer(Ann, Bob)

This pull req is very close to the desired result. I just think that User.asFrame is a bit unintuitive for the end user. And I'd like to return a CaseCollSeq from CsvParserFN.parse.

Sadly, I've never written a macro so I'm playing catch up here. Do you think the outlined api is possible?

marklister commented 9 years ago

btw there is a feature branch https://github.com/marklister/product-collections/tree/antonkulaga-NamedFields

antonkulaga commented 9 years ago

Do you think the outlined api is possible?

I do not know how to achieve such api, I am not that good in macroses. I just chose the easiest way - macro annotations and quaziquotes with changing of companion object.