From 5d9cb44389766306cc5af7e7f46c65e6118f2919 Mon Sep 17 00:00:00 2001 From: Fabian Finke Date: Tue, 21 Nov 2023 21:58:56 +0100 Subject: [PATCH] chore(core): override toJSON and console log --- .changeset/quick-cameras-begin.md | 5 + packages/core/src/__test__/model.test.ts | 178 ++--------------------- packages/core/src/__test__/union.test.ts | 154 +------------------- packages/core/src/model.ts | 6 + 4 files changed, 34 insertions(+), 309 deletions(-) create mode 100644 .changeset/quick-cameras-begin.md diff --git a/.changeset/quick-cameras-begin.md b/.changeset/quick-cameras-begin.md new file mode 100644 index 0000000..c58d96d --- /dev/null +++ b/.changeset/quick-cameras-begin.md @@ -0,0 +1,5 @@ +--- +"@model-ts/core": minor +--- + +prettify instance logging diff --git a/packages/core/src/__test__/model.test.ts b/packages/core/src/__test__/model.test.ts index 507c2cf..16eaf1c 100644 --- a/packages/core/src/__test__/model.test.ts +++ b/packages/core/src/__test__/model.test.ts @@ -20,37 +20,9 @@ describe("without providers", () => { const decoded = MyModel.from({ foo: 42, bar: "omitted" }) expect(decoded).toBeInstanceOf(MyModel) expect(decoded).toMatchInlineSnapshot(` - MyModel { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| foo: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ foo: number }", - "props": Object { - "foo": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "MyModel", - "encode": [Function], "foo": 42, - "values": [Function], } `) }) @@ -71,37 +43,9 @@ describe("without providers", () => { const decoded = new MyModel({ foo: 42 }) expect(decoded).toBeInstanceOf(MyModel) expect(decoded).toMatchInlineSnapshot(` - MyModel { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| foo: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ foo: number }", - "props": Object { - "foo": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "MyModel", - "encode": [Function], "foo": 42, - "values": [Function], } `) }) @@ -120,14 +64,14 @@ describe("without providers", () => { test("it encodes a value", () => { expect(MyModel.from({ foo: 432 }).encode()).toEqual({ foo: 432, - _tag: MyModel._tag, + _tag: MyModel._tag }) }) test("it omits extraneous fields", () => { - expect(new MyModel({ foo: 432, bar: "omitted" } as any).encode()).toEqual( - { foo: 432, _tag: MyModel._tag } - ) + expect( + new MyModel({ foo: 432, bar: "omitted" } as any).encode() + ).toEqual({ foo: 432, _tag: MyModel._tag }) }) }) }) @@ -141,7 +85,7 @@ describe("with provider", () => { constValue: 42, get doubleConstValue() { return this.constValue * 2 - }, + } }, instanceProps: { getFooString(this: C) { @@ -150,8 +94,8 @@ describe("with provider", () => { num: 33, get tripleNum() { return this.num * 3 - }, - }, + } + } } class MyModel extends model("MyModel", SIMPLE_CODEC, PROVIDER) {} @@ -167,40 +111,9 @@ describe("with provider", () => { const decoded = MyModel.from({ foo: 42, bar: "omitted" }) expect(decoded).toBeInstanceOf(MyModel) expect(decoded).toMatchInlineSnapshot(` - MyModel { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| foo: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ foo: number }", - "props": Object { - "foo": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "MyModel", - "encode": [Function], "foo": 42, - "getFooString": [Function], - "num": 33, - "tripleNum": 99, - "values": [Function], } `) @@ -228,40 +141,9 @@ describe("with provider", () => { expect(decoded._tag).toEqual(MyModel._tag) expect(decoded).toMatchInlineSnapshot(` - MyModel { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| foo: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ foo: number }", - "props": Object { - "foo": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "MyModel", - "encode": [Function], "foo": 42, - "getFooString": [Function], - "num": 33, - "tripleNum": 99, - "values": [Function], } `) @@ -284,14 +166,14 @@ describe("with provider", () => { test("it encodes a value", () => { expect(MyModel.from({ foo: 432 }).encode()).toEqual({ foo: 432, - _tag: MyModel._tag, + _tag: MyModel._tag }) }) test("it omits extraneous fields", () => { - expect(new MyModel({ foo: 432, bar: "omitted" } as any).encode()).toEqual( - { foo: 432, _tag: MyModel._tag } - ) + expect( + new MyModel({ foo: 432, bar: "omitted" } as any).encode() + ).toEqual({ foo: 432, _tag: MyModel._tag }) }) }) @@ -314,37 +196,9 @@ describe("as io-ts codec", () => { Object { "_tag": "Right", "right": Object { - "model": MyModel { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| foo: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ foo: number }", - "props": Object { - "foo": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + "model": Object { "_tag": "MyModel", - "encode": [Function], "foo": 42, - "values": [Function], }, }, } diff --git a/packages/core/src/__test__/union.test.ts b/packages/core/src/__test__/union.test.ts index 85b2bdf..379ae4d 100644 --- a/packages/core/src/__test__/union.test.ts +++ b/packages/core/src/__test__/union.test.ts @@ -18,74 +18,18 @@ describe("without providers", () => { const decodedA = Union.from({ a: "a" }) expect(decodedA).toBeInstanceOf(A) expect(decodedA).toMatchInlineSnapshot(` - A { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| a: string |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ a: string }", - "props": Object { - "a": StringType { - "_tag": "StringType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "string", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "A", "a": "a", - "encode": [Function], - "values": [Function], } `) const decodedB = Union.from({ b: 42, c: "" }) expect(decodedB).toBeInstanceOf(B) expect(decodedB).toMatchInlineSnapshot(` - B { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| b: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ b: number }", - "props": Object { - "b": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "B", "b": 42, - "encode": [Function], - "values": [Function], } `) }) @@ -106,74 +50,18 @@ describe("without providers", () => { const decodedA = Union.from({ _tag: "A", a: "a" }) expect(decodedA).toBeInstanceOf(A) expect(decodedA).toMatchInlineSnapshot(` - A { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| a: string |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ a: string }", - "props": Object { - "a": StringType { - "_tag": "StringType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "string", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "A", "a": "a", - "encode": [Function], - "values": [Function], } `) const decodedB = Union.from({ _tag: "B", b: 42, c: "" }) expect(decodedB).toBeInstanceOf(B) expect(decodedB).toMatchInlineSnapshot(` - B { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| b: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ b: number }", - "props": Object { - "b": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + Object { "_tag": "B", "b": 42, - "encode": [Function], - "values": [Function], } `) }) @@ -203,8 +91,8 @@ describe("with provider", () => { constValue: 42, get doubleConstValue() { return this.constValue * 2 - }, - }, + } + } } class A extends model("A", t.type({ a: t.string })) {} @@ -230,37 +118,9 @@ describe("as io-ts codec", () => { Object { "_tag": "Right", "right": Object { - "union": B { - "_codec": ExactType { - "_tag": "ExactType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{| b: number |}", - "type": InterfaceType { - "_tag": "InterfaceType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "{ b: number }", - "props": Object { - "b": NumberType { - "_tag": "NumberType", - "decode": [Function], - "encode": [Function], - "is": [Function], - "name": "number", - "validate": [Function], - }, - }, - "validate": [Function], - }, - "validate": [Function], - }, + "union": Object { "_tag": "B", "b": 42, - "encode": [Function], - "values": [Function], }, }, } diff --git a/packages/core/src/model.ts b/packages/core/src/model.ts index 881c671..01feee1 100644 --- a/packages/core/src/model.ts +++ b/packages/core/src/model.ts @@ -171,6 +171,12 @@ export function model< return Object.fromEntries( Object.entries(this).filter(([key]) => keys.has(key)) ) + }, + toJSON(): t.TypeOf { + return { _tag: this._tag, ...this.values() } + }, + [Symbol.for("nodejs.util.inspect.custom")](): string { + return `${Model._tag} ${JSON.stringify(this.values(), null, 2)}` } }) Object.assign(this, provider?.instanceProps ?? {})