Skip to content

Commit

Permalink
Allow record default properties to be overridden (#103)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jhugman authored Sep 30, 2024
1 parent 0c36911 commit 1fa0a52
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion typescript/src/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export const uniffiCreateRecord = <T, D extends Partial<T>>(
defaults: () => D,
) => {
type MissingKeys = Omit<T, keyof D>;
return (partial: Required<MissingKeys> & Partial<D>): T =>
return (partial: Partial<T> & Required<MissingKeys>): T =>
Object.freeze({ ...defaults(), ...partial } as T);
};
30 changes: 30 additions & 0 deletions typescript/tests/playground/records.ts
Original file line number Diff line number Diff line change
@@ -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<MyRecord, ReturnType<typeof defaults>>(
defaults,
);

return {
create,
};
})();
38 changes: 38 additions & 0 deletions typescript/tests/records.test.ts
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 { 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);
});

0 comments on commit 1fa0a52

Please sign in to comment.