Skip to content

Commit f02111f

Browse files
Make SemVer sortable
1 parent dbea286 commit f02111f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

common/scala/src/main/scala/org/apache/openwhisk/core/entity/SemVer.scala

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import spray.json.deserializationError
2121
import spray.json.JsString
2222
import spray.json.JsValue
2323
import spray.json.RootJsonFormat
24+
25+
import scala.annotation.tailrec
2426
import scala.util.Try
2527

2628
/**
@@ -34,7 +36,7 @@ import scala.util.Try
3436
*
3537
* @param (major, minor, patch) for the semantic version
3638
*/
37-
protected[core] class SemVer private (private val version: (Int, Int, Int)) extends AnyVal {
39+
protected[core] class SemVer private (private val version: (Int, Int, Int)) extends AnyVal with Ordered[SemVer] {
3840

3941
protected[core] def major = version._1
4042
protected[core] def minor = version._2
@@ -46,6 +48,24 @@ protected[core] class SemVer private (private val version: (Int, Int, Int)) exte
4648

4749
protected[entity] def toJson = JsString(toString)
4850
override def toString = s"$major.$minor.$patch"
51+
52+
protected[core] def versionList = Seq(major, minor, patch)
53+
54+
override def compare(that: SemVer): Int = {
55+
compareVersion(that)
56+
}
57+
58+
@tailrec
59+
private def compareVersion(that: SemVer, depth: Int = 0): Int = {
60+
require(depth >= 0 && depth <= 2, "depth exceed the limit of 0 to 2")
61+
val result = versionList(depth) - that.versionList(depth)
62+
if (result != 0)
63+
result
64+
else if (depth == 2)
65+
0
66+
else
67+
compareVersion(that, depth + 1)
68+
}
4969
}
5070

5171
protected[core] object SemVer {

common/scala/src/main/scala/org/apache/openwhisk/core/entity/WhiskAction.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ case class WhiskActionVersionList(namespace: EntityPath, name: EntityName, versi
370370
case Some(ver) =>
371371
versions.get(ver).map(DocId(_))
372372
case None if versions.nonEmpty =>
373-
Some(DocId(versions.maxBy(_._1.toString)._2))
373+
Some(DocId(versions.maxBy(_._1)._2))
374374
case _ =>
375375
None
376376
}

0 commit comments

Comments
 (0)