Skip to content
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

[Backport 1.16] fix(builtin-function): fix string conversion of years-months-durations #644

Merged
merged 2 commits into from
May 26, 2023
Merged
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@
<differenceType>7004</differenceType>
<method>* $anonfun*(*)</method>
</ignored>
<ignored>
<className>org/camunda/feel/**</className>
<differenceType>7002</differenceType>
<method>* $anonfun*(*)</method>
</ignored>
<ignored>
<!-- allow new methods in the interface-->
<className>org/camunda/feel/**</className>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ object ConversionBuiltinFunctions {
.replaceAll("$1$3")
ValString(dateTimeWithOffsetOrZoneId)
}
case List(ValYearMonthDuration(from)) => ValString(from.toString)
case List(duration: ValYearMonthDuration) => ValString(duration.toString)
case List(duration: ValDayTimeDuration) => ValString(duration.toString)
}
)
Expand Down
22 changes: 22 additions & 0 deletions src/main/scala/org/camunda/feel/syntaxtree/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ case class ValDateTime(value: DateTime) extends Val {
}

case class ValYearMonthDuration(value: YearMonthDuration) extends Val {

override def toString: String = {
def makeString(sign: String, year: Long, month: Long): String = {
val y = Option(year).filterNot(_ == 0).map(_ + "Y").getOrElse("")
val m = Option(month).filterNot(_ == 0).map(_ + "M").getOrElse("")

val stringBuilder = new StringBuilder("")
stringBuilder.append(sign).append("P").append(y).append(m)
stringBuilder.toString()
}

val year = value.getYears
val month = value.getMonths % 12

if (year == 0 && month == 0)
"P0Y"
else if (year <= 0 && month <= 0)
makeString("-", -year, -month)
else
makeString("", year, month)
}

override val properties: Map[String, Val] = Map(
"years" -> ValNumber(value.getYears),
"months" -> ValNumber(value.getMonths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ class BuiltinConversionFunctionsTest
eval(""" string(@"P1DT2H3M4S") """) should be(ValString("P1DT2H3M4S"))
}

it should "convert zero-length years-months-duration" in {

eval(""" string(@"P0Y") """) should be(ValString("P0Y"))
eval(""" string(@"-P0M") """) should be(ValString("P0Y"))
eval(""" string(@"P0Y0M") """) should be(ValString("P0Y"))
}
it should "convert negative years-months-duration" in {

eval(""" string(@"-P1Y") """) should be(ValString("-P1Y"))
eval(""" string(@"-P5M") """) should be(ValString("-P5M"))
eval(""" string(@"-P3Y1M") """) should be(ValString("-P3Y1M"))
}
it should "convert years-months-duration" in {

eval(""" string(@"P1Y") """) should be(ValString("P1Y"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class DateTimeDurationPropertiesTest

result shouldBe a[ValError]
result.asInstanceOf[ValError].error should startWith(
"No property found with name 'x' of value 'ValYearMonthDuration(P2Y3M)'. Available properties:")
"No property found with name 'x' of value 'P2Y3M'. Available properties:")
}

it should "has properties with @-notation" in {
Expand Down