-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide private fields in tagged enums and errors (#84)
According to [The Big O of Code Reviews](https://www.egorand.dev/the-big-o-of-code-reviews/), this is a O(_m * n_) change. Potentially fixes #75. This PR does a couple of things for TaggedEnums: - we eliminate the `Variant__data` intermediate type and replace it with a macro to render the type explicitly. This is to make the values and their types show up in autocomplete. - we move or remove `__uniffiTypeName`, `__variant` and `__variantName` from `UniffiEnum` and `UniffiError`. I think that this fixes the bug #75. If not, I don't think it's harmful to keep this fix. - we introduce a `playground` directory into the `typescript/tests` directory: this is explicitly for experimenting with hand writing generated code before touching the templates.
- Loading branch information
Showing
8 changed files
with
157 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
import { test } from "../testing/asserts"; | ||
import { MyEnum } from "./playground/enums"; | ||
|
||
test("Enums have private fields", (t) => { | ||
const v1: MyEnum = new MyEnum.Variant1({ myValue: "string" }); | ||
const v2: MyEnum = new MyEnum.Variant2(42, "string"); | ||
|
||
function switchGetTag(obj: any): string { | ||
if (!MyEnum.instanceOf(obj)) { | ||
t.fail("Obj should be a MyEnum"); | ||
return ""; | ||
} | ||
const v = obj; | ||
switch (v.tag) { | ||
case "Variant1": { | ||
const { myValue } = v.inner; | ||
t.assertEqual(myValue, "string"); | ||
t.assertTrue(MyEnum.Variant1.instanceOf(v)); | ||
return v.tag; | ||
} | ||
case "Variant2": { | ||
const [p1, p2] = v.inner; | ||
t.assertEqual(p1, 42); | ||
t.assertEqual(p2, "string"); | ||
t.assertTrue(MyEnum.Variant2.instanceOf(v)); | ||
return v.tag; | ||
} | ||
} | ||
} | ||
switchGetTag(v1); | ||
switchGetTag(v2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
import { UniffiEnum } from "../../src/enums"; | ||
|
||
export const MyEnum = (() => { | ||
const typeName = "MyEnum"; | ||
type Variant1__interface = { | ||
tag: "Variant1"; | ||
inner: Readonly<{ myValue: string }>; | ||
}; | ||
class Variant1_ extends UniffiEnum implements Variant1__interface { | ||
private readonly __uniffiTypeName = typeName; | ||
readonly tag = "Variant1"; | ||
readonly inner: Readonly<{ myValue: string }>; | ||
constructor(inner: { myValue: string }) { | ||
super(typeName, "Variant1"); | ||
this.inner = Object.freeze(inner); | ||
} | ||
static instanceOf(obj: any): obj is Variant1_ { | ||
return obj.tag === "Variant1" && instanceOf(obj); | ||
} | ||
} | ||
|
||
type Variant2__interface = { | ||
tag: "Variant2"; | ||
inner: Readonly<[number, string]>; | ||
}; | ||
class Variant2_ extends UniffiEnum implements Variant2__interface { | ||
private readonly __uniffiTypeName = typeName; | ||
readonly tag = "Variant2"; | ||
readonly inner: Readonly<[number, string]>; | ||
constructor(p1: number, p2: string) { | ||
super(typeName, "Variant2"); | ||
this.inner = Object.freeze([p1, p2]); | ||
} | ||
static instanceOf(obj: any): obj is Variant2_ { | ||
return obj.tag === "Variant2" && instanceOf(obj); | ||
} | ||
} | ||
|
||
function instanceOf(obj: any): obj is MyEnum { | ||
return obj.__uniffiTypeName === "MyEnum"; | ||
} | ||
|
||
return Object.freeze({ | ||
Variant1: Variant1_, | ||
Variant2: Variant2_, | ||
instanceOf, | ||
}); | ||
})(); | ||
|
||
export type MyEnum = InstanceType< | ||
(typeof MyEnum)[keyof Omit<typeof MyEnum, "instanceOf">] | ||
>; |
File renamed without changes.