Skip to content

Commit

Permalink
Fix wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Jun 20, 2023
1 parent 43da8ec commit ab719e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import aqua.raw.ops.*
import aqua.raw.value.{CollectionRaw, LiteralRaw, MakeStructRaw, VarRaw}
import aqua.types.{CanonStreamType, OptionType, ScalarType, StreamType, StructType}
import cats.data.{NonEmptyList, NonEmptyMap}
import cats.syntax.show.*
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

Expand All @@ -25,15 +26,14 @@ class CollectionRawInlinerSpec extends AnyFlatSpec with Matchers {
val raw = CollectionRaw(NonEmptyList.of(makeStruct), OptionType(nestedType))

val (v, tree) =
RawValueInliner.valueToModel[InliningState](raw, false).run(InliningState()).value._2
RawValueInliner.valueToModel[InliningState](raw, false).runA(InliningState()).value

val resultValue = VarModel("option-inline-0", CanonStreamType(nestedType))

v shouldBe resultValue

tree.get.equalsOrShowDiff(
// create a stream
RestrictionModel("option-inline", StreamType(nestedType)).wrap(
val expected =
RestrictionModel("option-inline", StreamType(nestedType)).wrap( // create a stream
SeqModel.wrap(
// create an object
CallServiceModel(
Expand Down Expand Up @@ -61,8 +61,8 @@ class CollectionRawInlinerSpec extends AnyFlatSpec with Matchers {
).leaf
)
)
) shouldBe true

tree.get.equalsOrShowDiff(expected) shouldBe true
}

}
2 changes: 1 addition & 1 deletion model/raw/src/main/scala/aqua/raw/ops/RawTag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ sealed trait ParGroupTag extends GroupTag
case object SeqTag extends SeqGroupTag {

override def wrap(children: Tree*): Tree =
super.wrapNonEmpty(children.filterNot(_.head == EmptyTag).toList, RawTag.empty)
super.wrapNonEmpty(Chain.fromSeq(children).filterNot(_.head == EmptyTag), RawTag.empty)
}

case object ParTag extends ParGroupTag {
Expand Down
6 changes: 3 additions & 3 deletions model/src/main/scala/aqua/model/OpModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ sealed trait ParGroupModel extends GroupOpModel

case object SeqModel extends SeqGroupModel {

override def wrap(children: Tree*): Tree =
super.wrapNonEmpty(children.filterNot(_.head == EmptyModel).toList, EmptyModel.leaf)
override def wrap(children: Chain[Tree]): Tree =
super.wrapNonEmpty(children.filterNot(_.head == EmptyModel), EmptyModel.leaf)

// EmptyModel allowed – useful for tests
def wrapWithEmpty(children: Tree*): Tree =
super.wrapNonEmpty(children.toList, EmptyModel.leaf)
super.wrapNonEmpty(Chain.fromSeq(children), EmptyModel.leaf)

}

Expand Down
14 changes: 9 additions & 5 deletions model/tree/src/main/scala/aqua/tree/TreeNode.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aqua.tree

import cats.data.Chain
import cats.data.Chain.==:
import cats.free.Cofree
import cats.Eval

Expand All @@ -16,10 +17,13 @@ trait TreeNode[T <: TreeNode[T]] {

def wrap(children: Chain[Tree]): Tree = Cofree(self, Eval.now(children))

protected def wrapNonEmpty(children: List[Tree], empty: Tree): Tree = children match {
case Nil => empty
case x :: Nil => x
case _ => Cofree(self, Eval.now(Chain.fromSeq(children)))
}
protected def wrapNonEmpty(children: Chain[Tree], empty: Tree): Tree =
children match {
case Chain.nil => empty
case x ==: Chain.nil => x
// Do not use `wrap` here as children
// could redefine `wrap` through this method
case _ => Cofree(self, Eval.now(children))
}

}

0 comments on commit ab719e3

Please sign in to comment.