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

Support parse method result #2485

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
import type {
IAssetRef,
IBasicType,
ICanCallbackMethodObject,
IClassObject,
IClassTypeObject,
IComponentRef,
IEntity,
IEntityRef,
IComponentRef,
IHierarchyFile,
IMethodParams,
IRefEntity
} from "../schema";
import { ParserContext, ParserType } from "./ParserContext";
Expand Down Expand Up @@ -70,17 +73,27 @@
});
}

parseMethod(instance: any, methodName: string, methodParams: Array<IBasicType>) {
return Promise.all(methodParams.map((param) => this.parseBasicType(param))).then((result) => {
return instance[methodName](...result);
parseMethod(instance: any, methodName: string, methodParams: IMethodParams) {
const isCanCallbackMethodObject = ReflectionParser._isCanCallbackMethodObject(methodParams);
const params = isCanCallbackMethodObject ? methodParams.params : methodParams;

return Promise.all(params.map((param) => this.parseBasicType(param))).then((result) => {
const methodCallback = instance[methodName](...result);
if (isCanCallbackMethodObject && methodParams.callback) {
return this.parsePropsAndMethods(methodCallback, methodParams.callback);
} else {

Check warning on line 84 in packages/loader/src/resource-deserialize/resources/parser/ReflectionParser.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/resource-deserialize/resources/parser/ReflectionParser.ts#L83-L84

Added lines #L83 - L84 were not covered by tests
return methodCallback;
}
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
});
}

parseBasicType(value: IBasicType, originValue?: any): Promise<any> {
if (Array.isArray(value)) {
return Promise.all(value.map((item) => this.parseBasicType(item)));
} else if (typeof value === "object" && value != null) {
if (ReflectionParser._isClass(value)) {
if (ReflectionParser._isClassType(value)) {
return Promise.resolve(Loader.getClass(value["classType"]));

Check warning on line 95 in packages/loader/src/resource-deserialize/resources/parser/ReflectionParser.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/resource-deserialize/resources/parser/ReflectionParser.ts#L95

Added line #L95 was not covered by tests
} else if (ReflectionParser._isClass(value)) {
// class object
return this.parseClassObject(value);
} else if (ReflectionParser._isAssetRef(value)) {
Expand Down Expand Up @@ -158,6 +171,10 @@
return value["class"] !== undefined;
}

private static _isClassType(value: any): value is IClassTypeObject {
return value["classType"] !== undefined;
}

private static _isAssetRef(value: any): value is IAssetRef {
return value["refId"] !== undefined;
}
Expand All @@ -169,4 +186,8 @@
private static _isComponentRef(value: any): value is IComponentRef {
return value["ownerId"] !== undefined && value["componentId"] !== undefined;
}

private static _isCanCallbackMethodObject(value: any): value is ICanCallbackMethodObject {
return Array.isArray(value?.params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ export interface IHierarchyFile {
entities: Array<IEntity>;
}

export type IMethodParams = Array<IBasicType>;
export type ICanCallbackMethodObject = {
params: Array<IBasicType>;
callback?: {
methods?: { [methodName: string]: Array<IMethodParams> };
props?: { [key: string]: IBasicType | IMethodParams };
};
};

export type IMethodParams = Array<IBasicType> | ICanCallbackMethodObject;

export interface IBasicEntity {
name?: string;
Expand Down Expand Up @@ -73,11 +81,15 @@ export type IComponent = { id: string; refId?: string } & IClassObject;

export type IClassObject = {
class: string;
constructParams?: IMethodParams;
constructParams?: Array<IBasicType>;
methods?: { [methodName: string]: Array<IMethodParams> };
props?: { [key: string]: IBasicType | IMethodParams };
};

export type IClassTypeObject = {
Copy link
Contributor

@luzhuang luzhuang Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need discuss: merge to IClassObject use needInstance to distinguish?

classType: string;
};

export type IBasicType =
| string
| number
Expand All @@ -86,6 +98,7 @@ export type IBasicType =
| undefined
| IAssetRef
| IClassObject
| IClassTypeObject
| IMethodParams
| IEntityRef;

Expand Down
Loading