Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
- Set and Map java\kotlin types are not supported in the frontend using the Set and Map of typescript, because its not type safe and is an empty object when passed as parameter.
- "contained" not a valid descriptor in pom.xml.
  • Loading branch information
YairLevi committed Jan 24, 2024
1 parent d1df17f commit e9b50a6
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 14 deletions.
38 changes: 38 additions & 0 deletions examples/demoKotlinReact/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
127 changes: 127 additions & 0 deletions examples/demoKotlinReact/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>app.coffee</groupId>
<artifactId>kotlin-maven</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.9.22</kotlin.version>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
</properties>

<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<!-- run mvn clean compile assembly:single to create self-contained jar -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>MainKt</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>1.9.21</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>MainKt</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.github.YairLevi</groupId>
<artifactId>Coffee</artifactId>
<version>0.1.8</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.9.21</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.9.21</version>
</dependency>
</dependencies>

</project>
20 changes: 20 additions & 0 deletions examples/demoKotlinReact/src/main/kotlin/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import org.levi.coffee.Ipc
import org.levi.coffee.annotations.BindAllMethods

@BindAllMethods
class App(
val persons: MutableList<Person> = mutableListOf()
) {
fun newPerson(person: Person) {
persons.add(person)
Ipc.invoke("ppl-count")
}

fun getPersonsByName(name: String): Person {
return persons.first { it.name == name }
}

fun getAll(): List<Person> {
return persons
}
}
15 changes: 15 additions & 0 deletions examples/demoKotlinReact/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.levi.coffee.Ipc
import org.levi.coffee.Window

fun main() {
val w = Window(dev = true)
w.setSize(800, 600)
w.setURL("http://localhost:5173")
w.setTitle("Kotlin coffee app!")

w.bind(
App(),
Person()
)
w.run()
}
16 changes: 16 additions & 0 deletions examples/demoKotlinReact/src/main/kotlin/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import org.levi.coffee.annotations.BindType

@BindType
class Person(
var age: Int = 0,
var name: String = "",
var hobbies: MutableSet<String> = mutableSetOf()
) {
fun addHobby(hobby: String) {
hobbies.add(hobby)
}

fun removeHobby(hobby: String) {
hobbies.remove(hobby)
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>contained</descriptorRef>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/org/levi/coffee/internal/TypeConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ internal object TypeConverter {
type = type.replace(javaType, jsTypes[javaType]!!)
}
}
return toRegular(type)
}

private fun toRegular(type: String): String {
if (type.startsWith("Array<")) {
val innerType = type.removePrefix("Array<").removeSuffix(">")
return "(${toRegular(innerType)})[]"
}
if (type.startsWith("Set<")) {
val innerType = type.removePrefix("Set<").removeSuffix(">")
return "(${toRegular(innerType)})[]"
}
if (type.startsWith("Map<")) {
val typeArray = type
.removePrefix("Map<")
.removeSuffix(">")
.split(",")
.map { it.replace(" ", "") }
return "{[key: ${toRegular(typeArray[0])}]: ${toRegular(typeArray[1])}}"
}
return type
}
}
36 changes: 23 additions & 13 deletions src/test/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import org.levi.coffee.Window
import org.levi.coffee.annotations.BindType

@BindType
class App(
val name: String = "",
val age: Int = 0,
val list: List<String> = emptyList(),
val set: Set<String> = emptySet(),
val map: Map<String, Int> = emptyMap(),
val complex: Map<String, List<Set<App>>> = emptyMap()
)

fun main() {
// val win = Window(isDev = true)
// win.setSize(700, 700)
// win.setTitle("My first Javatron app!")
//
// win.setURL("http://localhost:5173")
// win.bind(
// Calculator(),
// )
//
// win.addBeforeStartCallback { println("Started app...") }
// win.addOnCloseCallback { println("Closed the app!") }
//
// win.run()
val win = Window(dev = true)
win.setSize(700, 700)
win.setTitle("My first Javatron app!")

win.setURL("http://localhost:5173")
win.bind(
App()
)

win.addBeforeStartCallback { println("Started app...") }
win.addOnCloseCallback { println("Closed the app!") }

win.run()
}

0 comments on commit e9b50a6

Please sign in to comment.