Skip to content

Commit

Permalink
Review First: SDK Update (#149)
Browse files Browse the repository at this point in the history
* update Dict.update function to take an alter function

* update Aggregate.scala with missing functions and mismatched types

* update sdk functions

* align sdk.Regex more closely with elm\/regex

* - Revert Result.map3 param signature to `fn: (A, B, C) => V`
- change counter implementation for Regex.find

* added unit test for Regex.find and Regex.split
  • Loading branch information
nwokafor-choongsaeng authored Aug 31, 2023
1 parent a2f86e9 commit d250c91
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rewrite.rules = [RedundantBraces]

project.excludeFilters = []
# Defaults
project.excludePaths = ["glob:**/ir/src-2.12/morphir/ir/**.scala", "glob:**/ir/src-2.13/morphir/ir/**.scala", "glob:**/ir/src/morphir/ir/**.scala" ,"glob:**/DAG.scala"]
project.excludePaths = ["glob:**/ir/src-2.12/morphir/ir/**.scala", "glob:**/ir/src-2.13/morphir/ir/**.scala", "glob:**/ir/src/morphir/ir/**.scala" ,"glob:**/ir/src/morphir/dependency/**.scala"]


rewrite.redundantBraces.generalExpressions = false
Expand Down
2 changes: 2 additions & 0 deletions morphir/sdk/core/src/morphir/sdk/Decimal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ object Decimal extends DecimalModuleCompat {
//TODO: Make sure the Elm call and this call return the same value
def toString(value: Decimal): morphir.sdk.String.String = value.toString

def _toString: Decimal => String = toString

def truncate(decimal: Decimal): Decimal =
decimal.setScale(0, BigDecimal.RoundingMode.DOWN)

Expand Down
31 changes: 30 additions & 1 deletion morphir/sdk/core/src/morphir/sdk/Regex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,47 @@ package morphir.sdk

import morphir.sdk.Maybe.Maybe

import scala.util.Try
import scala.util.matching.{ Regex => RE }

object Regex {

case class Regex(toRE: RE) extends AnyVal
case class Options(caseInsensitive: Boolean, multiline: Boolean)
case class Match(
`match`: String,
_match: String,
index: Int,
number: Int,
submatches: List[Maybe[String]]
)

val never: Regex = Regex(".^".r)

def fromString(string: String): Maybe[Regex] =
Try(string.r).toOption.map(Regex.apply)

def split(regex: Regex)(string: String): List[String] =
regex.toRE.split(string).toList

/** Find matches in a string:
*
* val location: Regex.Regex = Maybe.withDefault(never)(fromString "[oi]n a (\\w+)")
*
* val places : List[Regex.Match places = Regex.find location "I am on a boat in a lake."
*
* // places.map(_.match) == List( "on a boat", "in a lake" ) // places.map(_.submatches) == List( List(Just "boat"),
* List(Just "lake") )
*
* > Note: .submatches will always return an empty list
*/
def find(regex: Regex)(str: String): List[Match] =
regex.toRE.findAllMatchIn(str).toList.zipWithIndex.map { case (mtch, idx) =>
Match(
_match = mtch.matched,
index = mtch.start,
number = idx + 1,
submatches = mtch.subgroups.map(Maybe.Just(_))
)
}

}
24 changes: 24 additions & 0 deletions morphir/sdk/core/src/morphir/sdk/ResultList.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package morphir.sdk

object ResultList {

type ResultList[E, A] = List.List[Result.Result[E, A]]

/** Turn a list of results into a single result keeping all errors.
*/
def keepAllErrors[E, A](results: ResultList[E, A]): Result.Result[List[E], List[A]] = {
val oks: List[A] = List.filterMap(Result.toMaybe[E, A])(results)
val errs: List[E] = List.filterMap((result: Result[E, A]) =>
result match {
case Result.Err(e) => Maybe.Just(e)
case Result.Ok(_) => Maybe.Nothing
}
)(results)

errs match {
case Nil => Result.Ok(oks)
case _ => Result.Err(errs)
}
}

}
48 changes: 48 additions & 0 deletions morphir/sdk/core/test/src/morphir/sdk/RegexSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package morphir.sdk

import zio.test._

object RegexSpec extends DefaultRunnableSpec {

val splitPattern: Regex.Regex =
Maybe.withDefault(Regex.never)(Regex.fromString("""[^\w\s]+"""))

val splitScenarios = Seq(
("morphir.sdk", List("morphir", "sdk")),
("morphir:sdk", List("morphir", "sdk"))
)

val wordPattern: Regex.Regex =
Maybe.withDefault(Regex.never)(Regex.fromString("""([a-zA-Z][a-z]*|[0-9]+)"""))

val findScenarios = Seq(
("Morphir", List("Morphir")),
("SDK", List("S", "D", "K")),
("camelCaseName", List("camel", "Case", "Name"))
)

def spec = suite("Regex Spec")(
suite(s"""Regex.split with $splitPattern pattern""")(
splitScenarios.map { case (testString, expectedOutput) =>
test(s"""split("$testString") == $expectedOutput""") {
assert(Regex.split(splitPattern)(testString))(
Assertion.equalTo(expectedOutput)
)
}
}: _*
),
suite(s"""Regex.find with $wordPattern pattern""")(
findScenarios.map { case (testString, expectedOutput) =>
test(s"""find("$testString").map(_.match) == $expectedOutput""") {
assert(
Regex
.find(wordPattern)(testString)
.map(_._match)
)(
Assertion.equalTo(expectedOutput)
)
}
}: _*
)
)
}

0 comments on commit d250c91

Please sign in to comment.