Skip to content

Commit dbde6d3

Browse files
committed
Dev: add support enum with associated value
1 parent 9dae0aa commit dbde6d3

File tree

7 files changed

+276
-13
lines changed

7 files changed

+276
-13
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111

1212
dependencies {
1313
classpath 'com.android.tools.build:gradle:7.4.2'
14-
classpath "com.readdle.android.swift:gradle:1.4.4"
14+
classpath "com.readdle.android.swift:gradle:1.4.5"
1515
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1616
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
1717
classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'

compiler/src/main/java/com/readdle/codegen/SwiftValueDescriptor.java

-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ class SwiftValueDescriptor {
6767
enclosingElement = enclosingElement.getEnclosingElement();
6868
}
6969

70-
71-
// Check if it's an abstract class
72-
if (classElement.getModifiers().contains(Modifier.ABSTRACT)) {
73-
throw new IllegalArgumentException(String.format("The class %s is abstract. You can't annotate abstract classes with @%s",
74-
classElement.getQualifiedName().toString(), SwiftValue.class.getSimpleName()));
75-
}
76-
7770
boolean hasEmptyConstructor = false;
7871

7972
// Check if an empty constructor is given
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package com.readdle.swiftjava.sample
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import com.readdle.codegen.anotation.JavaSwift
5+
import org.junit.Assert
6+
import org.junit.Before
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
10+
@RunWith(AndroidJUnit4::class)
11+
class SampleEnumWithValueTests {
12+
13+
@Before
14+
fun setUp() {
15+
System.loadLibrary("SampleAppCore")
16+
JavaSwift.init()
17+
}
18+
19+
@Test
20+
fun testNone() {
21+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.None)
22+
Assert.assertTrue(value is SampleEnumWithValue.None)
23+
}
24+
25+
@Test
26+
fun testStringValue() {
27+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.StringValue("42"))
28+
Assert.assertEquals((value as SampleEnumWithValue.StringValue).value, "42")
29+
}
30+
31+
@Test
32+
fun testIntegerValue() {
33+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.IntegerValue(42))
34+
Assert.assertEquals((value as SampleEnumWithValue.IntegerValue).value, 42)
35+
}
36+
37+
@Test
38+
fun testInt8Value() {
39+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Int8Value(42))
40+
Assert.assertEquals((value as SampleEnumWithValue.Int8Value).value.toInt(), 42)
41+
}
42+
43+
@Test
44+
fun testInt16Value() {
45+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Int16Value(42))
46+
Assert.assertEquals((value as SampleEnumWithValue.Int16Value).value.toInt(), 42)
47+
}
48+
49+
@Test
50+
fun testInt32Value() {
51+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Int32Value(42))
52+
Assert.assertEquals((value as SampleEnumWithValue.Int32Value).value, 42)
53+
}
54+
55+
@Test
56+
fun testInt64Value() {
57+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Int64Value(42))
58+
Assert.assertEquals((value as SampleEnumWithValue.Int64Value).value, 42)
59+
}
60+
61+
@Test
62+
fun testUIntValue() {
63+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.UintValue(42U))
64+
Assert.assertEquals((value as SampleEnumWithValue.UintValue).value, 42U)
65+
}
66+
67+
@Test
68+
fun testUInt8Value() {
69+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Uint8Value(42U))
70+
Assert.assertEquals((value as SampleEnumWithValue.Uint8Value).value.toUInt(), 42U)
71+
}
72+
73+
@Test
74+
fun testUInt16Value() {
75+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Uint16Value(42U))
76+
Assert.assertEquals((value as SampleEnumWithValue.Uint16Value).value.toUInt(), 42U)
77+
}
78+
79+
@Test
80+
fun testUInt32Value() {
81+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Uint32Value(42U))
82+
Assert.assertEquals((value as SampleEnumWithValue.Uint32Value).value, 42U)
83+
}
84+
85+
@Test
86+
fun testUInt64Value() {
87+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Uint64Value(42U))
88+
Assert.assertEquals((value as SampleEnumWithValue.Uint64Value).value.toUInt(), 42U)
89+
}
90+
91+
@Test
92+
fun testObjectArrayValue() {
93+
val array = arrayListOf(SampleValue.getRandomValue())
94+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.ObjectArrayValue(array))
95+
Assert.assertEquals((value as SampleEnumWithValue.ObjectArrayValue).value, array)
96+
}
97+
98+
@Test
99+
fun testStringArrayValue() {
100+
val array = arrayListOf("42")
101+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.StringArrayValue(array))
102+
Assert.assertEquals((value as SampleEnumWithValue.StringArrayValue).value, array)
103+
}
104+
105+
@Test
106+
fun testNumberArrayValue() {
107+
val array = arrayListOf(42)
108+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.NumberArrayValue(array))
109+
Assert.assertEquals((value as SampleEnumWithValue.NumberArrayValue).value, array)
110+
}
111+
112+
@Test
113+
fun testArrayInArrayValue() {
114+
val array = arrayListOf(arrayListOf(42))
115+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.ArrayInArrayValue(array))
116+
Assert.assertEquals((value as SampleEnumWithValue.ArrayInArrayValue).value, array)
117+
}
118+
119+
@Test
120+
fun testDictInArrayValue() {
121+
val array = arrayListOf(hashMapOf(42 to 42))
122+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.DictInArrayValue(array))
123+
Assert.assertEquals((value as SampleEnumWithValue.DictInArrayValue).value, array)
124+
}
125+
126+
@Test
127+
fun testDictSampleClassValue() {
128+
val dict = hashMapOf("abc" to SampleValue.getRandomValue())
129+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.DictSampleClassValue(dict))
130+
Assert.assertEquals((value as SampleEnumWithValue.DictSampleClassValue).value, dict)
131+
}
132+
133+
@Test
134+
fun testDictStringsValue() {
135+
val dict = hashMapOf("abc" to "42")
136+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.DictStringsValue(dict))
137+
Assert.assertEquals((value as SampleEnumWithValue.DictStringsValue).value, dict)
138+
}
139+
140+
@Test
141+
fun testDictNumbersValue() {
142+
val dict = hashMapOf(42 to 42)
143+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.DictNumbersValue(dict))
144+
Assert.assertEquals((value as SampleEnumWithValue.DictNumbersValue).value, dict)
145+
}
146+
147+
@Test
148+
fun testDict64NumbersValue() {
149+
val dict = hashMapOf(10 to 10)
150+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.Dict64NumbersValue(dict))
151+
Assert.assertEquals((value as SampleEnumWithValue.Dict64NumbersValue).value, dict)
152+
}
153+
154+
@Test
155+
fun testDictInDictValue() {
156+
val dict = hashMapOf(10 to hashMapOf(10 to 10))
157+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.DictInDictValue(dict))
158+
Assert.assertEquals((value as SampleEnumWithValue.DictInDictValue).value, dict)
159+
}
160+
161+
@Test
162+
fun testArrayInDictValue() {
163+
val dict = hashMapOf(10 to arrayListOf(10, 10))
164+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.ArrayInDictValue(dict))
165+
Assert.assertEquals((value as SampleEnumWithValue.ArrayInDictValue).value, dict)
166+
}
167+
168+
@Test
169+
fun testSetValue() {
170+
val set = hashSetOf(42)
171+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.SetValue(set))
172+
Assert.assertEquals((value as SampleEnumWithValue.SetValue).value, set)
173+
}
174+
175+
@Test
176+
fun testSetValuesValue() {
177+
val set = hashSetOf(SampleValue.getRandomValue())
178+
val value = SampleEnumWithValue.copy(SampleEnumWithValue.SetValuesValue(set))
179+
Assert.assertEquals((value as SampleEnumWithValue.SetValuesValue).value, set)
180+
}
181+
182+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.readdle.swiftjava.sample
2+
3+
import com.readdle.codegen.anotation.SwiftFunc
4+
import com.readdle.codegen.anotation.SwiftValue
5+
6+
@SwiftValue
7+
sealed class SampleEnumWithValue {
8+
9+
object None : SampleEnumWithValue()
10+
data class StringValue(val value: String) : SampleEnumWithValue()
11+
data class IntegerValue(val value: Int) : SampleEnumWithValue()
12+
data class Int8Value(val value: Byte) : SampleEnumWithValue()
13+
data class Int16Value(val value: Short) : SampleEnumWithValue()
14+
data class Int32Value(val value: Int) : SampleEnumWithValue()
15+
data class Int64Value(val value: Long) : SampleEnumWithValue()
16+
data class UintValue(val value: UInt) : SampleEnumWithValue()
17+
data class Uint8Value(val value: UByte) : SampleEnumWithValue()
18+
data class Uint16Value(val value: UShort) : SampleEnumWithValue()
19+
data class Uint32Value(val value: UInt) : SampleEnumWithValue()
20+
data class Uint64Value(val value: ULong) : SampleEnumWithValue()
21+
data class ObjectArrayValue(val value: ArrayList<SampleValue>) : SampleEnumWithValue()
22+
data class StringArrayValue(val value: ArrayList<String>) : SampleEnumWithValue()
23+
data class NumberArrayValue(val value: ArrayList<Int>) : SampleEnumWithValue()
24+
data class ArrayInArrayValue(val value: ArrayList<ArrayList<Int>>) : SampleEnumWithValue()
25+
data class DictInArrayValue(val value: ArrayList<HashMap<Int, Int>>) : SampleEnumWithValue()
26+
data class DictSampleClassValue(val value: HashMap<String, SampleValue>) : SampleEnumWithValue()
27+
data class DictStringsValue(val value: HashMap<String, String>) : SampleEnumWithValue()
28+
data class DictNumbersValue(val value: HashMap<Int, Int>) : SampleEnumWithValue()
29+
data class Dict64NumbersValue(val value: HashMap<Int, Int>) : SampleEnumWithValue()
30+
data class DictInDictValue(val value: HashMap<Int, HashMap<Int, Int>>) : SampleEnumWithValue()
31+
data class ArrayInDictValue(val value: HashMap<Int, ArrayList<Int>>) : SampleEnumWithValue()
32+
data class SetValue(val value: HashSet<Int>) : SampleEnumWithValue()
33+
data class SetValuesValue(val value: HashSet<SampleValue>) : SampleEnumWithValue()
34+
35+
companion object {
36+
37+
@JvmStatic
38+
@SwiftFunc("copy(value:)")
39+
external fun copy(value: SampleEnumWithValue): SampleEnumWithValue
40+
}
41+
}

sample/src/main/swift/Package.resolved

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"repositoryURL": "https://github.com/readdle/java_swift.git",
77
"state": {
88
"branch": null,
9-
"revision": "c1ed735ca2e82d961511ee6e938157f78c31278b",
10-
"version": "2.2.0"
9+
"revision": "f50fd0845ef22caa955444e72b25b72a59cf7b71",
10+
"version": "2.2.2"
1111
}
1212
},
1313
{
@@ -42,8 +42,8 @@
4242
"repositoryURL": "https://github.com/readdle/swift-java-coder.git",
4343
"state": {
4444
"branch": null,
45-
"revision": "c2980b1eab70202b09d31db7cdce317b1c05c2b2",
46-
"version": "1.1.0"
45+
"revision": "d65e2ca83c524e9fde6ff90d291115c57a268217",
46+
"version": "1.1.1"
4747
}
4848
}
4949
]

sample/src/main/swift/Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
dependencies: [
1616
.package(url: "https://github.com/readdle/java_swift.git", .upToNextMinor(from: "2.2.0")),
1717
.package(url: "https://github.com/readdle/swift-java.git", .upToNextMinor(from: "0.3.0")),
18-
.package(url: "https://github.com/readdle/swift-java-coder.git", .upToNextMinor(from: "1.1.0")),
18+
.package(url: "https://github.com/readdle/swift-java-coder.git", .upToNextMinor(from: "1.1.1")),
1919
.package(url: "https://github.com/readdle/swift-anycodable.git", .upToNextMinor(from: "1.0.3")),
2020
],
2121
targets: [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Created by Andrew Druk on 06.05.2024.
3+
//
4+
5+
import Foundation
6+
7+
public enum SampleEnumWithValue: Codable {
8+
case none
9+
case stringValue(value: String)
10+
case integerValue(value: Int)
11+
case int8Value(value: Int8)
12+
case int16Value(value: Int16)
13+
case int32Value(value: Int32)
14+
case int64Value(value: Int64)
15+
case uintValue(value: UInt)
16+
case uint8Value(value: UInt8)
17+
case uint16Value(value: UInt16)
18+
case uint32Value(value: UInt32)
19+
case uint64Value(value: UInt64)
20+
case optionalIntegerValue(value: Int?)
21+
case optionalInt8Value(value: Int8?)
22+
case optionalInt16Value(value: Int16?)
23+
case optionalInt32Value(value: Int32?)
24+
case optionalInt64Value(value: Int64?)
25+
case optionalUintValue(value: UInt?)
26+
case optionalUint8Value(value: UInt8?)
27+
case optionalUint16Value(value: UInt16?)
28+
case optionalUint32Value(value: UInt32?)
29+
case optionalUint64Value(value: UInt64?)
30+
case objectArrayValue(value: [SampleValue])
31+
case stringArrayValue(value: [String])
32+
case numberArrayValue(value: [Int])
33+
case arrayInArrayValue(value: [[Int]])
34+
case dictInArrayValue(value: [[Int: Int]])
35+
case dictSampleClassValue(value: [String: SampleValue])
36+
case dictStringsValue(value: [String: String])
37+
case dictNumbersValue(value: [Int: Int])
38+
case dict64NumbersValue(value: [Int: Int])
39+
case dictInDictValue(value: [Int: [Int: Int]])
40+
case arrayInDictValue(value: [Int: [Int]])
41+
case setValue(value: Set<Int>)
42+
case setValuesValue(value: Set<SampleValue>)
43+
44+
public static func copy(value: SampleEnumWithValue) -> SampleEnumWithValue {
45+
return value
46+
}
47+
}

0 commit comments

Comments
 (0)