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.
Support generic types as contract keys in codegen (digital-asset#1743)
Fixes digital-asset#1728
- Loading branch information
1 parent
a15d9e2
commit 02e9503
Showing
10 changed files
with
183 additions
and
24 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
81 changes: 81 additions & 0 deletions
81
language-support/java/codegen/src/it/daml/Tests/ContractKeys.daml
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,81 @@ | ||
-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
|
||
daml 1.2 | ||
module Tests.ContractKeys where | ||
|
||
data PartyAndInt = | ||
PartyAndInt | ||
with | ||
party: Party | ||
int: Int | ||
|
||
template NoKey | ||
with | ||
owner : Party | ||
where | ||
signatory owner | ||
|
||
controller owner can | ||
NoKey_Choice : () | ||
do return () | ||
|
||
template PartyKey | ||
with | ||
owner : Party | ||
where | ||
signatory owner | ||
|
||
key owner: Party | ||
maintainer key | ||
|
||
controller owner can | ||
PartyKey_Choice : () | ||
do return () | ||
|
||
template RecordKey | ||
with | ||
owner : Party | ||
number : Int | ||
where | ||
signatory owner | ||
|
||
key PartyAndInt owner number : PartyAndInt | ||
maintainer key.party | ||
|
||
controller owner can | ||
RecordKey_Choice : () | ||
do return () | ||
|
||
template TupleKey | ||
with | ||
owner : Party | ||
number : Int | ||
where | ||
signatory owner | ||
|
||
key (owner, number): (Party, Int) | ||
maintainer key._1 | ||
|
||
controller owner can | ||
TupleKey_Choice : () | ||
do return () | ||
|
||
template NestedTupleKey | ||
with | ||
t1_1 : Party | ||
t1_2 : Int | ||
t1_3 : Text | ||
t2_1 : Int | ||
t2_2 : Bool | ||
t2_3 : Text | ||
t2_4 : Int | ||
where | ||
signatory t1_1 | ||
|
||
key ((t1_1, t1_2, t1_3), (t2_1, t2_2, t2_3, t2_4)): ((Party, Int, Text), (Int, Bool, Text, Int)) | ||
maintainer key._1._1 | ||
|
||
controller t1_1 can | ||
NestedTupleKey_Choice : () | ||
do return () |
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
71 changes: 71 additions & 0 deletions
71
...-support/java/codegen/src/it/java-latest/com/digitalasset/lf_latest/ContractKeysTest.java
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,71 @@ | ||
// Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.digitalasset.lf_latest; | ||
|
||
import da.types.Tuple2; | ||
import da.types.Tuple3; | ||
import da.types.Tuple4; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.commons.util.ReflectionUtils; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
import tests.contractkeys.*; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class ContractKeysTest { | ||
|
||
// NOTE: the tests are mostly here to make sure the code compiles | ||
|
||
NoKey.Contract noKey = new NoKey.Contract(new NoKey.ContractId("no-key"), new NoKey("Alice"), Optional.empty()); | ||
PartyKey.Contract partyKey = new PartyKey.Contract(new PartyKey.ContractId("party-key"), new PartyKey("Alice"), Optional.empty(), Optional.of("Alice")); | ||
RecordKey.Contract recordKey = new RecordKey.Contract(new RecordKey.ContractId("record-key"), new RecordKey("Alice", 42L), Optional.empty(), Optional.of(new PartyAndInt("Alice", 42L))); | ||
TupleKey.Contract tupleKey = new TupleKey.Contract(new TupleKey.ContractId("tuple-key"), new TupleKey("Alice", 42L), Optional.empty(), Optional.of(new Tuple2<>("Alice", 42L))); | ||
NestedTupleKey.Contract nestedTupleKey = new NestedTupleKey.Contract(new NestedTupleKey.ContractId("nested-tuple-key"), new NestedTupleKey("Alice", 42L, "blah", 47L, true, "foobar", 0L), Optional.empty(), Optional.of(new Tuple2<>(new Tuple3<>("Alice", 42L, "blah"), new Tuple4<>(47L, true, "foobar", 0L)))); | ||
|
||
@Test | ||
void noKeyHasNoKey() { | ||
assertFalse(ReflectionUtils.readFieldValue(NoKey.Contract.class, "key", noKey).isPresent()); | ||
} | ||
|
||
@Test | ||
void allOthersHaveKeys() { | ||
assertTrue(partyKey.key.isPresent()); | ||
assertTrue(recordKey.key.isPresent()); | ||
assertTrue(tupleKey.key.isPresent()); | ||
assertTrue(nestedTupleKey.key.isPresent()); | ||
} | ||
|
||
@Test | ||
void partyKeyIsString() { | ||
assertEquals(partyKey.key.get(), "Alice"); | ||
} | ||
|
||
@Test | ||
void recordKeyIsPartyAndInt() { | ||
assertEquals(recordKey.key.get().party, "Alice"); | ||
assertEquals(recordKey.key.get().int$.longValue(), 42L); | ||
} | ||
|
||
@Test | ||
void tupleKeyIsPartyAndInt() { | ||
assertEquals(tupleKey.key.get()._1, "Alice"); | ||
assertEquals(tupleKey.key.get()._2.longValue(), 42L); | ||
} | ||
|
||
@Test | ||
void nestedTupleKeyIsPartyAndInt() { | ||
assertEquals(nestedTupleKey.key.get()._1._1, "Alice"); | ||
assertEquals(nestedTupleKey.key.get()._1._2.longValue(), 42L); | ||
assertEquals(nestedTupleKey.key.get()._1._3, "blah"); | ||
assertEquals(nestedTupleKey.key.get()._2._1.longValue(), 47L); | ||
assertEquals(nestedTupleKey.key.get()._2._2.booleanValue(), true); | ||
assertEquals(nestedTupleKey.key.get()._2._3, "foobar"); | ||
assertEquals(nestedTupleKey.key.get()._2._4.longValue(), 0L); | ||
} | ||
|
||
} |
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