Repository navigation
feat(core)!: Codec.from #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
| --- | ||
| "@ckb-ccc/core": major | ||
| --- | ||
|
|
||
| feat(core)!: Codec.from | ||
|
|
This file contains hidden or 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 hidden or 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,105 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { bytesFrom } from "../bytes/index.js"; | ||
| import { mol } from "../molecule/index.js"; | ||
| import { Codec } from "./codec.js"; | ||
| import { codec, Entity } from "./entity.js"; | ||
|
|
||
| interface FooLike { | ||
| v: number; | ||
| } | ||
|
|
||
| /** | ||
| * A minimal entity decorated with `@codec`, used as the base class for the | ||
| * dispatch tests below. `Foo.from` marks values it produces with `via: "Foo"` | ||
| * so tests can assert which class' `from` actually ran. | ||
| */ | ||
| @codec(mol.table({ v: mol.Uint8 })) | ||
| class Foo extends Entity.Base<FooLike, Foo>() { | ||
| public readonly v: number; | ||
| public readonly via: string; | ||
|
|
||
| constructor(v: number, via: string) { | ||
| super(); | ||
| this.v = v; | ||
| this.via = via; | ||
| } | ||
|
|
||
| static override from(fooLike: FooLike | Foo): Foo { | ||
| if (fooLike instanceof Foo) { | ||
| return fooLike; | ||
| } | ||
| return new Foo(fooLike.v, "Foo"); | ||
| } | ||
|
|
||
| override clone(): Foo { | ||
| return new Foo(this.v, this.via); | ||
| } | ||
| } | ||
|
|
||
| /** A subclass of `Foo` that overrides `from` with distinguishable behavior. */ | ||
| class Bar extends Foo { | ||
| static override from(fooLike: FooLike | Foo): Foo { | ||
| if (fooLike instanceof Foo) { | ||
| return fooLike; | ||
| } | ||
| return new Foo(fooLike.v, "Bar"); | ||
| } | ||
| } | ||
|
|
||
| describe("codec decorator static method dispatch", () => { | ||
| test("Foo.encode/decode/fromBytes dispatch to Foo.from", () => { | ||
| const bytes = Foo.encode({ v: 1 }); | ||
| expect(Foo.decode(bytes).via).toBe("Foo"); | ||
| expect(Foo.fromBytes(bytes).via).toBe("Foo"); | ||
| }); | ||
|
|
||
| test("Bar.encode/decode/fromBytes dispatch to Bar.from, not the inherited Foo.from", () => { | ||
| // Bar does not redefine encode/decode/fromBytes; it inherits them from the | ||
| // shared Base installed by the `@codec` decorator on `Foo`. Calling them | ||
| // through `Bar` must still resolve `from` against `Bar`, since `this` is | ||
| // bound to the receiver of the call (`Bar`), not the class that was | ||
| // originally decorated (`Foo`). | ||
| const bytes = Bar.encode({ v: 1 }); | ||
| expect(Bar.decode(bytes).via).toBe("Bar"); | ||
| expect(Bar.fromBytes(bytes).via).toBe("Bar"); | ||
| }); | ||
|
|
||
| test("Bar used as a nested field codec dispatches to Bar.from, not Foo.from", () => { | ||
| // `mol.table` builds its field codec via `Codec.from(Bar)`, which reads | ||
| // `Bar.encode`/`Bar.decode`/`Bar.from` off `Bar` without ever detaching | ||
| // them from their receiver (no destructuring). This must preserve the | ||
| // correct `this` binding so nested encode/decode still dispatch to | ||
| // `Bar.from` and not to `Foo.from`. | ||
| const outer = mol.table({ inner: Bar }); | ||
|
|
||
| const encoded = outer.encode({ inner: { v: 1 } }); | ||
| const decoded = outer.decode(encoded); | ||
| expect(decoded.inner.via).toBe("Bar"); | ||
|
|
||
| const fromed = outer.from({ inner: { v: 1 } }); | ||
| expect(fromed.inner.via).toBe("Bar"); | ||
| }); | ||
|
|
||
| test("tearing encode off a class and calling it without a receiver throws", () => { | ||
| // This is expected, ordinary JavaScript behavior for static methods that | ||
| // rely on `this`: once detached from their receiver, they can no longer | ||
| // resolve `this.from`. The `@codec` decorator and `Codec.from` must not | ||
| // paper over this by rebinding `this` at property-read time; only the | ||
| // framework's own internal call sites (e.g. `Codec.from`) are responsible | ||
| // for calling these methods without tearing them off their class. | ||
| const { encode } = Bar; | ||
| expect(() => encode({ v: 1 })).toThrow(TypeError); | ||
| }); | ||
|
|
||
| test("Codec.from does not tear encode/decode/from off the source object", () => { | ||
| // Regression guard for `Codec.from`: it must keep a reference to the | ||
| // passed-in `codecLike` and call `codecLike.encode(...)` etc. as member | ||
| // calls (preserving `this`), rather than destructuring | ||
| // `{ encode, decode, from }` into standalone functions, which would lose | ||
| // the receiver and break subclass dispatch through nested codecs. | ||
| const rebound = Codec.from(Bar); | ||
| const encoded = rebound.encode({ v: 1 }); | ||
| expect(rebound.decode(bytesFrom(encoded)).via).toBe("Bar"); | ||
| expect(rebound.from({ v: 1 }).via).toBe("Bar"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.