Skip to content

Commit

Permalink
Add support for default value in Module Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
N/A authored and N/A committed Apr 18, 2024
1 parent b39559a commit be343ba
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 6 deletions.
9 changes: 9 additions & 0 deletions demo/basic/generated/kotlin/IHtmlApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +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? = null, num: Float = 1)
}

open class IHtmlApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson, "htmlApi"), IHtmlApiBridge {
Expand Down Expand Up @@ -86,6 +87,14 @@ open class IHtmlApiBridge(editor: WebEditor, gson: Gson) : JsBridge(editor, gson
"dict" to dict
))
}

override fun testDefaultValue(bool: Boolean? = null, bool2: Boolean? = null, num: Float = 1) {
executeJs("testDefaultValue", mapOf(
"bool" to bool
"bool2" to bool2
"num" to num
))
}
}

data class JSBaseSize(
Expand Down
14 changes: 14 additions & 0 deletions demo/basic/generated/swift/IHtmlApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ public class IHtmlApi {
)
jsExecutor.execute(with: "htmlApi", feature: "testDictionaryWithAnyKey", args: args, completion: completion)
}

public func testDefaultValue(bool: Bool? = nil, bool2: Bool? = nil, num: Double = 1, completion: BridgeJSExecutor.Completion? = nil) {
struct Args: Encodable {
let bool: Bool?
let bool2: Bool?
let num: Double
}
let args = Args(
bool: bool,
bool2: bool2,
num: num
)
jsExecutor.execute(with: "htmlApi", feature: "testDefaultValue", args: args, completion: completion)
}
}

public struct BaseSize: Codable {
Expand Down
12 changes: 12 additions & 0 deletions demo/basic/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ export interface IHtmlApi {
getName(): 'A2' | 'B2';
getAge({ gender }: { gender: 'Male' | 'Female' }): 21 | 22;
testDictionaryWithAnyKey({ dict }: { dict: DictionaryWithAnyKey }): void;

testDefaultValue(options: {
/**
* @default null
*/
bool?: boolean;
bool2?: boolean;
/**
* @default 1
*/
num: number;
}): void;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions documentation/interface-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ ts-gyb parses tags in [JSDoc](https://jsdoc.app) documentation.
- `@shouldExport`: Specify whether an `interface` should be exported. Set it to `true` to export.
- `@overrideModuleName`: Change the name of the interface for ts-gyb. This is helpful for dropping the `I` prefix in TypeScript interface name.
- `@overrideTypeName`: Similar to `@overrideModuleName`, this is used to override the name of custom types used in method parameters or return values.
- `@default`: default value for Module Interface's function parameter,
```typescript
/**
Expand All @@ -225,6 +226,18 @@ ts-gyb parses tags in [JSDoc](https://jsdoc.app) documentation.
interface InterfaceWithTags {
// The name of the module would be `ProperNamedInterface` in generated code
...

foo(bar: {
/**
* @default null
*/
bool?: boolean;
bool2?: boolean;
/**
* @default 1
*/
num: number;
}): void;
}
```

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.10.1",
"version": "0.11.0",
"description": "Generate Native API based on TS interface",
"repository": {
"type": "git",
Expand Down
17 changes: 16 additions & 1 deletion src/parser/ValueParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ export class ValueParser {
const documentation = ts.displayPartsToString(symbol?.getDocumentationComment(this.checker));

const staticValue = this.parseLiteralNode(node.type);

if (staticValue !== null) {
return { name, type: staticValue.type, staticValue: staticValue.value, documentation };
return { name, type: staticValue.type, staticValue: staticValue.value, documentation, defaultValue: "" };
}

if (node.type.kind === ts.SyntaxKind.NeverKeyword) {
Expand All @@ -155,10 +156,24 @@ export class ValueParser {

const valueType = this.valueTypeFromNode(node);

const jsDocTags = symbol?.getJsDocTags(this.checker);
let defaultValue = "";
jsDocTags?.forEach((tag) => {
if (tag.name === 'default' && tag.text?.length === 1) {
if (tag.text[0].kind === 'text') {
defaultValue = tag.text[0].text;
}
}
});
if (defaultValue.length === 0 && valueType.kind === ValueTypeKind.optionalType) {
defaultValue = "null";
}

return {
name,
type: valueType,
documentation,
defaultValue
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/renderer/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export function renderCode<View>(templatePath: string, view: View): string {
return Mustache.render(template, view, (partialName) => {
const partialPath = path.join(directory, `${partialName}.mustache`);
return fs.readFileSync(partialPath).toString();
}, {
escape: (value: string) => value,
});
}
4 changes: 4 additions & 0 deletions src/renderer/value-transformer/KotlinValueTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,8 @@ export class KotlinValueTransformer implements ValueTransformer {
convertTypeNameFromCustomMap(name: string): string {
return this.typeNameMap[name] ?? name;
}

null(): string {
return 'null';
}
}
4 changes: 4 additions & 0 deletions src/renderer/value-transformer/SwiftValueTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,8 @@ export class SwiftValueTransformer implements ValueTransformer {
convertTypeNameFromCustomMap(name: string): string {
return this.typeNameMap[name] ?? name;
}

null(): string {
return 'nil';
}
}
1 change: 1 addition & 0 deletions src/renderer/value-transformer/ValueTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface ValueTransformer {
convertValue(value: Value, type: ValueType): string;
convertEnumKey(text: string): string;
convertTypeNameFromCustomMap(name: string): string;
null(): string;
}
14 changes: 12 additions & 2 deletions src/renderer/views/MethodView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ export class MethodView {
}

get parametersDeclaration(): string {
return this.parameters.map((parameter) => `${parameter.name}: ${parameter.type}`).join(', ');
return this.parameters.map((parameter) => {
if (parameter.defaultValue.length === 0) {
return `${parameter.name}: ${parameter.type}`;
}
let { defaultValue } = parameter;
if (defaultValue === 'null') {
defaultValue = this.valueTransformer.null();
}
return `${parameter.name}: ${parameter.type} = ${defaultValue}`;
}).join(', ');
}

get parameters(): { name: string; type: string; last: boolean }[] {
get parameters(): { name: string; type: string; defaultValue: string; last: boolean }[] {
return this.method.parameters.map((parameter, index) => ({
name: parameter.name,
type: this.valueTransformer.convertValueType(parameter.type),
defaultValue: parameter.defaultValue,
last: index === this.method.parameters.length - 1,
}));
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Field {
type: ValueType;
staticValue?: Value;
documentation: string;
defaultValue: string;
}

export type ValueType = NonEmptyType | OptionalType;
Expand Down

0 comments on commit be343ba

Please sign in to comment.