Skip to content

Commit

Permalink
update behaviour for undefined multi property based on joern's behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier committed Mar 22, 2024
1 parent 72c2b09 commit 293d717
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
4 changes: 1 addition & 3 deletions core/src/main/scala/flatgraph/Accessors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ object Accessors {
schema.getNodePropertyFormalQuantity(nodeKind, propertyKind) match {
case FormalQtyType.QtyNone => None
case FormalQtyType.QtyOne | FormalQtyType.QtyOption => getNodePropertyOption[Object](graph, nodeKind, propertyKind, seq)
case FormalQtyType.QtyMulti =>
val multiPropertyValue = getNodePropertyMulti[Object](graph, nodeKind, propertyKind, seq)
Option(multiPropertyValue).filter(_.nonEmpty)
case FormalQtyType.QtyMulti => Option(getNodePropertyMulti[Object](graph, nodeKind, propertyKind, seq))
}
}

Expand Down
6 changes: 4 additions & 2 deletions core/src/main/scala/flatgraph/traversal/Language.scala
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,10 @@ class NodeMethods(node: GNode) extends AnyVal {
def propertyOption[ValueType](propertyKey: OptionalPropertyKey[ValueType]): Option[ValueType] =
Accessors.getNodePropertyOption(node.graph, node.nodeKind, propertyKey.kind, node.seq())

def propertyOption[ValueType](propertyKey: MultiPropertyKey[ValueType]): IndexedSeq[ValueType] =
Accessors.getNodePropertyMulti(node.graph, node.nodeKind, propertyKey.kind, node.seq())
// TODO this should rather return `None` for an undefined property, rather than `Some(Seq.empty)`, but we only want
// to make that change after joern's transition to flatgraph - see https://github.com/joernio/joern/pull/4382
def propertyOption[ValueType](propertyKey: MultiPropertyKey[ValueType]): Option[IndexedSeq[ValueType]] =
Option(Accessors.getNodePropertyMulti(node.graph, node.nodeKind, propertyKey.kind, node.seq()))

def propertyOption[ValueType](name: String): Option[ValueType] = {
node.graph.schema.getPropertyKindByName(name) match {
Expand Down
21 changes: 12 additions & 9 deletions core/src/test/scala/flatgraph/GraphTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -752,17 +752,20 @@ class GraphTests extends AnyWordSpec with Matchers {

// test cases where the property is not defined
v0.property(propertySingle) shouldBe "propertySingleDefault"
v0.property(propertyOptional) shouldBe None
v0.property(propertyMulti) shouldBe Seq.empty
v0.propertyOption(propertySingle) shouldBe Some("propertySingleDefault")
v0.property(propertyOptional) shouldBe None
v0.propertyOption(propertyOptional) shouldBe None
v0.propertyOption(propertyMulti) shouldBe Seq.empty
v0.property(propertyMulti) shouldBe Seq.empty
// TODO this should rather return `None` for an undefined property, rather than `Some(Seq.empty)`, but we only want
// to make that change after joern's transition to flatgraph - see https://github.com/joernio/joern/pull/4382
// v0.propertyOption(propertyMulti) shouldBe None
v0.propertyOption(propertyMulti) shouldBe Some(Seq.empty[String])

// ensure odb compat accessors work just like before
// see https://github.com/ShiftLeftSecurity/overflowdb/blob/1b5df058569a3e87192f2dce4e019e3b4a0c1d91/core-tests/src/test/java/overflowdb/ElementTest.java#L54-L67
// see https://github.com/joernio/joern/pull/4382
Accessors.getNodePropertyOptionCompat(v0, 0) shouldBe None
Accessors.getNodePropertyOptionCompat(v0, 1) shouldBe None
Accessors.getNodePropertyOptionCompat(v0, 2) shouldBe None
Accessors.getNodePropertyOptionCompat(v0, 2) shouldBe Some(Seq.empty)

DiffGraphApplier.applyDiff(
g,
Expand All @@ -780,14 +783,14 @@ class GraphTests extends AnyWordSpec with Matchers {

// test cases where the property is defined
v0.property(propertySingle) shouldBe "a"
v0.property(propertyOptional) shouldBe Some("b")
v0.property(propertyMulti) shouldBe Seq("c", "d")
v0.propertyOption(propertySingle) shouldBe Some("a")
v0.property(propertyOptional) shouldBe Some("b")
v0.propertyOption(propertyOptional) shouldBe Some("b")
v0.propertyOption(propertyMulti) shouldBe Seq("c", "d")
v0.property(propertyMulti) shouldBe Seq("c", "d")
v0.propertyOption(propertyMulti) shouldBe Some(Seq("c", "d"))

// ensure odb compat accessors work just like before
// see https://github.com/ShiftLeftSecurity/overflowdb/blob/1b5df058569a3e87192f2dce4e019e3b4a0c1d91/core-tests/src/test/java/overflowdb/ElementTest.java#L54-L67
// see https://github.com/joernio/joern/pull/4382
Accessors.getNodePropertyOptionCompat(v0, 0) shouldBe Some("a")
Accessors.getNodePropertyOptionCompat(v0, 1) shouldBe Some("b")
Accessors.getNodePropertyOptionCompat(v0, 2) shouldBe Some(Seq("c", "d"))
Expand Down

0 comments on commit 293d717

Please sign in to comment.