Skip to content

Commit 91bab33

Browse files
authored
fix: generate-utils (#245)
1 parent a8a36b6 commit 91bab33

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

src/commands/generate/generate-utils.ts

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,88 @@ export const generateUtils = `import { GArray, GDictionary } from "godot";
55
* @param data Any JavaScript value to convert to a Godot-compatible type
66
* @returns The Godot-compatible representation of the input data
77
*/
8-
export function toGD(data: any) {
9-
// Handle null/undefined
10-
if (data === null || data === undefined) {
11-
return null;
12-
}
13-
14-
// Handle primitive types (pass through)
15-
if (typeof data !== 'object') {
16-
return data;
17-
}
18-
19-
// Handle arrays
8+
export function toGD(data: any): any {
209
if (Array.isArray(data)) {
2110
const gArray = new GArray();
2211
for (const item of data) {
2312
gArray.push_back(toGD(item));
2413
}
2514
return gArray;
2615
}
27-
28-
// Handle objects
16+
2917
if (data instanceof GArray || data instanceof GDictionary) {
30-
// Already a Godot type, return as is
3118
return data;
3219
}
33-
34-
// Convert object to GDictionary
35-
const gDict = new GDictionary();
36-
for (const key in data) {
37-
if (Object.prototype.hasOwnProperty.call(data, key)) {
38-
gDict.set_keyed(key, toGD(data[key]));
20+
21+
if (typeof data === "object") {
22+
const gDict = new GDictionary();
23+
for (const [key, value] of Object.entries(data as Object)) {
24+
gDict.set(key, toGD(value));
3925
}
26+
return gDict;
4027
}
41-
return gDict;
28+
29+
return data;
30+
}
31+
32+
/**
33+
* Recursively converts arbitrary JavaScript data into a Godot-compatible GDictionary object.
34+
* @param data The data to be converted
35+
* @returns A GDictionary containing the converted data
36+
*/
37+
export function toGDictionary<T>(data: T): GDictionary<T> {
38+
return toGD(data);
39+
}
40+
41+
/**
42+
* Recursively converts arbitrary JavaScript array into a Godot-compatible GArray object.
43+
* @param data The data to be converted
44+
* @returns A GArray containing the converted data
45+
*/
46+
export function toGArray<T>(data: T): GArray<T> {
47+
return toGD(data);
4248
}
4349
4450
/**
4551
* Recursively converts Godot GDictionary and GArray types to JavaScript objects and arrays
4652
* @param data Godot data structure to convert to a JavaScript equivalent
4753
* @returns The JavaScript representation of the input Godot data
4854
*/
49-
export function fromGD<T = any>(data: any): T {
50-
// Handle null/undefined
51-
if (data === null || data === undefined) {
52-
return null as T;
53-
}
54-
55-
// Handle GArray
55+
export function fromGD(data: any) {
5656
if (data instanceof GArray) {
5757
const jsArray: any[] = [];
5858
for (let i = 0; i < data.size(); i++) {
5959
jsArray.push(fromGD(data.get_indexed(i)));
6060
}
61-
return jsArray as T;
61+
return jsArray;
6262
}
63-
64-
// Handle GDictionary
63+
6564
if (data instanceof GDictionary) {
66-
const jsObject: Record<string | number, any> = {};
67-
const keys = data.keys();
68-
69-
for (let i = 0; i < keys.size(); i++) {
70-
const key = keys.get_indexed(i);
71-
jsObject[key] = fromGD(data.get_keyed(key));
65+
const jsObject: { [key: string]: any } = {};
66+
for (const key of data.keys()) {
67+
jsObject[key.toString()] = fromGD(data.get(key));
7268
}
73-
74-
return jsObject as T;
69+
return jsObject;
7570
}
76-
77-
// Handle primitive types and anything else (pass through)
78-
return data as T;
79-
}`;
71+
72+
return data;
73+
}
74+
75+
/**
76+
* Recursively converts arbitrary GDictionary data into a JSObject object.
77+
* @param data The data to be converted
78+
* @returns A JSObject containing the converted data
79+
*/
80+
export function fromGDictionary<T>(data: GDictionary<T>): T {
81+
return fromGD(data);
82+
}
83+
84+
/**
85+
* Recursively converts arbitrary GArray array into a JS array.
86+
* @param data The data to be converted
87+
* @returns A JS array containing the converted data
88+
*/
89+
export function fromGArray<T>(data: T): T[] {
90+
return fromGD(data);
91+
}
92+
`;

0 commit comments

Comments
 (0)