Skip to content

Commit

Permalink
#38 - fixed field selection syntax:
Browse files Browse the repository at this point in the history
- compose field selection with current arrow;
  • Loading branch information
fehu committed Jul 11, 2018
1 parent 4bc97c1 commit ef35209
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/main/scala/com/abraxas/slothql/mapper/ScalaExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ object ScalaExpr {
protected trait FieldSelectionOps extends Dynamic {
expr: ScalaExpr =>

def selectDynamic(k: String)(implicit ev: Syntax.HasField[Source, Symbol @@ k.type]): SelectField[Source, k.type, ev.Value] = SelectField(k)
def selectDynamic[V, A <: Arrow](k: String)(
implicit
ev0: Syntax.HasField.Aux[Target, Symbol @@ k.type, V],
ev1: expr.type <:< A, // using `expr.type` directly in `compose` would require _existential types_
compose: Arrow.Compose[SelectField[Target, k.type, V], A]
): compose.Out = compose(SelectField(k), expr)
}

object Syntax {
Expand Down
6 changes: 6 additions & 0 deletions src/test/scala/com/abraxas/slothql/TestArrows.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ object FunctorsTest {
// ]{type Source = Book;type Target = Option[String]}
// = MBind(SelectField(pseudonym)) ∘ SelectField(author)

val selIsbn = ScalaExpr[Book].meta.isbn
// Arrow.Composition[
// ScalaExpr.SelectField[Meta, isbn, String],
// ScalaExpr.SelectField[Book, meta, Meta]
// ]{type Source = Book;type Target = String}
// = SelectField(isbn) ∘ SelectField(meta)

// val mapped0 = Functor.map(sel2 ∘ sel1).to[GraphPath]
// val mapped1 = Functor.map(sel3 ∘ (sel2 ∘ sel1)).to[GraphPath]
Expand Down
32 changes: 27 additions & 5 deletions src/test/scala/com/abraxas/slothql/test/models/Book.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import shapeless.syntax.singleton._

import com.abraxas.slothql.mapper.{ GraphRepr, Schema }

case class Book(author: Option[Author], pages: List[Page])
case class Book(author: Option[Author], pages: List[Page], meta: Meta)
case class Author(name: String, pseudonym: Option[String])
case class Page(text: String)
case class Meta(isbn: String)

object Book {
object PageListRepr extends GraphRepr.Relation {
Expand All @@ -31,16 +32,23 @@ object Book {
type Labels = Witness.`"Book"`.T :: HNil
type Fields = HNil
type Outgoing = Witness.`'author`.Field[GraphRepr.Node.Optional[Author.AuthorRepr.type]] ::
Witness.`'pages` .Field[PageListRepr.type] :: HNil
Witness.`'pages` .Field[PageListRepr.type] ::
Witness.`'meta` .Field[Meta.MetaRepr.type] :: HNil

lazy val Labels: Labels = "Book".narrow :: HNil
lazy val Fields: Fields = HNil
lazy val Outgoing: Outgoing = 'author ->> GraphRepr.Node.Optional(Author.AuthorRepr) ::
'pages ->> PageListRepr :: HNil
'pages ->> PageListRepr ::
'meta ->> Meta.MetaRepr :: HNil

lazy val labels: List[String] = List("Book")
lazy val fields: Map[String, GraphRepr.Property] = Map("author" -> GraphRepr.Property[String])
lazy val outgoing: Map[String, GraphRepr.Relation] = Map("pages" -> PageListRepr)
lazy val fields: Map[String, GraphRepr.Property] = Map()
// TODO =========================================================================================================
lazy val outgoing: Map[String, GraphRepr.Relation] = ??? /*Map(
"author" -> GraphRepr.Node.Optional(Author.AuthorRepr),
"pages" -> PageListRepr,
"meta" -> Meta.MetaRepr
)*/
}

implicit def pagesSchema: Schema.Aux[List[Page], PageListRepr.type] = Schema.defineFor[List[Page]](PageListRepr)
Expand Down Expand Up @@ -79,4 +87,18 @@ object Page {
lazy val outgoing: Map[String, GraphRepr.Relation] = Map()
}
implicit def pageSchema: Schema.Aux[Page, PageRepr.type] = Schema.defineFor[Page](PageRepr)
}

object Meta {
object MetaRepr extends GraphRepr.Node {
type Labels = Witness.`"Meta"`.T :: HNil
type Fields = Witness.`'isbn`.Field[GraphRepr.Property.Aux[String]] :: HNil
type Outgoing = HNil
val Labels: Labels = "Meta".narrow :: HNil
val Fields: Fields = 'isbn ->> GraphRepr.Property[String] :: HNil
val Outgoing: Outgoing = HNil
val labels: List[String] = List("Meta")
val fields: Map[String, GraphRepr.Property] = Map("isbn" -> GraphRepr.Property[String])
val outgoing: Map[String, GraphRepr.Relation] = Map()
}
}

0 comments on commit ef35209

Please sign in to comment.