Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for union to enum #117

Merged
merged 15 commits into from
Jan 9, 2024
Merged
113 changes: 113 additions & 0 deletions demo/basic/generated/kotlin/BridgeTypes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2021.
* Microsoft Corporation. All rights reserved.
*
*
* This file is automatically generated
* Please DO NOT modify
*/

package com.microsoft.office.outlook.rooster.web.bridge

import java.lang.reflect.Type
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import com.google.gson.annotations.SerializedName


data class OverriddenFullSize(
@JvmField val size: Float,
@JvmField val count: Int,
@JvmField val stringEnum: StringEnum,
@JvmField val numEnum: NumEnum,
@JvmField val defEnum: DefaultEnum,
@JvmField val stringUnion: OverriddenFullSizeMembersStringUnionType,
@JvmField val nullableStringUnion: OverriddenFullSizeMembersNullableStringUnionType?,
@JvmField val numUnion1: OverriddenFullSizeMembersNumUnion1Type,
@JvmField val foo: OverriddenFullSizeMembersFooType,
@JvmField val width: Float,
@JvmField val height: Float,
@JvmField val scale: Float,
@JvmField val member: NumEnum = NumEnum.ONE,
)

enum class NumEnum(val value: Int) {
ONE(1),
TWO(2);

companion object {
fun find(value: Int) = values().find { it.value == value }
}
}

class NumEnumTypeAdapter : JsonSerializer<NumEnum>, JsonDeserializer<NumEnum> {
override fun serialize(obj: NumEnum, type: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(obj.value)
}

override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): NumEnum? {
return NumEnum.find(json.asInt)
}
}

enum class StringEnum {
@SerializedName("a") A,
@SerializedName("b") B
}

enum class DefaultEnum(val value: Int) {
DEFAULT_VALUE_C(0),
DEFAULT_VALUE_D(1);

companion object {
fun find(value: Int) = values().find { it.value == value }
}
}

class DefaultEnumTypeAdapter : JsonSerializer<DefaultEnum>, JsonDeserializer<DefaultEnum> {
override fun serialize(obj: DefaultEnum, type: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(obj.value)
}

override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): DefaultEnum? {
return DefaultEnum.find(json.asInt)
}
}

enum class OverriddenFullSizeMembersStringUnionType {
@SerializedName("A1") A1,
@SerializedName("B1") B1
}

enum class OverriddenFullSizeMembersNullableStringUnionType {
@SerializedName("A1") A1,
@SerializedName("B1") B1
}

enum class OverriddenFullSizeMembersNumUnion1Type(val value: Int) {
_11(11),
_21(21);

companion object {
fun find(value: Int) = values().find { it.value == value }
}
}

class OverriddenFullSizeMembersNumUnion1TypeTypeAdapter : JsonSerializer<OverriddenFullSizeMembersNumUnion1Type>, JsonDeserializer<OverriddenFullSizeMembersNumUnion1Type> {
override fun serialize(obj: OverriddenFullSizeMembersNumUnion1Type, type: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(obj.value)
}

override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): OverriddenFullSizeMembersNumUnion1Type? {
return OverriddenFullSizeMembersNumUnion1Type.find(json.asInt)
}
}

data class OverriddenFullSizeMembersFooType(
@JvmField val stringField: String,
@JvmField val numberField: Float,
)
66 changes: 26 additions & 40 deletions demo/basic/generated/kotlin/IHtmlApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface IHtmlApiBridge {
fun requestRenderingResult()
fun getSize(callback: Callback<OverriddenFullSize>)
fun getAliasSize(callback: Callback<JSBaseSize>)
fun getName(callback: Callback<IHtmlApiGetNameReturnType>)
fun getAge(gender: IHtmlApiGetAgeGender, callback: Callback<IHtmlApiGetAgeReturnType>)
fun testDictionaryWithAnyKey(dict: Map<String, String>)
}

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

override fun getName(callback: Callback<IHtmlApiGetNameReturnType>) {
executeJsForResponse(IHtmlApiGetNameReturnType::class.java, "getName", callback)
}

override fun getAge(gender: IHtmlApiGetAgeGender, callback: Callback<IHtmlApiGetAgeReturnType>) {
executeJsForResponse(IHtmlApiGetAgeReturnType::class.java, "getAge", callback, mapOf(
"gender" to gender
))
}

override fun testDictionaryWithAnyKey(dict: Map<String, String>) {
executeJs("testDictionaryWithAnyKey", mapOf(
"dict" to dict
))
}
}

data class OverriddenFullSize(
@JvmField val size: Float,
@JvmField val count: Int,
@JvmField val stringEnum: StringEnum,
@JvmField val numEnum: NumEnum,
@JvmField val defEnum: DefaultEnum,
data class JSBaseSize(
@JvmField val width: Float,
@JvmField val height: Float,
@JvmField val scale: Float,
@JvmField val member: NumEnum = NumEnum.ONE,
)

enum class NumEnum(val value: Int) {
ONE(1),
TWO(2);

companion object {
fun find(value: Int) = values().find { it.value == value }
}
enum class IHtmlApiGetNameReturnType {
@SerializedName("A2") A2,
@SerializedName("B2") B2
}

class NumEnumTypeAdapter : JsonSerializer<NumEnum>, JsonDeserializer<NumEnum> {
override fun serialize(obj: NumEnum, type: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(obj.value)
}

override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): NumEnum? {
return NumEnum.find(json.asInt)
}
}

enum class StringEnum {
@SerializedName("a") A,
@SerializedName("b") B
enum class IHtmlApiGetAgeGender {
@SerializedName("Male") MALE,
@SerializedName("Female") FEMALE
}

enum class DefaultEnum(val value: Int) {
DEFAULT_VALUE_C(0),
DEFAULT_VALUE_D(1);
enum class IHtmlApiGetAgeReturnType(val value: Int) {
_21(21),
_22(22);

companion object {
fun find(value: Int) = values().find { it.value == value }
}
}

class DefaultEnumTypeAdapter : JsonSerializer<DefaultEnum>, JsonDeserializer<DefaultEnum> {
override fun serialize(obj: DefaultEnum, type: Type, context: JsonSerializationContext): JsonElement {
class IHtmlApiGetAgeReturnTypeTypeAdapter : JsonSerializer<IHtmlApiGetAgeReturnType>, JsonDeserializer<IHtmlApiGetAgeReturnType> {
override fun serialize(obj: IHtmlApiGetAgeReturnType, type: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(obj.value)
}

override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): DefaultEnum? {
return DefaultEnum.find(json.asInt)
override fun deserialize(json: JsonElement, type: Type, context: JsonDeserializationContext): IHtmlApiGetAgeReturnType? {
return IHtmlApiGetAgeReturnType.find(json.asInt)
}
}

data class JSBaseSize(
@JvmField val width: Float,
@JvmField val height: Float,
)
5 changes: 5 additions & 0 deletions demo/basic/generated/kotlin/IImageOptionApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface IImageOptionApiBridge {
fun getSourceOfImageWithID(id: String, callback: Callback<String?>)
fun getImageDataList(callback: Callback<String>)
fun getContentBoundsOfElementWithID(id: String, callback: Callback<String?>)
fun getSize(callback: Callback<OverriddenFullSize>)
}

open class IImageOptionApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson, "imageOption"), IImageOptionApiBridge {
Expand Down Expand Up @@ -51,4 +52,8 @@ open class IImageOptionApiBridge(editor: WebEditor, gson: Gson) : JsBridge(edito
"id" to id
))
}

override fun getSize(callback: Callback<OverriddenFullSize>) {
executeJsForResponse(OverriddenFullSize::class.java, "getSize", callback)
}
}
64 changes: 26 additions & 38 deletions demo/basic/generated/swift/IHtmlApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ public class IHtmlApi {
jsExecutor.execute(with: "htmlApi", feature: "getAliasSize", args: nil, completion: completion)
}

public func getName(completion: @escaping BridgeCompletion<IHtmlApiGetNameReturnType>) {
jsExecutor.execute(with: "htmlApi", feature: "getName", args: nil, completion: completion)
}

public func getAge(gender: IHtmlApiGetAgeGender, completion: @escaping BridgeCompletion<IHtmlApiGetAgeReturnType>) {
struct Args: Encodable {
let gender: IHtmlApiGetAgeGender
}
let args = Args(
gender: gender
)
jsExecutor.execute(with: "htmlApi", feature: "getAge", args: args, completion: completion)
}

public func testDictionaryWithAnyKey(dict: [String: String], completion: BridgeJSExecutor.Completion? = nil) {
struct Args: Encodable {
let dict: [String: String]
Expand All @@ -73,53 +87,27 @@ public class IHtmlApi {
}
}

/// Example documentation for interface
public struct OverriddenFullSize: Codable {
public var size: Double
public var count: Int
public var stringEnum: StringEnum
public var numEnum: NumEnum
public var defEnum: DefaultEnum
public struct BaseSize: Codable {
public var width: Double
public var height: Double
public var scale: Double
/// Example documentation for member
private var member: NumEnum = .one

public init(size: Double, count: Int, stringEnum: StringEnum, numEnum: NumEnum, defEnum: DefaultEnum, width: Double, height: Double, scale: Double) {
self.size = size
self.count = count
self.stringEnum = stringEnum
self.numEnum = numEnum
self.defEnum = defEnum

public init(width: Double, height: Double) {
self.width = width
self.height = height
self.scale = scale
}
}

public enum NumEnum: Int, Codable {
case one = 1
case two = 2
public enum IHtmlApiGetNameReturnType: String, Codable {
case a2 = "A2"
case b2 = "B2"
}

public enum StringEnum: String, Codable {
/// Description for enum member a
case a = "a"
case b = "b"
public enum IHtmlApiGetAgeGender: String, Codable {
case male = "Male"
case female = "Female"
}

public enum DefaultEnum: Int, Codable {
case defaultValueC = 0
case defaultValueD = 1
}

public struct BaseSize: Codable {
public var width: Double
public var height: Double

public init(width: Double, height: Double) {
self.width = width
self.height = height
}
public enum IHtmlApiGetAgeReturnType: Int, Codable {
case _21 = 21
case _22 = 22
}
4 changes: 4 additions & 0 deletions demo/basic/generated/swift/IImageOptionApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public class IImageOptionApi {
)
jsExecutor.execute(with: "imageOption", feature: "getContentBoundsOfElementWithID", args: args, completion: completion)
}

public func getSize(completion: @escaping BridgeCompletion<OverriddenFullSize>) {
jsExecutor.execute(with: "imageOption", feature: "getSize", args: nil, completion: completion)
}
}
Loading
Loading