|
1 | | -import { Type as RecsType, Schema, ComponentValue } from "@dojoengine/recs"; |
| 1 | +import { Type as RecsType, Schema } from "@dojoengine/recs"; |
2 | 2 |
|
3 | | -export function convertValues(schema: Schema, values: any): ComponentValue { |
4 | | - return Object.keys(schema).reduce<any>( |
5 | | - (acc: ComponentValue, key: string) => { |
6 | | - if (!acc) { |
7 | | - acc = {}; // Ensure acc is initialized |
8 | | - } |
9 | | - const schemaType = schema[key]; |
10 | | - const value = values[key]; |
| 3 | +export function convertValues(schema: Schema, values: any) { |
| 4 | + return Object.keys(schema).reduce<any>((acc, key) => { |
| 5 | + if (!acc) { |
| 6 | + acc = {}; |
| 7 | + } |
| 8 | + const schemaType = schema[key]; |
| 9 | + const value = values[key]; |
11 | 10 |
|
12 | | - if (value === null || value === undefined) { |
13 | | - acc[key] = value; |
14 | | - return acc; |
15 | | - } |
| 11 | + if (value === null || value === undefined) { |
| 12 | + acc[key] = value; |
| 13 | + return acc; |
| 14 | + } |
16 | 15 |
|
17 | | - if (value.type === "enum") { |
18 | | - acc[key] = value.value.option; |
19 | | - return acc; |
20 | | - } |
| 16 | + if (value.type === "enum") { |
| 17 | + acc[key] = value.value.option; |
| 18 | + return acc; |
| 19 | + } |
21 | 20 |
|
22 | | - switch (schemaType) { |
23 | | - case RecsType.StringArray: |
24 | | - if ( |
25 | | - value.type === "array" && |
26 | | - value.value[0].type === "enum" |
27 | | - ) { |
28 | | - acc[key] = value.value.map( |
29 | | - (item: any) => item.value.option |
30 | | - ); |
31 | | - } else { |
32 | | - acc[key] = value.value.map((a: any) => { |
33 | | - try { |
34 | | - return BigInt(a.value); |
35 | | - } catch (error) { |
36 | | - console.warn( |
37 | | - `Failed to convert ${a.value} to BigInt. Using string value instead.` |
38 | | - ); |
39 | | - return a.value; |
40 | | - } |
41 | | - }); |
42 | | - } |
43 | | - break; |
| 21 | + switch (schemaType) { |
| 22 | + case RecsType.StringArray: |
| 23 | + if (value.type === "array" && value.value[0].type === "enum") { |
| 24 | + acc[key] = value.value.map( |
| 25 | + (item: any) => item.value.option |
| 26 | + ); |
| 27 | + } else { |
| 28 | + acc[key] = value.value.map((a: any) => { |
| 29 | + try { |
| 30 | + return BigInt(a.value); |
| 31 | + } catch (error) { |
| 32 | + console.warn( |
| 33 | + `Failed to convert ${a.value} to BigInt. Using string value instead.` |
| 34 | + ); |
| 35 | + return a.value; |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | + break; |
44 | 40 |
|
45 | | - case RecsType.String: |
46 | | - acc[key] = value.value; |
47 | | - break; |
| 41 | + case RecsType.String: |
| 42 | + acc[key] = value.value; |
| 43 | + break; |
48 | 44 |
|
49 | | - case RecsType.BigInt: |
50 | | - try { |
51 | | - acc[key] = BigInt(value.value); |
52 | | - } catch (error) { |
53 | | - console.warn( |
54 | | - `Failed to convert ${value.value} to BigInt. Using string value instead.` |
55 | | - ); |
| 45 | + case RecsType.BigInt: |
| 46 | + try { |
| 47 | + acc[key] = BigInt(value.value); |
| 48 | + } catch (error) { |
| 49 | + console.warn( |
| 50 | + `Failed to convert ${value.value} to BigInt. Using string value instead.` |
| 51 | + ); |
56 | 52 |
|
57 | | - acc[key] = BigInt(`0x${value.value}`); |
58 | | - } |
59 | | - break; |
| 53 | + acc[key] = BigInt(`0x${value.value}`); |
| 54 | + } |
| 55 | + break; |
60 | 56 |
|
61 | | - case RecsType.Boolean: |
62 | | - acc[key] = value.value; |
63 | | - break; |
| 57 | + case RecsType.Boolean: |
| 58 | + acc[key] = value.value; |
| 59 | + break; |
64 | 60 |
|
65 | | - case RecsType.Number: |
66 | | - acc[key] = Number(value.value); |
67 | | - break; |
| 61 | + case RecsType.Number: |
| 62 | + acc[key] = Number(value.value); |
| 63 | + break; |
68 | 64 |
|
69 | | - default: |
70 | | - if ( |
71 | | - typeof schemaType === "object" && |
72 | | - typeof value === "object" |
73 | | - ) { |
| 65 | + default: |
| 66 | + if (typeof schemaType === "object" && value.type === "struct") { |
| 67 | + if (value.value instanceof Map) { |
| 68 | + const structValues = Object.fromEntries(value.value); |
| 69 | + acc[key] = convertValues(schemaType, structValues); |
| 70 | + } else { |
74 | 71 | acc[key] = convertValues(schemaType, value.value); |
75 | 72 | } |
76 | | - break; |
77 | | - } |
| 73 | + } else if ( |
| 74 | + Array.isArray(schemaType) && |
| 75 | + value.type === "array" |
| 76 | + ) { |
| 77 | + acc[key] = value.value.map((item: any) => |
| 78 | + convertValues(schemaType[0], item) |
| 79 | + ); |
| 80 | + } else { |
| 81 | + acc[key] = value.value; |
| 82 | + } |
| 83 | + break; |
| 84 | + } |
78 | 85 |
|
79 | | - return acc; |
80 | | - }, |
81 | | - {} |
82 | | - ); |
| 86 | + return acc; |
| 87 | + }, {}); |
83 | 88 | } |
0 commit comments