Skip to content

Commit f405382

Browse files
authored
Implement Kotlin renderer (#24)
* Implement Kotlin renderer * Lint * Implement named type generation * Imports * Fix * Fix template * Convert kotlin values * Better alignment * Address feedbacks * Fix for kotlin
1 parent 969ca43 commit f405382

24 files changed

+692
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,5 @@ basic-types/dist
126126
.vscode/launch.json
127127

128128
src/basic-types
129+
130+
.DS_Store

demo/config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222
"typeNameMap": {
2323
"CodeGen_Int": "Int"
2424
}
25+
},
26+
"kotlin": {
27+
"templates": {
28+
"api": "../example-templates/kotlin-bridge.mustache"
29+
},
30+
"outputDirectory": {
31+
"api": "generated/kotlin"
32+
},
33+
"namedTypesTemplatePath": "../example-templates/kotlin-named-types.mustache",
34+
"namedTypesOutputPath": "generated/kotlin/BridgeTypes.kt",
35+
"typeNameMap": {
36+
"CodeGen_Int": "Int"
37+
}
2538
}
2639
}
2740
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2021.
3+
* Microsoft Corporation. All rights reserved.
4+
*
5+
*
6+
* This file is automatically generated
7+
* Please DO NOT modify
8+
*/
9+
10+
package com.microsoft.office.outlook.rooster.web.bridge
11+
12+
import java.lang.reflect.Type
13+
import com.google.gson.JsonDeserializationContext
14+
import com.google.gson.JsonDeserializer
15+
import com.google.gson.JsonElement
16+
import com.google.gson.JsonPrimitive
17+
import com.google.gson.JsonSerializationContext
18+
import com.google.gson.JsonSerializer
19+
import com.google.gson.annotations.SerializedName

demo/generated/kotlin/IHtmlApi.kt

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2021.
3+
* Microsoft Corporation. All rights reserved.
4+
*
5+
*
6+
* This file is automatically generated
7+
* Please DO NOT modify
8+
*/
9+
10+
package com.microsoft.office.outlook.rooster.web.bridge
11+
12+
import com.google.gson.Gson
13+
import com.microsoft.office.outlook.rooster.Callback
14+
import com.microsoft.office.outlook.rooster.web.JsBridge
15+
import com.microsoft.office.outlook.rooster.web.WebEditor
16+
import java.lang.reflect.Type
17+
import com.google.gson.JsonDeserializationContext
18+
import com.google.gson.JsonDeserializer
19+
import com.google.gson.JsonElement
20+
import com.google.gson.JsonPrimitive
21+
import com.google.gson.JsonSerializationContext
22+
import com.google.gson.JsonSerializer
23+
import com.google.gson.annotations.SerializedName
24+
25+
interface IHtmlApiBridge {
26+
fun setMentionClassNames(idToClassNames: Map<String, Array<String>>)
27+
fun getHeight(callback: Callback<Float>)
28+
fun getHeightWithBottomAnchor(sta: Array<String>, callback: Callback<Float>)
29+
fun getHTML(title: String, callback: Callback<String>)
30+
fun requestRenderingResult()
31+
fun getSize(callback: Callback<OverriddenFullSize>)
32+
fun getAliasSize(callback: Callback<BaseSize>)
33+
fun testDictionaryWithAnyKey(dict: Map<String, String>)
34+
}
35+
36+
open class IHtmlApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson, "htmlApi"), IHtmlApiBridge {
37+
38+
override fun setMentionClassNames(idToClassNames: Map<String, Array<String>>) {
39+
executeJs("setMentionClassNames", mapOf(
40+
"idToClassNames" to idToClassNames
41+
))
42+
}
43+
44+
override fun getHeight(callback: Callback<Float>) {
45+
executeJsForResponse(Float::class.java, "getHeight", callback)
46+
}
47+
48+
override fun getHeightWithBottomAnchor(sta: Array<String>, callback: Callback<Float>) {
49+
executeJsForResponse(Float::class.java, "getHeightWithBottomAnchor", callback, mapOf(
50+
"sta" to sta
51+
))
52+
}
53+
54+
override fun getHTML(title: String, callback: Callback<String>) {
55+
executeJsForResponse(String::class.java, "getHTML", callback, mapOf(
56+
"title" to title
57+
))
58+
}
59+
60+
override fun requestRenderingResult() {
61+
executeJs("requestRenderingResult")
62+
}
63+
64+
override fun getSize(callback: Callback<OverriddenFullSize>) {
65+
executeJsForResponse(OverriddenFullSize::class.java, "getSize", callback)
66+
}
67+
68+
override fun getAliasSize(callback: Callback<BaseSize>) {
69+
executeJsForResponse(BaseSize::class.java, "getAliasSize", callback)
70+
}
71+
72+
override fun testDictionaryWithAnyKey(dict: Map<String, String>) {
73+
executeJs("testDictionaryWithAnyKey", mapOf(
74+
"dict" to dict
75+
))
76+
}
77+
}
78+
79+
data class OverriddenFullSize(
80+
@JvmField val size: Float,
81+
@JvmField val count: Int,
82+
@JvmField val stringEnum: StringEnum,
83+
@JvmField val numEnum: NumEnum,
84+
@JvmField val defEnum: DefaultEnum,
85+
@JvmField val width: Float,
86+
@JvmField val height: Float,
87+
@JvmField val scale: Float,
88+
@JvmField val member: NumEnum = NumEnum.ONE,
89+
)
90+
91+
enum class NumEnum(val value: Int) {
92+
ONE(1),
93+
TWO(2);
94+
95+
companion object {
96+
fun find(value: Int) = values().find { it.value == value }
97+
}
98+
}
99+
100+
class NumEnumTypeAdapter : JsonSerializer<NumEnum>, JsonDeserializer<NumEnum> {
101+
override fun serialize(obj: NumEnum, type: Type, context: JsonSerializationContext): JsonElement {
102+
return JsonPrimitive(obj.value)
103+
}
104+
105+
override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): NumEnum? {
106+
return NumEnum.find(json.asInt)
107+
}
108+
}
109+
110+
enum class StringEnum {
111+
@SerializedName("a") A,
112+
@SerializedName("b") B
113+
}
114+
115+
enum class DefaultEnum {
116+
@SerializedName("c") C,
117+
@SerializedName("d") D
118+
}
119+
120+
data class BaseSize(
121+
@JvmField val width: Float,
122+
@JvmField val height: Float,
123+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2021.
3+
* Microsoft Corporation. All rights reserved.
4+
*
5+
*
6+
* This file is automatically generated
7+
* Please DO NOT modify
8+
*/
9+
10+
package com.microsoft.office.outlook.rooster.web.bridge
11+
12+
import com.google.gson.Gson
13+
import com.microsoft.office.outlook.rooster.Callback
14+
import com.microsoft.office.outlook.rooster.web.JsBridge
15+
import com.microsoft.office.outlook.rooster.web.WebEditor
16+
17+
interface IImageOptionApiBridge {
18+
fun hideElementWithID(id: String)
19+
fun restoreElementVisibilityWithID(id: String)
20+
fun getSourceOfImageWithID(id: String, callback: Callback<String?>)
21+
fun getImageDataList(callback: Callback<String>)
22+
fun getContentBoundsOfElementWithID(id: String, callback: Callback<String?>)
23+
}
24+
25+
open class IImageOptionApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson, "imageOption"), IImageOptionApiBridge {
26+
27+
override fun hideElementWithID(id: String) {
28+
executeJs("hideElementWithID", mapOf(
29+
"id" to id
30+
))
31+
}
32+
33+
override fun restoreElementVisibilityWithID(id: String) {
34+
executeJs("restoreElementVisibilityWithID", mapOf(
35+
"id" to id
36+
))
37+
}
38+
39+
override fun getSourceOfImageWithID(id: String, callback: Callback<String?>) {
40+
executeJsForResponse(String::class.java, "getSourceOfImageWithID", callback, mapOf(
41+
"id" to id
42+
))
43+
}
44+
45+
override fun getImageDataList(callback: Callback<String>) {
46+
executeJsForResponse(String::class.java, "getImageDataList", callback)
47+
}
48+
49+
override fun getContentBoundsOfElementWithID(id: String, callback: Callback<String?>) {
50+
executeJsForResponse(String::class.java, "getContentBoundsOfElementWithID", callback, mapOf(
51+
"id" to id
52+
))
53+
}
54+
}

demo/generated/swift/IHtmlApi.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public class IHtmlApi {
2222
let args = Args(
2323
idToClassNames: idToClassNames
2424
)
25-
jsExecutor.execute(with: "", feature: "setMentionClassNames", args: args, completion: completion)
25+
jsExecutor.execute(with: "htmlApi", feature: "setMentionClassNames", args: args, completion: completion)
2626
}
2727

2828
public func getHeight(completion: @escaping BridgeCompletion<Double>) {
29-
jsExecutor.execute(with: "", feature: "getHeight", args: nil, completion: completion)
29+
jsExecutor.execute(with: "htmlApi", feature: "getHeight", args: nil, completion: completion)
3030
}
3131

3232
public func getHeightWithBottomAnchor(sta: [String], completion: @escaping BridgeCompletion<Double>) {
@@ -36,7 +36,7 @@ public class IHtmlApi {
3636
let args = Args(
3737
sta: sta
3838
)
39-
jsExecutor.execute(with: "", feature: "getHeightWithBottomAnchor", args: args, completion: completion)
39+
jsExecutor.execute(with: "htmlApi", feature: "getHeightWithBottomAnchor", args: args, completion: completion)
4040
}
4141

4242
public func getHTML(title: String, completion: @escaping BridgeCompletion<String>) {
@@ -46,19 +46,19 @@ public class IHtmlApi {
4646
let args = Args(
4747
title: title
4848
)
49-
jsExecutor.execute(with: "", feature: "getHTML", args: args, completion: completion)
49+
jsExecutor.execute(with: "htmlApi", feature: "getHTML", args: args, completion: completion)
5050
}
5151

5252
public func requestRenderingResult(completion: BridgeJSExecutor.Completion? = nil) {
53-
jsExecutor.execute(with: "", feature: "requestRenderingResult", args: nil, completion: completion)
53+
jsExecutor.execute(with: "htmlApi", feature: "requestRenderingResult", args: nil, completion: completion)
5454
}
5555

5656
public func getSize(completion: @escaping BridgeCompletion<OverriddenFullSize>) {
57-
jsExecutor.execute(with: "", feature: "getSize", args: nil, completion: completion)
57+
jsExecutor.execute(with: "htmlApi", feature: "getSize", args: nil, completion: completion)
5858
}
5959

6060
public func getAliasSize(completion: @escaping BridgeCompletion<BaseSize>) {
61-
jsExecutor.execute(with: "", feature: "getAliasSize", args: nil, completion: completion)
61+
jsExecutor.execute(with: "htmlApi", feature: "getAliasSize", args: nil, completion: completion)
6262
}
6363

6464
public func testDictionaryWithAnyKey(dict: [String: String], completion: BridgeJSExecutor.Completion? = nil) {
@@ -68,7 +68,7 @@ public class IHtmlApi {
6868
let args = Args(
6969
dict: dict
7070
)
71-
jsExecutor.execute(with: "", feature: "testDictionaryWithAnyKey", args: args, completion: completion)
71+
jsExecutor.execute(with: "htmlApi", feature: "testDictionaryWithAnyKey", args: args, completion: completion)
7272
}
7373
}
7474

demo/generated/swift/IImageOptionApi.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class IImageOptionApi {
1919
let args = Args(
2020
id: id
2121
)
22-
jsExecutor.execute(with: "", feature: "hideElementWithID", args: args, completion: completion)
22+
jsExecutor.execute(with: "imageOption", feature: "hideElementWithID", args: args, completion: completion)
2323
}
2424

2525
public func restoreElementVisibilityWithID(id: String, completion: BridgeJSExecutor.Completion? = nil) {
@@ -29,7 +29,7 @@ public class IImageOptionApi {
2929
let args = Args(
3030
id: id
3131
)
32-
jsExecutor.execute(with: "", feature: "restoreElementVisibilityWithID", args: args, completion: completion)
32+
jsExecutor.execute(with: "imageOption", feature: "restoreElementVisibilityWithID", args: args, completion: completion)
3333
}
3434

3535
public func getSourceOfImageWithID(id: String, completion: @escaping BridgeCompletion<String?>) {
@@ -39,11 +39,11 @@ public class IImageOptionApi {
3939
let args = Args(
4040
id: id
4141
)
42-
jsExecutor.execute(with: "", feature: "getSourceOfImageWithID", args: args, completion: completion)
42+
jsExecutor.execute(with: "imageOption", feature: "getSourceOfImageWithID", args: args, completion: completion)
4343
}
4444

4545
public func getImageDataList(completion: @escaping BridgeCompletion<String>) {
46-
jsExecutor.execute(with: "", feature: "getImageDataList", args: nil, completion: completion)
46+
jsExecutor.execute(with: "imageOption", feature: "getImageDataList", args: nil, completion: completion)
4747
}
4848

4949
public func getContentBoundsOfElementWithID(id: String, completion: @escaping BridgeCompletion<String?>) {
@@ -53,6 +53,6 @@ public class IImageOptionApi {
5353
let args = Args(
5454
id: id
5555
)
56-
jsExecutor.execute(with: "", feature: "getContentBoundsOfElementWithID", args: args, completion: completion)
56+
jsExecutor.execute(with: "imageOption", feature: "getContentBoundsOfElementWithID", args: args, completion: completion)
5757
}
5858
}

demo/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ interface DictionaryWithAnyKey {
5454
/**
5555
* Documentation for module
5656
* @shouldExport true
57+
* @invokePath htmlApi
5758
*/
5859
export interface IHtmlApi {
5960
/**
@@ -73,6 +74,7 @@ export interface IHtmlApi {
7374

7475
/**
7576
* @shouldExport true
77+
* @invokePath imageOption
7678
*/
7779
export interface IImageOptionApi {
7880
hideElementWithID({ id }: { id: string }): void;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2021.
3+
* Microsoft Corporation. All rights reserved.
4+
*
5+
*
6+
* This file is automatically generated
7+
* Please DO NOT modify
8+
*/
9+
10+
package com.microsoft.office.outlook.rooster.web.bridge
11+
12+
import com.google.gson.Gson
13+
import com.microsoft.office.outlook.rooster.Callback
14+
import com.microsoft.office.outlook.rooster.web.JsBridge
15+
import com.microsoft.office.outlook.rooster.web.WebEditor
16+
{{#associatedTypes.length}}
17+
import java.lang.reflect.Type
18+
import com.google.gson.JsonDeserializationContext
19+
import com.google.gson.JsonDeserializer
20+
import com.google.gson.JsonElement
21+
import com.google.gson.JsonPrimitive
22+
import com.google.gson.JsonSerializationContext
23+
import com.google.gson.JsonSerializer
24+
import com.google.gson.annotations.SerializedName
25+
{{/associatedTypes.length}}
26+
27+
interface {{moduleName}}Bridge {
28+
{{#methods}}
29+
fun {{methodName}}({{{parametersDeclaration}}}{{#returnType}}{{#parametersDeclaration.length}}, {{/parametersDeclaration.length}}callback: Callback<{{{returnType}}}>{{/returnType}})
30+
{{/methods}}
31+
}
32+
33+
open class {{moduleName}}Bridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson, "{{customTags.invokePath}}"), {{moduleName}}Bridge {
34+
{{#methods}}
35+
36+
override fun {{methodName}}({{{parametersDeclaration}}}{{#returnType}}{{#parametersDeclaration.length}}, {{/parametersDeclaration.length}}callback: Callback<{{{returnType}}}>{{/returnType}}) {
37+
{{#returnType}}executeJsForResponse({{{nonOptionalReturnType}}}::class.java, {{/returnType}}{{^returnType}}executeJs({{/returnType}}"{{methodName}}"{{#returnType}}, callback{{/returnType}}{{#parametersDeclaration.length}}, mapOf({{/parametersDeclaration.length}}{{^parameters}}){{/parameters}}
38+
{{#parameters}}
39+
"{{name}}" to {{name}}
40+
{{/parameters}}
41+
{{#parametersDeclaration.length}}
42+
))
43+
{{/parametersDeclaration.length}}
44+
}
45+
{{/methods}}
46+
}
47+
{{#associatedTypes}}
48+
49+
{{> kotlin-named-type}}
50+
{{/associatedTypes}}

0 commit comments

Comments
 (0)