Skip to content

Commit e14655a

Browse files
committed
feat(json-type-value): 🎸 add ability to print ObjectValue
1 parent eeae664 commit e14655a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/json-type-value/ObjectValue.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {Value} from './Value';
22
import {toText} from '../json-type/typescript/toText';
33
import {TypeSystem} from '../json-type/system/TypeSystem';
4+
import {printTree} from 'sonic-forest/lib/print/printTree';
45
import type {ResolveType} from '../json-type';
56
import type * as classes from '../json-type/type';
67
import type * as ts from '../json-type/typescript/types';
78
import type {TypeBuilder} from '../json-type/type/TypeBuilder';
9+
import type {Printable} from 'sonic-forest/lib/print/types';
810

911
export type UnObjectType<T> = T extends classes.ObjectType<infer U> ? U : never;
1012
export type UnObjectValue<T> = T extends ObjectValue<infer U> ? U : never;
@@ -31,7 +33,7 @@ export type TuplesToFields<T> = T extends PropDefinition<infer K, infer V>[] ? c
3133
type PropDefinition<K extends string, V extends classes.Type> = [key: K, val: V, data: ResolveType<V>];
3234
type PropDef = <K extends string, V extends classes.Type>(key: K, val: V, data: ResolveType<V>) => PropDefinition<K, V>;
3335

34-
export class ObjectValue<T extends classes.ObjectType<any>> extends Value<T> {
36+
export class ObjectValue<T extends classes.ObjectType<any>> extends Value<T> implements Printable {
3537
public static create = (system: TypeSystem = new TypeSystem()) => new ObjectValue(system.t.obj, {});
3638

3739
public get system(): TypeSystem {
@@ -155,4 +157,8 @@ export class ObjectValue<T extends classes.ObjectType<any>> extends Value<T> {
155157
public toTypeScript(): string {
156158
return toText(this.toTypeScriptModuleAst());
157159
}
160+
161+
public toString(tab: string = ''): string {
162+
return this.constructor.name + printTree(tab, [(tab) => this.type.toString(tab)]);
163+
}
158164
}

src/json-type-value/__tests__/ObjectValue.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ test('can retrieve field as Value', () => {
99
expect(foo.type.getTypeName()).toBe('str');
1010
expect(foo.data).toBe('bar');
1111
});
12+
13+
test('can print to string', () => {
14+
const system = new TypeSystem();
15+
const {t} = system;
16+
const obj = new ObjectValue(t.Object(t.prop('foo', t.str)), {foo: 'bar'});
17+
expect(obj + '').toMatchSnapshot();
18+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`can print to string 1`] = `
4+
"ObjectValue
5+
└─ obj
6+
└─ "foo":
7+
└─ str"
8+
`;

0 commit comments

Comments
 (0)