Repository: Microsoft/thrifty (kotlin)

Issue:
Const refs list should be `distinct()`
We currently allow for the same Constant to appear in `referencesConstants` more than once.  We should either make this member a `Set` or use `distinct()` when building it.  Having duplicates will probably break the topo-sort algorithm.

Pull request:
Fixes #504

Patch:
diff --git a/thrifty-schema/src/main/kotlin/com/microsoft/thrifty/schema/Constant.kt b/thrifty-schema/src/main/kotlin/com/microsoft/thrifty/schema/Constant.kt
index 3f7582b71..7000cd7c9 100644
--- a/thrifty-schema/src/main/kotlin/com/microsoft/thrifty/schema/Constant.kt
+++ b/thrifty-schema/src/main/kotlin/com/microsoft/thrifty/schema/Constant.kt
@@ -68,7 +68,7 @@ class Constant private constructor (
     }
 
     internal fun linkReferencedConstants(linker: Linker) {
-        referencedConstants = type.accept(ConstantReferenceVisitor(value, linker))
+        referencedConstants = type.accept(ConstantReferenceVisitor(value, linker)).distinct()
     }
 
     internal fun validate(linker: Linker) {


Test patch:
diff --git a/thrifty-schema/src/test/kotlin/com/microsoft/thrifty/schema/LoaderTest.kt b/thrifty-schema/src/test/kotlin/com/microsoft/thrifty/schema/LoaderTest.kt
index 897f10007..bca5b6e88 100644
--- a/thrifty-schema/src/test/kotlin/com/microsoft/thrifty/schema/LoaderTest.kt
+++ b/thrifty-schema/src/test/kotlin/com/microsoft/thrifty/schema/LoaderTest.kt
@@ -1278,6 +1278,20 @@ class LoaderTest {
         (singleValue as IdentifierValueElement).value shouldBe "THE_EXAMPLE"
     }
 
+    @Test
+    fun `struct references are deduplicated`() {
+        val thrift = """
+            |const string STR = "foo"
+            |
+            |const list<string> STRS = [STR, STR]
+        """.trimMargin()
+
+        val schema = load(thrift)
+        val (str, strs) = schema.constants
+
+        strs.referencedConstants shouldBe listOf(str)
+    }
+
     private fun load(thrift: String): Schema {
         val f = File.createTempFile("test", ".thrift", tempDir)
         f.writeText(thrift)