From 1fa0a52ec850d81319fd1ed057cd1dd11a86948e Mon Sep 17 00:00:00 2001 From: jhugman Date: Tue, 1 Oct 2024 00:18:35 +0100 Subject: [PATCH] Allow record default properties to be overridden (#103) Fixes #101 . According to [The Big O of Code Reviews](https://www.egorand.dev/the-big-o-of-code-reviews/), this is a O(_n_) change. This PR adds a test for the behaviour described in #101, and a fix. --- typescript/src/records.ts | 2 +- typescript/tests/playground/records.ts | 30 ++++++++++++++++++++ typescript/tests/records.test.ts | 38 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 typescript/tests/playground/records.ts create mode 100644 typescript/tests/records.test.ts diff --git a/typescript/src/records.ts b/typescript/src/records.ts index 66b55359..e9db22e8 100644 --- a/typescript/src/records.ts +++ b/typescript/src/records.ts @@ -12,6 +12,6 @@ export const uniffiCreateRecord = >( defaults: () => D, ) => { type MissingKeys = Omit; - return (partial: Required & Partial): T => + return (partial: Partial & Required): T => Object.freeze({ ...defaults(), ...partial } as T); }; diff --git a/typescript/tests/playground/records.ts b/typescript/tests/playground/records.ts new file mode 100644 index 00000000..31b1a6b5 --- /dev/null +++ b/typescript/tests/playground/records.ts @@ -0,0 +1,30 @@ +/* + * 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 { uniffiCreateRecord } from "../../src/records"; + +export type MyRecord = { + string: string; + number: number; + optionalString: string | undefined; + bool: boolean; + optionalBool: boolean | undefined; +}; + +export const MyRecord = (() => { + const defaults = () => ({ + string: "default", + optionalString: undefined, + }); + + const create = uniffiCreateRecord>( + defaults, + ); + + return { + create, + }; +})(); diff --git a/typescript/tests/records.test.ts b/typescript/tests/records.test.ts new file mode 100644 index 00000000..ed8521ab --- /dev/null +++ b/typescript/tests/records.test.ts @@ -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 { Asserts, test, xtest } from "../testing/asserts"; +import { console } from "../testing/hermes"; +import { MyRecord } from "./playground/records"; + +test("Allow defaults to be missing", (t) => { + const v: MyRecord = MyRecord.create({ + number: 42, + bool: true, + optionalBool: undefined, + }); + t.assertEqual(v.string, "default"); + t.assertEqual(v.optionalString, undefined); + t.assertEqual(v.number, 42); + t.assertEqual(v.bool, true); + t.assertEqual(v.optionalBool, undefined); +}); + +test("Allow defaults to be overridden", (t) => { + const v: MyRecord = MyRecord.create({ + string: "overridden", + optionalString: "also overridden", + number: 43, + bool: false, + optionalBool: true, + }); + + t.assertEqual(v.string, "overridden"); + t.assertEqual(v.optionalString, "also overridden"); + t.assertEqual(v.number, 43); + t.assertEqual(v.bool, false); + t.assertEqual(v.optionalBool, true); +});