Skip to content

Commit db5ddfa

Browse files
authored
Merge pull request #348 from edisontim/feat/support-options
feat: add support for option types
2 parents f52a83e + 4fb5980 commit db5ddfa

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

packages/state/src/utils/index.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@ import { Type as RecsType, Schema } from "@dojoengine/recs";
22

33
export function convertValues(schema: Schema, values: any) {
44
return Object.keys(schema).reduce<any>((acc, key) => {
5-
const schemaType = schema[key];
6-
const value = values[key];
5+
let schemaType = schema[key];
6+
let value = values[key];
77

88
if (value == null) {
99
acc[key] = value;
1010
return acc;
1111
}
1212

1313
if (value.type === "enum") {
14-
acc[key] = value.value.option;
15-
return acc;
14+
if (value.type_name && value.type_name.includes("Option")) {
15+
if (value.value.option === "None") {
16+
acc[key] = null;
17+
return acc;
18+
} else {
19+
schemaType = mapOptionalToRealType(schemaType);
20+
value = value.value.value;
21+
}
22+
} else {
23+
acc[key] = value.value.option;
24+
return acc;
25+
}
1626
}
1727

1828
switch (schemaType) {
@@ -45,6 +55,31 @@ export function convertValues(schema: Schema, values: any) {
4555
}, {});
4656
}
4757

58+
function mapOptionalToRealType(schemaType: any) {
59+
switch (schemaType) {
60+
case RecsType.OptionalNumber:
61+
return RecsType.Number;
62+
case RecsType.OptionalBigInt:
63+
return RecsType.BigInt;
64+
case RecsType.OptionalString:
65+
return RecsType.String;
66+
case RecsType.OptionalNumberArray:
67+
return RecsType.NumberArray;
68+
case RecsType.OptionalBigIntArray:
69+
return RecsType.BigIntArray;
70+
case RecsType.OptionalStringArray:
71+
return RecsType.StringArray;
72+
case RecsType.OptionalEntity:
73+
return RecsType.Entity;
74+
case RecsType.OptionalEntityArray:
75+
return RecsType.EntityArray;
76+
case RecsType.OptionalT:
77+
return RecsType.T;
78+
default:
79+
return schemaType;
80+
}
81+
}
82+
4883
function handleStringArray(value: any) {
4984
if (value.type === "array" && value.value.length === 0) {
5085
return [];

0 commit comments

Comments
 (0)