Skip to content

Commit

Permalink
Enum match fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vorlias committed Dec 26, 2021
1 parent a9f69ce commit 80c902e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
6 changes: 6 additions & 0 deletions example/server/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ZirconPrint from "BuiltIn/Print";
import { ZirconEnumBuilder } from "Class/ZirconEnumBuilder";
import { ZirconFunctionBuilder } from "Class/ZirconFunctionBuilder";
import { ZirconNamespaceBuilder } from "Class/ZirconNamespaceBuilder";
import { $print } from "rbxts-transform-debug";

Log.SetLogger(
Logger.configure()
Expand Down Expand Up @@ -91,6 +92,8 @@ ZirconServer.Registry.Init(
)
.AddFunction(
new ZirconFunctionBuilder("test_enum").AddArgument(TestEnum).Bind((context, value) => {
$print("call to test_enum", context, value);
Log.Info("{Item}", value.getName());
value.match({
Value2: () => {
Log.Info("Got given enum item 2 (member)");
Expand All @@ -106,6 +109,9 @@ ZirconServer.Registry.Init(
Value2: () => {
Log.Info("Got given enum item 2 (parent)");
},
_: () => {
Log.Info("Anything else");
},
});
}),
[ZirconDefaultGroup.User],
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
"@rbxts/snapdragon": "2.0.0-beta.1",
"@rbxts/string-utils": "^1.0.3",
"@rbxts/t": "^1.3.5",
"@rbxts/zirconium": "^1.1.0-beta.0"
"@rbxts/zirconium": "^1.1.0-beta.1"
}
}
39 changes: 25 additions & 14 deletions src/Class/ZirconEnum.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ZrEnum } from "@rbxts/zirconium/out/Data/Enum";
import { $print } from "rbxts-transform-debug";
import { ZirconEnumItem } from "./ZirconEnumItem";
import { ZirconValidator } from "./ZirconTypeValidator";

Expand All @@ -16,7 +17,7 @@ export type ZirconEnumValidator<K extends string> = ZirconValidator<
*/
export class ZirconEnum<K extends string> extends ZrEnum {
public constructor(name: string, members: K[]) {
super(members, name);
super(members, name, (value, index) => new ZirconEnumItem(this, index, value));
}

/**
Expand Down Expand Up @@ -57,30 +58,40 @@ export class ZirconEnum<K extends string> extends ZrEnum {

public getMemberType(): ZirconEnumValidator<K> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const thisRef = this;
const enumType = this;
return {
Validate(value): value is ZirconEnumItem<ZirconEnum<K>, K> | string {
print("validate", value, thisRef.getItems());
return (
(typeIs(value, "string") &&
thisRef.getItems().find((f) => f.getName().lower() === value.lower()) !== undefined) ||
(typeIs(value, "number") && thisRef.getItems().find((f) => f.getValue() === value) !== undefined) ||
(value instanceof ZirconEnumItem && (value.getEnum() as ZirconEnum<any>) === thisRef)
);
if (typeIs(value, "string")) {
const strItem = enumType.getItems().find((item) => item.getName().lower() === value.lower());
$print("scmp", value, strItem?.getName());
return strItem !== undefined;
} else if (typeIs(value, "number")) {
const intItem = enumType.getItems().find((item) => item.getValue() === value);
$print("icmp", value, intItem?.getValue());
return intItem !== undefined;
} else if (value instanceof ZirconEnumItem) {
$print("instancecmp", value, enumType);
return value.getEnum() === enumType;
}

return false;
},
Transform(value) {
if (typeIs(value, "string")) {
return thisRef.getItems().find((v) => v.getName().lower() === value.lower())! as ZirconEnumItem<
ZirconEnum<K>,
K
>;
const strItem = enumType.getItems().find((item) => item.getName().lower() === value.lower());
return strItem as ZirconEnumItem<ZirconEnum<K>, K>;
} else if (typeIs(value, "number")) {
return thisRef.getItems().find((v) => v.getValue() === value)! as ZirconEnumItem<ZirconEnum<K>, K>;
const strItem = enumType.getItems().find((item) => item.getValue() === value);
return strItem as ZirconEnumItem<ZirconEnum<K>, K>;
} else {
return value as ZirconEnumItem<ZirconEnum<K>, K>;
}
},
Type: this.getEnumName(),
};
}

public toString() {
return this.getEnumName();
}
}
8 changes: 6 additions & 2 deletions src/Class/ZirconEnumItem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZrEnumItem } from "@rbxts/zirconium/out/Data/Enum";
import { ZrEnumItem } from "@rbxts/zirconium/out/Data/EnumItem";
import { ZrObjectUserdata } from "@rbxts/zirconium/out/Data/Userdata";
import { ZirconEnum, EnumMatchTree } from "./ZirconEnum";

Expand All @@ -9,7 +9,7 @@ export class ZirconEnumItem<
TParent extends ZirconEnum<string> = ZirconEnum<string>,
K extends string = string
> extends ZrEnumItem {
public constructor(private enumParent: TParent, id: number, name: K) {
public constructor(enumParent: TParent, id: number, name: K) {
super(enumParent, id, name);
}

Expand All @@ -20,4 +20,8 @@ export class ZirconEnumItem<
public match<R>(matches: EnumMatchTree<TParent, K, R>) {
return matches[this.getName() as K](this);
}

public toString() {
return `${this.getEnum().getEnumName()}.${this.getName()}`;
}
}

0 comments on commit 80c902e

Please sign in to comment.