Skip to content

Add type safe projection #271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion gremlin-scala/src/main/scala/gremlin/scala/GremlinScala.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import java.util.{
Map => JMap,
Collection => JCollection,
Iterator => JIterator,
Set => JSet
Set => JSet,
UUID
}
import java.util.stream.{Stream => JStream}

Expand All @@ -32,7 +33,9 @@ import org.apache.tinkerpop.gremlin.process.traversal.{Bytecode, Path, Scope, Tr
import org.apache.tinkerpop.gremlin.structure.{Direction, T}
import shapeless.{::, HList, HNil}
import shapeless.ops.hlist.{IsHCons, Mapper, Prepend, RightFolder, ToTraversable, Tupler}
import shapeless.ops.tuple.{Prepend => TuplePrepend}
import shapeless.ops.product.ToHList
import shapeless.syntax.std.tuple._
import scala.concurrent.duration.FiniteDuration
import scala.reflect.runtime.{universe => ru}
import scala.collection.{immutable, mutable}
Expand Down Expand Up @@ -116,6 +119,10 @@ class GremlinScala[End](val traversal: GraphTraversal[_, End]) {
otherProjectKeys: String*): GremlinScala.Aux[JMap[String, A], Labels] =
GremlinScala[JMap[String, A], Labels](traversal.project(projectKey, otherProjectKeys: _*))

def project[H <: Product](
builder: ProjectionBuilder[Nil.type] ⇒ ProjectionBuilder[H]): GremlinScala[H] =
builder(ProjectionBuilder()).build(this)

/** You might think that predicate should be `GremlinScala[End] => GremlinScala[Boolean]`,
* but that's not how tp3 works: e.g. `.value(Age).is(30)` returns `30`, not `true`
*/
Expand Down Expand Up @@ -1027,3 +1034,28 @@ class GremlinScala[End](val traversal: GraphTraversal[_, End]) {
travs.map(_.apply(start).traversal)

}

class ProjectionBuilder[T <: Product] private[gremlin] (
labels: Seq[String],
addBy: GraphTraversal[_, JMap[String, Any]] ⇒ GraphTraversal[_, JMap[String, Any]],
buildResult: JMap[String, Any] ⇒ T) {

def apply[U, TR <: Product](by: By[U])(
implicit prepend: TuplePrepend.Aux[T, Tuple1[U], TR]): ProjectionBuilder[TR] = {
val label = UUID.randomUUID().toString
new ProjectionBuilder[TR](labels :+ label,
addBy.andThen(by.apply),
map ⇒ buildResult(map) :+ map.get(label).asInstanceOf[U])
}

def and[U, TR <: Product](by: By[U])
(implicit prepend: TuplePrepend.Aux[T, Tuple1[U], TR]): ProjectionBuilder[TR] = apply(by)

private[gremlin] def build(g: GremlinScala[_]): GremlinScala[T] = {
GremlinScala(addBy(g.traversal.project(labels.head, labels.tail: _*))).map(buildResult)
}
}

object ProjectionBuilder {
def apply() = new ProjectionBuilder[Nil.type](Nil, scala.Predef.identity, _ ⇒ Nil)
}
37 changes: 37 additions & 0 deletions gremlin-scala/src/test/scala/gremlin/scala/ProjectSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gremlin.scala

import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory
import org.scalatest.{Matchers, WordSpec}

class ProjectSpec extends WordSpec with Matchers {
def graph: ScalaGraph = TinkerFactory.createModern.asScala()

"project steps" should {
"provide type safe result" in {
val result = graph
.V()
.out("created")
.project(_(By(Key[String]("name")))
.and(By(__.in("created").count())))
.toList()

result shouldBe List(
("lop", 3),
("lop",3),
("lop",3),
("ripple", 1)
)
}

"provide other type safe result" in {
val result = graph
.V()
.has(Key("name").of("marko"))
.project(_(By(__.outE().count()))
.and(By(__.inE().count())))
.head()

result shouldBe (3, 0)
}
}
}