-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #479 from streamich/json-type-value-2
Json type value 2
- Loading branch information
Showing
33 changed files
with
270 additions
and
145 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -221,6 +221,7 @@ | |
"json-stable", | ||
"json-text", | ||
"json-type", | ||
"json-type-value", | ||
"reactive-rpc", | ||
"util" | ||
] | ||
|
This file contains 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 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 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,96 @@ | ||
import {Value} from './Value'; | ||
import {toText} from '../json-type/typescript/toText'; | ||
import type {ResolveType} from '../json-type'; | ||
import type * as classes from '../json-type/type'; | ||
import type * as ts from '../json-type/typescript/types'; | ||
|
||
type UnObjectType<T> = T extends classes.ObjectType<infer U> ? U : never; | ||
type UnObjectFieldTypeVal<T> = T extends classes.ObjectFieldType<any, infer U> ? U : never; | ||
|
||
// export type MergeObjectsTypes<A, B> = | ||
// A extends classes.ObjectType<infer A2> | ||
// ? B extends classes.ObjectType<infer B2> | ||
// ? classes.ObjectType<[...A2, ...B2]> : | ||
// never : | ||
// never; | ||
|
||
// export type MergeObjectValues<A, B> = | ||
// A extends ObjectValue<infer A2> | ||
// ? B extends ObjectValue<infer B2> | ||
// ? ObjectValue<MergeObjectsTypes<A2, B2>> : | ||
// never : | ||
// never; | ||
|
||
export class ObjectValue<T extends classes.ObjectType<any>> extends Value<T> { | ||
public field<F extends classes.ObjectFieldType<any, any>>( | ||
field: F, | ||
data: ResolveType<UnObjectFieldTypeVal<F>>, | ||
): ObjectValue<classes.ObjectType<[...UnObjectType<T>, F]>> { | ||
const extendedData = {...this.data, [field.key]: data}; | ||
const type = this.type; | ||
const system = type.system; | ||
if (!system) throw new Error('NO_SYSTEM'); | ||
const extendedType = system.t.Object(...type.fields, field); | ||
return new ObjectValue(extendedType, extendedData) as any; | ||
} | ||
|
||
public prop<K extends string, V extends classes.Type>(key: K, type: V, data: ResolveType<V>) { | ||
const system = type.system; | ||
if (!system) throw new Error('NO_SYSTEM'); | ||
return this.field(system.t.prop(key, type), data); | ||
} | ||
|
||
public merge<O extends ObjectValue<any>>( | ||
obj: O, | ||
): ObjectValue<classes.ObjectType<[...UnObjectType<T>, ...UnObjectType<O['type']>]>> { | ||
const extendedData = {...this.data, ...obj.data}; | ||
const type = this.type; | ||
const system = type.system; | ||
if (!system) throw new Error('NO_SYSTEM'); | ||
const extendedType = system.t.Object(...type.fields, ...obj.type.fields); | ||
return new ObjectValue(extendedType, extendedData) as any; | ||
} | ||
|
||
public toTypeScriptAst(): ts.TsTypeLiteral { | ||
const node: ts.TsTypeLiteral = { | ||
node: 'TypeLiteral', | ||
members: [], | ||
}; | ||
const data = this.data as Record<string, classes.Type>; | ||
for (const [name, type] of Object.entries(data)) { | ||
const schema = type.getSchema(); | ||
const property: ts.TsPropertySignature = { | ||
node: 'PropertySignature', | ||
name, | ||
type: type.toTypeScriptAst(), | ||
}; | ||
if (schema.title) property.comment = schema.title; | ||
node.members.push(property); | ||
} | ||
return node; | ||
} | ||
|
||
public toTypeScriptModuleAst(): ts.TsModuleDeclaration { | ||
const node: ts.TsModuleDeclaration = { | ||
node: 'ModuleDeclaration', | ||
name: 'Router', | ||
export: true, | ||
statements: [ | ||
{ | ||
node: 'TypeAliasDeclaration', | ||
name: 'Routes', | ||
type: this.toTypeScriptAst(), | ||
export: true, | ||
}, | ||
], | ||
}; | ||
const system = this.type.system; | ||
if (!system) throw new Error('system is undefined'); | ||
for (const alias of system.aliases.values()) node.statements.push({...alias.toTypeScriptAst(), export: true}); | ||
return node; | ||
} | ||
|
||
public toTypeScript(): string { | ||
return toText(this.toTypeScriptModuleAst()); | ||
} | ||
} |
This file contains 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,5 @@ | ||
import type {ResolveType, Type} from '../json-type'; | ||
|
||
export class Value<T extends Type> { | ||
constructor(public type: T, public data: ResolveType<T>) {} | ||
} |
This file contains 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,11 @@ | ||
import {Value} from './Value'; | ||
import {ObjectValue} from './ObjectValue'; | ||
import * as classes from '../json-type/type'; | ||
|
||
export const value: { | ||
<T extends classes.ObjectType>(type: T, data: unknown): ObjectValue<T>; | ||
<T extends classes.Type>(type: T, data: unknown): Value<T>; | ||
} = (type: any, data: any): any => { | ||
if (type instanceof classes.ObjectType) return new ObjectValue(type as classes.ObjectType, <any>data); | ||
return new Value(type as classes.Type, <any>data); | ||
}; |
This file contains 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
2 changes: 1 addition & 1 deletion
2
src/json-type/codegen/capacity/CapacityEstimatorCodegenContext.ts
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.