Skip to content

Commit cc872d5

Browse files
committed
Merge pull request #154 from prezi/reintroduce-transitives
Kotlin enum support
2 parents 7c4dba0 + cac293e commit cc872d5

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

spaghetti-kotlin-support/src/main/groovy/com/prezi/spaghetti/kotlin/KotlinEnumGeneratorVisitor.groovy

+28-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ class KotlinEnumGeneratorVisitor extends AbstractKotlinGeneratorVisitor {
1717
"""class ${enumName} {
1818
class object {
1919
${values.join("\n")}
20+
private val _values = arrayListOf(${node.values.join(", ")})
21+
private val _names = arrayListOf(${node.values.collect { "\"${it}\"" }.join(", ")})
22+
23+
fun names():Array<String> = _names.copyToArray()
24+
25+
fun values():Array<${enumName}> = _values.copyToArray()
26+
27+
fun getName(value:${enumName}):String = _names.get(value as Int)
28+
29+
fun getValue(value:${enumName}):Int = value as Int
30+
31+
fun fromValue(value:Int):${enumName} {
32+
if (value < 0 || value >= _values.size) {
33+
throw IllegalArgumentException("Invalid value for ${enumName}: " + value)
34+
}
35+
return _values[value]
36+
}
37+
38+
fun valueOf(name:String):${enumName} {
39+
var result:MyEnum
40+
when(name)
41+
{
42+
${node.values.collect { " \"${it}\" -> result = ${it}" }.join("\n")}
43+
else -> throw IllegalArgumentException("Invalid name for ${enumName}: " + name)
44+
}
45+
return result
46+
}
2047
}
2148
}
2249
"""
@@ -33,7 +60,7 @@ ${values.join("\n")}
3360

3461
@Override
3562
String visitEnumValueNode(EnumValueNode node) {
36-
return "\t\tval ${node.name} = ${index} as ${enumName}"
63+
return " val ${node.name} = ${index} as ${enumName}"
3764
}
3865
}
3966
}

spaghetti-kotlin-support/src/test/groovy/com/prezi/spaghetti/kotlin/KotlinEnumGeneratorVisitorTest.groovy

+29
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ class KotlinEnumGeneratorVisitorTest extends AstTestBase {
3232
[deprecated("escape \\"this\\"!")]
3333
val BELA = 1 as MyEnum
3434
val GEZA = 2 as MyEnum
35+
private val _values = arrayListOf(ALMA, BELA, GEZA)
36+
private val _names = arrayListOf("ALMA", "BELA", "GEZA")
37+
38+
fun names():Array<String> = _names.copyToArray()
39+
40+
fun values():Array<MyEnum> = _values.copyToArray()
41+
42+
fun getName(value:MyEnum):String = _names.get(value as Int)
43+
44+
fun getValue(value:MyEnum):Int = value as Int
45+
46+
fun fromValue(value:Int):MyEnum {
47+
if (value < 0 || value >= _values.size) {
48+
throw IllegalArgumentException("Invalid value for MyEnum: " + value)
49+
}
50+
return _values[value]
51+
}
52+
53+
fun valueOf(name:String):MyEnum {
54+
var result:MyEnum
55+
when(name)
56+
{
57+
"ALMA" -> result = ALMA
58+
"BELA" -> result = BELA
59+
"GEZA" -> result = GEZA
60+
else -> throw IllegalArgumentException("Invalid name for MyEnum: " + name)
61+
}
62+
return result
63+
}
3564
}
3665
}
3766
"""

0 commit comments

Comments
 (0)