Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit a7244e5

Browse files
committed
Add hello world examples
1 parent 5f3891a commit a7244e5

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,63 @@ WARNING: Work in Progress (WIP) not ready for production
55

66
The runtime provides Kotlin running on Java 8u131 b11.
77

8+
## Hello World Kotlin Action
9+
10+
The runtime for Kotlin supports two types of API for creating actions:
11+
1. JSON parameter and return type, using the GSON Library.
12+
2. Data Class parameter and return type.
13+
14+
### JSON parameter based main.kt
15+
The following shows how to build a "HelloWorld" action using JSON parameters:
16+
17+
```kotlin
18+
import com.google.gson.JsonObject
19+
20+
fun main(args: JsonObject) : JsonObject {
21+
val name = args.getAsJsonPrimitive("name").getAsString();
22+
val hello = JsonObject();
23+
hello.addProperty("greeting", "Hello " + name + "!");
24+
return hello
25+
}
26+
```
27+
The action can then be compiled into a JAR file for use with the runtime for Kotlin. As the action has a dependency on the [Google GSON library](https://github.com/google/gson), it needs to be available on the classpath:
28+
```sh
29+
kotlinc -classpath ./gson-2.6.2.jar main.kt -d myAction.jar
30+
```
31+
32+
### Data Class based main.kt
33+
The following shows how to build a "HelloWorld" action using JSON parameters:
34+
35+
```kotlin
36+
data class User (
37+
val name: String
38+
)
39+
40+
data class Hello (
41+
val greeting: String
42+
)
43+
44+
fun main(user: User) : Hello {
45+
val hello = Hello("Hello " + user.name + "!")
46+
return hello
47+
}
48+
```
49+
The action can then be compiled into a JAR file for use with the runtime for Kotlin as follows:
50+
```sh
51+
kotlinc hello.kt -d myAction.jar
52+
```
53+
854
### How to use as a docker Action
955
To use as a docker action
1056

1157
```sh
12-
bx wsk action update myAction myAction.py --docker ibmfunctions/action-kotlin
58+
bx wsk action update myAction myAction.jar --docker ibmfunctions/action-kotlin
1359
```
1460

1561
This works on any deployment of Apache OpenWhisk or IBM Cloud Functions
1662

1763
### Future: IBM Cloud Functions (based on Apache OpenWhisk)
18-
To use as a Kotlin kind action
64+
To use as a Kotlin kind action:
1965

2066
```sh
2167
bx wsk action update myAction myAction --kind kotlin

0 commit comments

Comments
 (0)