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 2 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 @@ -3,9 +3,10 @@
IAssetRef,
IBasicType,
IClassObject,
IClassRealObject,
IComponentRef,
IEntity,
IEntityRef,
IComponentRef,
IHierarchyFile,
IRefEntity
} from "../schema";
Expand Down Expand Up @@ -72,15 +73,38 @@

parseMethod(instance: any, methodName: string, methodParams: Array<IBasicType>) {
return Promise.all(methodParams.map((param) => this.parseBasicType(param))).then((result) => {
return instance[methodName](...result);
const methodCallback = instance[methodName](...result);
// @todo: Only parse first param to adapter original data format
const firstParam = methodParams?.[0] as IClassRealObject;
if (ReflectionParser._isRealClass(firstParam)) {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Unsafe type assertion needs validation

The type assertion as IClassRealObject is unsafe. Consider validating the type before the assertion.

Apply this diff to add proper type validation:

- const firstParam = methodParams?.[0] as IClassRealObject;
- if (ReflectionParser._isRealClass(firstParam)) {
+ const firstParam = methodParams?.[0];
+ if (firstParam && typeof firstParam === 'object' && ReflectionParser._isRealClass(firstParam)) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const firstParam = methodParams?.[0] as IClassRealObject;
if (ReflectionParser._isRealClass(firstParam)) {
const firstParam = methodParams?.[0];
if (firstParam && typeof firstParam === 'object' && ReflectionParser._isRealClass(firstParam)) {

const callbackProps = firstParam?.callbackProps;
if (callbackProps) {
const promises = [];
for (let key in callbackProps) {
const value = callbackProps[key];
const promise = this.parseBasicType(value, methodCallback[key]).then((v) => {
if (methodCallback[key] !== v) {
methodCallback[key] = v;
}
return v;
});
promises.push(promise);
}
return Promise.all(promises);
}
} else {

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#L80-L95

Added lines #L80 - L95 were not covered by tests
return methodCallback;
}
});
}

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._isRealClass(value)) {
return Promise.resolve(Loader.getClass(value["classReal"]));

Check warning on line 106 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#L106

Added line #L106 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 +182,10 @@
return value["class"] !== undefined;
}

private static _isRealClass(value: any): value is IClassObject {
return value["classReal"] !== undefined;
}

private static _isAssetRef(value: any): value is IAssetRef {
return value["refId"] !== undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export type IClassObject = {
props?: { [key: string]: IBasicType | IMethodParams };
};

export type IClassRealObject = {
classReal: string;
callbackProps?: { [key: string]: IBasicType | IMethodParams };
};

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

Expand Down
Loading