forked from digital-asset/daml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix DAML runtime for the new DAML-LF type Map (digital-asset#204)
* add a test for daml-lf/interface * fix scala code gen * fix extractor * fix navigator backend/frontend * key of Map are strings in proto/json
- Loading branch information
1 parent
aee5af8
commit 0785845
Showing
64 changed files
with
885 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
daml-lf/data/src/main/scala/com/digitalasset/daml/lf/data/SortedLookupList.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.digitalasset.daml.lf.data | ||
|
||
import scala.collection.immutable.HashMap | ||
|
||
/** We use this container to pass around DAML-LF maps as flat lists in various parts of the codebase. */ | ||
class SortedLookupList[+X] private (entries: ImmArray[(String, X)]) { | ||
|
||
def mapValue[Y](f: X => Y) = new SortedLookupList(entries.map { case (k, v) => k -> f(v) }) | ||
|
||
def toImmArray: ImmArray[(String, X)] = entries | ||
|
||
def keys: ImmArray[String] = entries.map(_._1) | ||
|
||
def values: ImmArray[X] = entries.map(_._2) | ||
|
||
def toHashMap: HashMap[String, X] = HashMap(entries.toSeq: _*) | ||
|
||
override def equals(obj: Any): Boolean = { | ||
obj match { | ||
case other: SortedLookupList[X] => other.toImmArray == entries | ||
case _ => false | ||
} | ||
} | ||
|
||
override def hashCode(): Int = entries.hashCode() | ||
|
||
override def toString: String = | ||
s"SortedMap(${entries.map { case (k, v) => k -> v }.toSeq.mkString(",")})" | ||
} | ||
|
||
object SortedLookupList { | ||
|
||
// Note: it's important that this ordering is the same as the DAML-LF ordering. | ||
private implicit val keyOrdering: Ordering[String] = UTF8.ordering | ||
|
||
def fromImmArray[X](entries: ImmArray[(String, X)]): Either[String, SortedLookupList[X]] = { | ||
entries.toSeq | ||
.groupBy(_._1) | ||
.collectFirst { | ||
case (k, l) if l.size > 1 => s"key $k duplicated when trying to build map" | ||
} | ||
.toLeft(new SortedLookupList(entries.toSeq.sortBy(_._1).toImmArray)) | ||
} | ||
|
||
def fromSortedImmArray[X](entries: ImmArray[(String, X)]): Either[String, SortedLookupList[X]] = { | ||
entries | ||
.map(_._1) | ||
.toSeq | ||
.sliding(2) | ||
.collectFirst { | ||
case Seq(k1, k2) if keyOrdering.gteq(k1, k2) => s"the list $entries is not sorted by key" | ||
} | ||
.toLeft(new SortedLookupList(entries)) | ||
} | ||
|
||
def apply[X](entries: Map[String, X]): SortedLookupList[X] = | ||
new SortedLookupList(ImmArray(entries.toSeq.sortBy(_._1))) | ||
|
||
def empty[X]: SortedLookupList[X] = new SortedLookupList(ImmArray.empty) | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
daml-lf/data/src/test/scala/com/digitalasset/daml/lf/data/SortedLookupListSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.digitalasset.daml.lf.data | ||
|
||
import org.scalatest.prop.PropertyChecks | ||
import org.scalatest.{Matchers, WordSpec} | ||
|
||
class SortedLookupListSpec extends WordSpec with Matchers with PropertyChecks { | ||
|
||
"SortMap.fromImmArray should fails if the input list contians duplicate keys" in { | ||
|
||
val negativeTestCases = Table( | ||
"list", | ||
ImmArray.empty[(String, Int)], | ||
ImmArray("1" -> 1), | ||
ImmArray("1" -> 1, "2" -> 2, "3" -> 3), | ||
ImmArray("2" -> 2, "3" -> 3, "1" -> 1)) | ||
|
||
val positiveTestCases = | ||
Table("list", ImmArray("1" -> 1, "1" -> 2), ImmArray("1" -> 1, "2" -> 2, "3" -> 3, "1" -> 2)) | ||
|
||
forAll(negativeTestCases)(l => SortedLookupList.fromImmArray(l) shouldBe 'right) | ||
|
||
forAll(positiveTestCases)(l => SortedLookupList.fromImmArray(l) shouldBe 'left) | ||
|
||
} | ||
|
||
"SortMap.fromSortedImmArray should fails if the input list is not sorted" in { | ||
|
||
val negativeTestCases = | ||
Table( | ||
"list", | ||
ImmArray.empty[(String, Int)], | ||
ImmArray("1" -> 1), | ||
ImmArray("1" -> 1, "2" -> 2, "3" -> 3)) | ||
|
||
val positiveTestCases = Table( | ||
"list", | ||
ImmArray("1" -> 1, "1" -> 2), | ||
ImmArray("1" -> 1, "2" -> 2, "3" -> 3, "1" -> 2), | ||
ImmArray("2" -> 2, "3" -> 3, "1" -> 1)) | ||
|
||
forAll(negativeTestCases)(l => SortedLookupList.fromSortedImmArray(l) shouldBe 'right) | ||
|
||
forAll(positiveTestCases)(l => SortedLookupList.fromSortedImmArray(l) shouldBe 'left) | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.