From 91bab33479e41818838f98620d360e452e6c70b4 Mon Sep 17 00:00:00 2001 From: Nicolas Merget <104347736+nmerget@users.noreply.github.com> Date: Sun, 18 May 2025 13:47:54 +0200 Subject: [PATCH] fix: generate-utils (#245) --- src/commands/generate/generate-utils.ts | 101 +++++++++++++----------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/src/commands/generate/generate-utils.ts b/src/commands/generate/generate-utils.ts index e9f86ae..3d7b7e1 100644 --- a/src/commands/generate/generate-utils.ts +++ b/src/commands/generate/generate-utils.ts @@ -5,18 +5,7 @@ export const generateUtils = `import { GArray, GDictionary } from "godot"; * @param data Any JavaScript value to convert to a Godot-compatible type * @returns The Godot-compatible representation of the input data */ -export function toGD(data: any) { - // Handle null/undefined - if (data === null || data === undefined) { - return null; - } - - // Handle primitive types (pass through) - if (typeof data !== 'object') { - return data; - } - - // Handle arrays +export function toGD(data: any): any { if (Array.isArray(data)) { const gArray = new GArray(); for (const item of data) { @@ -24,21 +13,38 @@ export function toGD(data: any) { } return gArray; } - - // Handle objects + if (data instanceof GArray || data instanceof GDictionary) { - // Already a Godot type, return as is return data; } - - // Convert object to GDictionary - const gDict = new GDictionary(); - for (const key in data) { - if (Object.prototype.hasOwnProperty.call(data, key)) { - gDict.set_keyed(key, toGD(data[key])); + + if (typeof data === "object") { + const gDict = new GDictionary(); + for (const [key, value] of Object.entries(data as Object)) { + gDict.set(key, toGD(value)); } + return gDict; } - return gDict; + + return data; +} + +/** + * Recursively converts arbitrary JavaScript data into a Godot-compatible GDictionary object. + * @param data The data to be converted + * @returns A GDictionary containing the converted data + */ +export function toGDictionary(data: T): GDictionary { + return toGD(data); +} + +/** + * Recursively converts arbitrary JavaScript array into a Godot-compatible GArray object. + * @param data The data to be converted + * @returns A GArray containing the converted data + */ +export function toGArray(data: T): GArray { + return toGD(data); } /** @@ -46,34 +52,41 @@ export function toGD(data: any) { * @param data Godot data structure to convert to a JavaScript equivalent * @returns The JavaScript representation of the input Godot data */ -export function fromGD(data: any): T { - // Handle null/undefined - if (data === null || data === undefined) { - return null as T; - } - - // Handle GArray +export function fromGD(data: any) { if (data instanceof GArray) { const jsArray: any[] = []; for (let i = 0; i < data.size(); i++) { jsArray.push(fromGD(data.get_indexed(i))); } - return jsArray as T; + return jsArray; } - - // Handle GDictionary + if (data instanceof GDictionary) { - const jsObject: Record = {}; - const keys = data.keys(); - - for (let i = 0; i < keys.size(); i++) { - const key = keys.get_indexed(i); - jsObject[key] = fromGD(data.get_keyed(key)); + const jsObject: { [key: string]: any } = {}; + for (const key of data.keys()) { + jsObject[key.toString()] = fromGD(data.get(key)); } - - return jsObject as T; + return jsObject; } - - // Handle primitive types and anything else (pass through) - return data as T; -}`; + + return data; +} + +/** + * Recursively converts arbitrary GDictionary data into a JSObject object. + * @param data The data to be converted + * @returns A JSObject containing the converted data + */ +export function fromGDictionary(data: GDictionary): T { + return fromGD(data); +} + +/** + * Recursively converts arbitrary GArray array into a JS array. + * @param data The data to be converted + * @returns A JS array containing the converted data + */ +export function fromGArray(data: T): T[] { + return fromGD(data); +} +`;