Skip to content

Commit cfdf7b4

Browse files
authored
Add support for union to enum (#117)
* Add support for union to enum * rename * reuse path for unique name & fix shared types is not in shared file * lint * fix: kotlin shared types is missing * fix: align usage with TupleType * style: rename members * fix: remove unused content * feature: add nullable check * fix: revert unused content * doc: update document * fix: remove kind from LiteralType * fix: remove type cast of namedType * fix: revert LiteralType from parseLiteralNode * fix: Add support for strings that look like numbers
1 parent bafb0d0 commit cfdf7b4

File tree

15 files changed

+498
-127
lines changed

15 files changed

+498
-127
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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
20+
21+
22+
data class OverriddenFullSize(
23+
@JvmField val size: Float,
24+
@JvmField val count: Int,
25+
@JvmField val stringEnum: StringEnum,
26+
@JvmField val numEnum: NumEnum,
27+
@JvmField val defEnum: DefaultEnum,
28+
@JvmField val stringUnion: OverriddenFullSizeMembersStringUnionType,
29+
@JvmField val numberStringUnion: OverriddenFullSizeMembersNumberStringUnionType,
30+
@JvmField val nullableStringUnion: OverriddenFullSizeMembersNullableStringUnionType?,
31+
@JvmField val numUnion1: OverriddenFullSizeMembersNumUnion1Type,
32+
@JvmField val foo: OverriddenFullSizeMembersFooType,
33+
@JvmField val width: Float,
34+
@JvmField val height: Float,
35+
@JvmField val scale: Float,
36+
@JvmField val member: NumEnum = NumEnum.ONE,
37+
)
38+
39+
enum class NumEnum(val value: Int) {
40+
ONE(1),
41+
TWO(2);
42+
43+
companion object {
44+
fun find(value: Int) = values().find { it.value == value }
45+
}
46+
}
47+
48+
class NumEnumTypeAdapter : JsonSerializer<NumEnum>, JsonDeserializer<NumEnum> {
49+
override fun serialize(obj: NumEnum, type: Type, context: JsonSerializationContext): JsonElement {
50+
return JsonPrimitive(obj.value)
51+
}
52+
53+
override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): NumEnum? {
54+
return NumEnum.find(json.asInt)
55+
}
56+
}
57+
58+
enum class StringEnum {
59+
@SerializedName("a") A,
60+
@SerializedName("b") B
61+
}
62+
63+
enum class DefaultEnum(val value: Int) {
64+
DEFAULT_VALUE_C(0),
65+
DEFAULT_VALUE_D(1);
66+
67+
companion object {
68+
fun find(value: Int) = values().find { it.value == value }
69+
}
70+
}
71+
72+
class DefaultEnumTypeAdapter : JsonSerializer<DefaultEnum>, JsonDeserializer<DefaultEnum> {
73+
override fun serialize(obj: DefaultEnum, type: Type, context: JsonSerializationContext): JsonElement {
74+
return JsonPrimitive(obj.value)
75+
}
76+
77+
override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): DefaultEnum? {
78+
return DefaultEnum.find(json.asInt)
79+
}
80+
}
81+
82+
enum class OverriddenFullSizeMembersStringUnionType {
83+
@SerializedName("A1") A1,
84+
@SerializedName("B1") B1
85+
}
86+
87+
enum class OverriddenFullSizeMembersNumberStringUnionType {
88+
@SerializedName("11") _11,
89+
@SerializedName("21") _21
90+
}
91+
92+
enum class OverriddenFullSizeMembersNullableStringUnionType {
93+
@SerializedName("A1") A1,
94+
@SerializedName("B1") B1
95+
}
96+
97+
enum class OverriddenFullSizeMembersNumUnion1Type(val value: Int) {
98+
_11(11),
99+
_21(21);
100+
101+
companion object {
102+
fun find(value: Int) = values().find { it.value == value }
103+
}
104+
}
105+
106+
class OverriddenFullSizeMembersNumUnion1TypeTypeAdapter : JsonSerializer<OverriddenFullSizeMembersNumUnion1Type>, JsonDeserializer<OverriddenFullSizeMembersNumUnion1Type> {
107+
override fun serialize(obj: OverriddenFullSizeMembersNumUnion1Type, type: Type, context: JsonSerializationContext): JsonElement {
108+
return JsonPrimitive(obj.value)
109+
}
110+
111+
override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): OverriddenFullSizeMembersNumUnion1Type? {
112+
return OverriddenFullSizeMembersNumUnion1Type.find(json.asInt)
113+
}
114+
}
115+
116+
data class OverriddenFullSizeMembersFooType(
117+
@JvmField val stringField: String,
118+
@JvmField val numberField: Float,
119+
)

demo/basic/generated/kotlin/IHtmlApi.kt

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ interface IHtmlApiBridge {
3030
fun requestRenderingResult()
3131
fun getSize(callback: Callback<OverriddenFullSize>)
3232
fun getAliasSize(callback: Callback<JSBaseSize>)
33+
fun getName(callback: Callback<IHtmlApiGetNameReturnType>)
34+
fun getAge(gender: IHtmlApiGetAgeGender, callback: Callback<IHtmlApiGetAgeReturnType>)
3335
fun testDictionaryWithAnyKey(dict: Map<String, String>)
3436
}
3537

@@ -69,69 +71,53 @@ open class IHtmlApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson
6971
executeJsForResponse(JSBaseSize::class.java, "getAliasSize", callback)
7072
}
7173

74+
override fun getName(callback: Callback<IHtmlApiGetNameReturnType>) {
75+
executeJsForResponse(IHtmlApiGetNameReturnType::class.java, "getName", callback)
76+
}
77+
78+
override fun getAge(gender: IHtmlApiGetAgeGender, callback: Callback<IHtmlApiGetAgeReturnType>) {
79+
executeJsForResponse(IHtmlApiGetAgeReturnType::class.java, "getAge", callback, mapOf(
80+
"gender" to gender
81+
))
82+
}
83+
7284
override fun testDictionaryWithAnyKey(dict: Map<String, String>) {
7385
executeJs("testDictionaryWithAnyKey", mapOf(
7486
"dict" to dict
7587
))
7688
}
7789
}
7890

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,
91+
data class JSBaseSize(
8592
@JvmField val width: Float,
8693
@JvmField val height: Float,
87-
@JvmField val scale: Float,
88-
@JvmField val member: NumEnum = NumEnum.ONE,
8994
)
9095

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-
}
96+
enum class IHtmlApiGetNameReturnType {
97+
@SerializedName("A2") A2,
98+
@SerializedName("B2") B2
9899
}
99100

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
101+
enum class IHtmlApiGetAgeGender {
102+
@SerializedName("Male") MALE,
103+
@SerializedName("Female") FEMALE
113104
}
114105

115-
enum class DefaultEnum(val value: Int) {
116-
DEFAULT_VALUE_C(0),
117-
DEFAULT_VALUE_D(1);
106+
enum class IHtmlApiGetAgeReturnType(val value: Int) {
107+
_21(21),
108+
_22(22);
118109

119110
companion object {
120111
fun find(value: Int) = values().find { it.value == value }
121112
}
122113
}
123114

124-
class DefaultEnumTypeAdapter : JsonSerializer<DefaultEnum>, JsonDeserializer<DefaultEnum> {
125-
override fun serialize(obj: DefaultEnum, type: Type, context: JsonSerializationContext): JsonElement {
115+
class IHtmlApiGetAgeReturnTypeTypeAdapter : JsonSerializer<IHtmlApiGetAgeReturnType>, JsonDeserializer<IHtmlApiGetAgeReturnType> {
116+
override fun serialize(obj: IHtmlApiGetAgeReturnType, type: Type, context: JsonSerializationContext): JsonElement {
126117
return JsonPrimitive(obj.value)
127118
}
128119

129-
override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): DefaultEnum? {
130-
return DefaultEnum.find(json.asInt)
120+
override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): IHtmlApiGetAgeReturnType? {
121+
return IHtmlApiGetAgeReturnType.find(json.asInt)
131122
}
132123
}
133-
134-
data class JSBaseSize(
135-
@JvmField val width: Float,
136-
@JvmField val height: Float,
137-
)

demo/basic/generated/kotlin/IImageOptionApi.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface IImageOptionApiBridge {
2020
fun getSourceOfImageWithID(id: String, callback: Callback<String?>)
2121
fun getImageDataList(callback: Callback<String>)
2222
fun getContentBoundsOfElementWithID(id: String, callback: Callback<String?>)
23+
fun getSize(callback: Callback<OverriddenFullSize>)
2324
}
2425

2526
open class IImageOptionApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson, "imageOption"), IImageOptionApiBridge {
@@ -51,4 +52,8 @@ open class IImageOptionApiBridge(editor: WebEditor, gson: Gson) : JsBridge(edito
5152
"id" to id
5253
))
5354
}
55+
56+
override fun getSize(callback: Callback<OverriddenFullSize>) {
57+
executeJsForResponse(OverriddenFullSize::class.java, "getSize", callback)
58+
}
5459
}

demo/basic/generated/swift/IHtmlApi.swift

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ public class IHtmlApi {
6262
jsExecutor.execute(with: "htmlApi", feature: "getAliasSize", args: nil, completion: completion)
6363
}
6464

65+
public func getName(completion: @escaping BridgeCompletion<IHtmlApiGetNameReturnType>) {
66+
jsExecutor.execute(with: "htmlApi", feature: "getName", args: nil, completion: completion)
67+
}
68+
69+
public func getAge(gender: IHtmlApiGetAgeGender, completion: @escaping BridgeCompletion<IHtmlApiGetAgeReturnType>) {
70+
struct Args: Encodable {
71+
let gender: IHtmlApiGetAgeGender
72+
}
73+
let args = Args(
74+
gender: gender
75+
)
76+
jsExecutor.execute(with: "htmlApi", feature: "getAge", args: args, completion: completion)
77+
}
78+
6579
public func testDictionaryWithAnyKey(dict: [String: String], completion: BridgeJSExecutor.Completion? = nil) {
6680
struct Args: Encodable {
6781
let dict: [String: String]
@@ -73,53 +87,27 @@ public class IHtmlApi {
7387
}
7488
}
7589

76-
/// Example documentation for interface
77-
public struct OverriddenFullSize: Codable {
78-
public var size: Double
79-
public var count: Int
80-
public var stringEnum: StringEnum
81-
public var numEnum: NumEnum
82-
public var defEnum: DefaultEnum
90+
public struct BaseSize: Codable {
8391
public var width: Double
8492
public var height: Double
85-
public var scale: Double
86-
/// Example documentation for member
87-
private var member: NumEnum = .one
88-
89-
public init(size: Double, count: Int, stringEnum: StringEnum, numEnum: NumEnum, defEnum: DefaultEnum, width: Double, height: Double, scale: Double) {
90-
self.size = size
91-
self.count = count
92-
self.stringEnum = stringEnum
93-
self.numEnum = numEnum
94-
self.defEnum = defEnum
93+
94+
public init(width: Double, height: Double) {
9595
self.width = width
9696
self.height = height
97-
self.scale = scale
9897
}
9998
}
10099

101-
public enum NumEnum: Int, Codable {
102-
case one = 1
103-
case two = 2
100+
public enum IHtmlApiGetNameReturnType: String, Codable {
101+
case a2 = "A2"
102+
case b2 = "B2"
104103
}
105104

106-
public enum StringEnum: String, Codable {
107-
/// Description for enum member a
108-
case a = "a"
109-
case b = "b"
105+
public enum IHtmlApiGetAgeGender: String, Codable {
106+
case male = "Male"
107+
case female = "Female"
110108
}
111109

112-
public enum DefaultEnum: Int, Codable {
113-
case defaultValueC = 0
114-
case defaultValueD = 1
115-
}
116-
117-
public struct BaseSize: Codable {
118-
public var width: Double
119-
public var height: Double
120-
121-
public init(width: Double, height: Double) {
122-
self.width = width
123-
self.height = height
124-
}
110+
public enum IHtmlApiGetAgeReturnType: Int, Codable {
111+
case _21 = 21
112+
case _22 = 22
125113
}

demo/basic/generated/swift/IImageOptionApi.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ public class IImageOptionApi {
5555
)
5656
jsExecutor.execute(with: "imageOption", feature: "getContentBoundsOfElementWithID", args: args, completion: completion)
5757
}
58+
59+
public func getSize(completion: @escaping BridgeCompletion<OverriddenFullSize>) {
60+
jsExecutor.execute(with: "imageOption", feature: "getSize", args: nil, completion: completion)
61+
}
5862
}

0 commit comments

Comments
 (0)