Skip to content

Commit

Permalink
Expose defaultValue to type member (#122)
Browse files Browse the repository at this point in the history
* expose defaultValue to type member

* bump version to 0.11.1
  • Loading branch information
miku1958 authored May 7, 2024
1 parent 1303b05 commit 7f33e0c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
10 changes: 7 additions & 3 deletions demo/basic/generated/kotlin/IHtmlApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface IHtmlApiBridge {
fun getName(callback: Callback<IHtmlApiGetNameReturnType>)
fun getAge(gender: IHtmlApiGetAgeGender, callback: Callback<IHtmlApiGetAgeReturnType>)
fun testDictionaryWithAnyKey(dict: Map<String, String>)
fun testDefaultValue(bool: Boolean? = null, bool2: Boolean?, bool3: Boolean = true, num: Float = 1, string: String = "hello")
fun testDefaultValue(bool: Boolean? = null, bool2: Boolean?, bool3: Boolean = true, num: Float = 1, string: String = "hello", callback: Callback<nterfaceWithDefeaultValue>)
}

open class IHtmlApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson, "htmlApi"), IHtmlApiBridge {
Expand Down Expand Up @@ -88,8 +88,8 @@ open class IHtmlApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson
))
}

override fun testDefaultValue(bool: Boolean? = null, bool2: Boolean?, bool3: Boolean = true, num: Float = 1, string: String = "hello") {
executeJs("testDefaultValue", mapOf(
override fun testDefaultValue(bool: Boolean? = null, bool2: Boolean?, bool3: Boolean = true, num: Float = 1, string: String = "hello", callback: Callback<nterfaceWithDefeaultValue>) {
executeJsForResponse(nterfaceWithDefeaultValue::class.java, "testDefaultValue", callback, mapOf(
"bool" to bool
"bool2" to bool2
"bool3" to bool3
Expand Down Expand Up @@ -132,3 +132,7 @@ class IHtmlApiGetAgeReturnTypeTypeAdapter : JsonSerializer<IHtmlApiGetAgeReturnT
return IHtmlApiGetAgeReturnType.find(json.asInt)
}
}

data class nterfaceWithDefeaultValue(
@JvmField val defaultValue: Boolean? = true,
)
10 changes: 9 additions & 1 deletion demo/basic/generated/swift/IHtmlApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class IHtmlApi {
jsExecutor.execute(with: "htmlApi", feature: "testDictionaryWithAnyKey", args: args, completion: completion)
}

public func testDefaultValue(bool: Bool? = nil, bool2: Bool?, bool3: Bool = true, num: Double = 1, string: String = "hello", completion: BridgeJSExecutor.Completion? = nil) {
public func testDefaultValue(bool: Bool? = nil, bool2: Bool?, bool3: Bool = true, num: Double = 1, string: String = "hello", completion: @escaping BridgeCompletion<nterfaceWithDefeaultValue>) {
struct Args: Encodable {
let bool: Bool?
let bool2: Bool?
Expand Down Expand Up @@ -129,3 +129,11 @@ public enum IHtmlApiGetAgeReturnType: Int, Codable {
case _21 = 21
case _22 = 22
}

public struct nterfaceWithDefeaultValue: Codable {
public var defaultValue: Bool?

public init(defaultValue: Bool? = true) {
self.defaultValue = defaultValue
}
}
10 changes: 9 additions & 1 deletion demo/basic/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ interface DictionaryWithAnyKey {
[key: string]: string;
}


interface ObjectWithDefeaultValue {
/**
* @default true
*/
defaultValue?: boolean;
}

/**
* Documentation for module
* @shouldExport true
Expand Down Expand Up @@ -95,7 +103,7 @@ export interface IHtmlApi {
* @default "hello"
*/
string: string;
}): void;
}): ObjectWithDefeaultValue;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion example-templates/kotlin-named-type.mustache
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#custom}}
data class {{typeName}}(
{{#members}}
@JvmField val {{name}}: {{type}},
@JvmField val {{name}}: {{type}}{{#defaultValue}} = {{defaultValue}}{{/defaultValue}},
{{/members}}
{{#staticMembers}}
@JvmField val {{name}}: {{type}} = {{{value}}},
Expand Down
2 changes: 1 addition & 1 deletion example-templates/swift-named-type.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct {{typeName}}: Codable {
private var {{name}}: {{type}} = {{{value}}}
{{/staticMembers}}

public init({{#members}}{{name}}: {{type}}{{^last}}, {{/last}}{{/members}}) {
public init({{#members}}{{name}}: {{type}}{{#defaultValue}} = {{defaultValue}}{{/defaultValue}}{{^last}}, {{/last}}{{/members}}) {
{{#members}}
self.{{name}} = {{name}}
{{/members}}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-gyb",
"version": "0.11.0",
"version": "0.11.1",
"description": "Generate Native API based on TS interface",
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/views/InterfaceTypeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export class InterfaceTypeView {
return this.valueTransformer.convertValueType(this.interfaceType);
}

get members(): { name: string; type: string; documentationLines: string[]; last: boolean }[] {
get members(): { name: string; type: string; documentationLines: string[]; last: boolean, defaultValue?: string }[] {
const members = this.interfaceType.members.filter((member) => member.staticValue === undefined);

return members.map((member, index) => ({
name: member.name,
type: this.valueTransformer.convertValueType(member.type),
documentationLines: getDocumentationLines(member.documentation),
last: index === members.length - 1,
defaultValue: member.defaultValue,
}));
}

Expand Down

0 comments on commit 7f33e0c

Please sign in to comment.