Skip to content

Commit

Permalink
fix: revert
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Oct 11, 2024
1 parent 66627bc commit fcd7fea
Showing 1 changed file with 26 additions and 76 deletions.
102 changes: 26 additions & 76 deletions packages/state/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { Type as RecsType, Schema } from "@dojoengine/recs";

export function convertValues(schema: Schema, values: any) {
if (typeof schema !== "object" || schema === null) {
console.warn("Invalid schema provided.");
return {};
}

if (typeof values !== "object" || values === null) {
console.warn("Invalid values provided.");
return {};
}

return Object.keys(schema).reduce<any>((acc, key) => {
if (!acc) {
acc = {};
}

const schemaType = schema[key];
const value = values[key];

Expand All @@ -31,30 +20,26 @@ export function convertValues(schema: Schema, values: any) {

switch (schemaType) {
case RecsType.StringArray:
if (value.type === "array") {
if (value.value.length === 0) {
acc[key] = [];
} else if (value.value[0].type === "enum") {
acc[key] = value.value.map(
(item: any) => item.value.option
);
} else {
acc[key] = value.value.map((a: any) => {
try {
return BigInt(a.value);
} catch (error) {
console.warn(
`Failed to convert ${a.value} to BigInt. Using string value instead.`
);
return a.value;
}
});
}
} else {
console.warn(
`Expected type 'array' for key '${key}', but received '${value.type}'.`
if (value.type === "array" && value.value.length === 0) {
acc[key] = [];
} else if (
value.type === "array" &&
value.value[0].type === "enum"
) {
acc[key] = value.value.map(
(item: any) => item.value.option
);
acc[key] = undefined;
} else {
acc[key] = value.value.map((a: any) => {
try {
return BigInt(a.value);
} catch (error) {
console.warn(
`Failed to convert ${a.value} to BigInt. Using string value instead.`
);
return a.value;
}
});
}
break;

Expand All @@ -67,16 +52,10 @@ export function convertValues(schema: Schema, values: any) {
acc[key] = BigInt(value.value);
} catch (error) {
console.warn(
`Failed to convert ${value.value} to BigInt. Attempting hexadecimal conversion.`
`Failed to convert ${value.value} to BigInt. Using string value instead.`
);
try {
acc[key] = BigInt(`0x${value.value}`);
} catch (hexError) {
console.warn(
`Failed to convert ${value.value} to BigInt even with hexadecimal. Assigning undefined.`
);
acc[key] = undefined;
}

acc[key] = BigInt(`0x${value.value}`);

Check failure on line 58 in packages/state/src/utils/index.ts

View workflow job for this annotation

GitHub Actions / check

src/__tests__/utils.test.ts > convertValues > BigInt conversion fallback > should fallback to hexadecimal conversion for invalid BigInt strings

SyntaxError: Cannot convert 0xinvalid_bigint to a BigInt ❯ src/utils/index.ts:58:32 ❯ Module.convertValues src/utils/index.ts:4:32 ❯ src/__tests__/utils.test.ts:156:28
}
break;

Expand All @@ -93,46 +72,17 @@ export function convertValues(schema: Schema, values: any) {
if (value.value instanceof Map) {
const structValues = Object.fromEntries(value.value);
acc[key] = convertValues(schemaType, structValues);
} else if (
typeof value.value === "object" &&
value.value !== null
) {
acc[key] = convertValues(schemaType, value.value);
} else {
console.warn(
`Expected 'struct' type with object value for key '${key}'.`
);
acc[key] = undefined;
acc[key] = convertValues(schemaType, value.value);
}
} else if (
Array.isArray(schemaType) &&
value.type === "array"
) {
if (!Array.isArray(value.value)) {
console.warn(
`Expected an array for key '${key}', but received '${typeof value.value}'.`
);
acc[key] = undefined;
} else {
acc[key] = value.value.map((item: any) => {
if (
item.type === "struct" &&
typeof schemaType[0] === "object"
) {
return convertValues(schemaType[0], item.value);
} else {
// Handle non-struct items or different types as needed
return convertValues(
typeof schemaType[0] === "object"
? schemaType[0]
: {},
item
);
}
});
}
acc[key] = value.value.map((item: any) =>
convertValues(schemaType[0], item)
);
} else {
// For unrecognized schema types, assign the raw value
acc[key] = value.value;
}
break;
Expand Down

0 comments on commit fcd7fea

Please sign in to comment.