Skip to content

Commit

Permalink
update test/readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier committed Sep 8, 2018
1 parent c2ac282 commit 09f3c1d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ g.V.has(age).order(By(age, Order.decr))
More working examples in [TraversalSpec](https://github.com/mpollmeier/gremlin-scala/blob/master/gremlin-scala/src/test/scala/gremlin/scala/TraversalSpec.scala).

### Mapping vertices from/to case classes
You can save and load case classes as a vertex - implemented with a [blackbox macro](http://docs.scala-lang.org/overviews/macros/blackbox-whitebox.html). You can optionally annotate the id and label of your case class. Scala's `Option` types will be automatically unwrapped, i.e. a `Some[A]` will be stored as the value of type `A` in the database, or `null` if it's `None`. If we wouldn't unwrap it, the database would have to understand Scala's Option type itself. The same goes for value classes, i.e. a `case class ShoeSize(value: Int) extends AnyVal` will be stored as an Integer. Note: your classes must be defined outside the scope where they are being used (e.g. in the code below the class `Example` cannot be inside `object Main`).
You can save and load case classes as a vertex - implemented with a [blackbox macro](http://docs.scala-lang.org/overviews/macros/blackbox-whitebox.html). You can optionally annotate the id and label of your case class. Scala's `Option` types will be automatically unwrapped, i.e. a `Some[A]` will be stored as the value of type `A` in the database, or `null` if it's `None`. If we wouldn't unwrap it, the database would have to understand Scala's Option type itself. The same goes for value classes, i.e. a `case class ShoeSize(value: Int) extends AnyVal` will be stored as an Integer.

Note: your classes must be defined outside the scope where they are being used (e.g. in the code below the class `Example` cannot be inside `object Main`).

Note: you cannot specify the id when adding a vertex like this. Using `@id` only works when retrieving the vertex back from the graph.

```scala
// this does _not_ work in a REPL
Expand Down
20 changes: 9 additions & 11 deletions gremlin-scala/src/test/scala/gremlin/scala/MarshallableSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ case class CCWithOptionIdNested(s: String, @id id: Option[Int], i: MyValueClass)
case class CCWithLabel(s: String)

@label("the_label")
case class CCWithLabelAndId(
case class ComplexCC(
s: String,
@id id: Int,
l: Long,
o: Option[String],
seq: Seq[String],
Expand Down Expand Up @@ -125,26 +124,25 @@ class MarshallableSpec extends WordSpec with Matchers {
}

"use @label and @id annotations" in new Fixture {
val ccWithLabelAndId = CCWithLabelAndId(
val cc = ComplexCC(
"some string",
id = Int.MaxValue,
Long.MaxValue,
Some("option type"),
Seq("test1", "test2"),
Map("key1" -> "value1", "key2" -> "value2"),
NestedClass("nested")
)

val v = graph + ccWithLabelAndId
val v = graph + cc

val vl = graph.V(v.id).head
vl.label shouldBe "the_label"
vl.valueMap should contain("s" -> ccWithLabelAndId.s)
vl.valueMap should contain("l" -> ccWithLabelAndId.l)
vl.valueMap should contain("o" -> ccWithLabelAndId.o.get)
vl.valueMap should contain("seq" -> ccWithLabelAndId.seq)
vl.valueMap should contain("map" -> ccWithLabelAndId.map)
vl.valueMap should contain("nested" -> ccWithLabelAndId.nested)
vl.valueMap should contain("s" -> cc.s)
vl.valueMap should contain("l" -> cc.l)
vl.valueMap should contain("o" -> cc.o.get)
vl.valueMap should contain("seq" -> cc.seq)
vl.valueMap should contain("map" -> cc.map)
vl.valueMap should contain("nested" -> cc.nested)
}

"have an Option @id annotation" in new Fixture {
Expand Down
1 change: 0 additions & 1 deletion version.sbt

This file was deleted.

0 comments on commit 09f3c1d

Please sign in to comment.