Skip to content

Commit

Permalink
Indexed Access Types #5
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 16, 2023
2 parents e4f1565 + a75cb67 commit b717629
Show file tree
Hide file tree
Showing 29 changed files with 500 additions and 79 deletions.
Empty file added .github/.keep
Empty file.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: GitHub CI

on: [push, pull_request]

jobs:
TypeBox:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node: [18.x]
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v2
- name: Install Node
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- name: Install Packages
run: npm install
- name: Build Library
run: npm run build
- name: Test Library
run: npm run test
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"files.exclude": {
"node_modules": true,
"package0lock.json": true
"package-lock.json": true,
".git-hooks": true
}
}
22 changes: 5 additions & 17 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { Type, Static } from '@sinclair/typebox'
import * as Codegen from '@typebox/codegen'

export type Vector = Static<typeof Vector>
export const Vector = Type.Recursive(
(This) =>
Type.Object({
x: Type.Number(),
y: Type.Number(),
z: This,
}),
{ $id: 'Vector' },
)
function Print(transform: string, code: any) {
const length = 72
console.log('┌' + '─'.repeat(length) + '┐')
Expand All @@ -21,14 +11,12 @@ function Print(transform: string, code: any) {
console.log('')
}
const Code = `
type A = string
type B = A
type C = B
interface Vector {
a: A
b: B
export interface Vector {
x: number
y: number
z: number
}
type A = Vector['x']
`
// ----------------------------------------------------------------------------
// Immediate Transform
Expand Down
22 changes: 18 additions & 4 deletions hammer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@
// Clean
// -------------------------------------------------------------------------------
export async function clean() {
await folder('target').delete()
await folder('target').delete()
}
// -------------------------------------------------------------------------------
// Format
// -------------------------------------------------------------------------------
export async function format() {
await shell('prettier --write src example')
await shell('prettier --write src test example')
}
// -------------------------------------------------------------------------------
// Test
// -------------------------------------------------------------------------------
export async function test(filter = '') {
const pattern = filter.length > 0 ? `"--test-name-pattern=${filter}.*"` : ''
await shell('hammer build test/index.ts --dist target/test --platform node')
await shell(`node --test ${pattern} target/test/index.js`)
}
// -------------------------------------------------------------------------------
// Serve
// -------------------------------------------------------------------------------
export async function serve() {
await shell('hammer serve example/index.html --dist target/serve')
await shell('hammer serve example/index.html --dist target/example')
}
// -------------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------------
export async function start() {
await shell('hammer run example/index.ts --dist target/start')
await shell('hammer run example/index.ts --dist target/example')
}
// -------------------------------------------------------------------------------
// Build
// -------------------------------------------------------------------------------
export async function build() {
await shell('tsc -p src/tsconfig.json --outDir target/build --declaration')
}
27 changes: 20 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
"version": "0.0.1",
"description": "Code Generation for TypeBox Types",
"main": "index.js",
"directories": {
"example": "example"
},
"keywords": [
"typebox",
"codegen"
],
"scripts": {
"build": "hammer task build",
"clean": "hammer task clean",
"format": "hammer task format",
"start": "hammer task start",
"serve": "hammer task serve"
"serve": "hammer task serve",
"test": "hammer task test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sinclairzx81/typebox-codegen.git"
},
"keywords": [
"typebox",
"codegen"
],
"author": "sinclairzx81",
"license": "MIT",
"homepage": "https://github.com/sinclairzx81/typebox-codegen#readme",
"devDependencies": {
"@sinclair/hammer": "^0.17.2",
"@types/node": "^18.15.11",
"@types/prettier": "^2.7.2"
},
"dependencies": {
"@sinclair/typebox": "^0.27.3",
"@sinclair/typebox": "^0.27.8",
"prettier": "^2.8.7",
"typescript": "^5.0.4"
},
Expand Down
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*--------------------------------------------------------------------------
@typebox/codegen
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './encoder'
export * from './formatter'
9 changes: 3 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './model-to-json-schema'
export * from './model-to-typebox'
export * from './model-to-typescript'
export * from './model-to-zod'
export * from './typescript-to-model'
export * from './typescript-to-typebox'
export * from './common'
export * from './model'
export * from './typescript'
31 changes: 31 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*--------------------------------------------------------------------------
@typebox/codegen
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './model-to-json-schema'
export * from './model-to-typebox'
export * from './model-to-typescript'
export * from './model-to-zod'
export * from './model'
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeBoxModel } from './model'
import { Formatter } from './formatter'
import { Formatter } from '../common/formatter'
import { Value } from '@sinclair/typebox/value'

export namespace ModelToJsonSchema {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import * as Types from '@sinclair/typebox'
import { Formatter } from './formatter'
import { Formatter } from '../common/index'
import { TypeBoxModel } from './model'

export namespace ModelToTypeScript {
Expand Down
5 changes: 2 additions & 3 deletions src/model-to-zod.ts → src/model/model-to-zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import * as Types from '@sinclair/typebox'
import { Formatter, PropertyEncoder } from '../common/index'
import { TypeBoxModel } from './model'
import { Formatter } from './formatter'
import { PropertyEncoder } from './encoder'
import * as Types from '@sinclair/typebox'

// --------------------------------------------------------------------------
// Errors
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.json",
"files": ["index.ts"]
}
28 changes: 28 additions & 0 deletions src/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*--------------------------------------------------------------------------
@typebox/codegen
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---------------------------------------------------------------------------*/

export * from './typescript-to-model'
export * from './typescript-to-typebox'
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,19 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeScriptToTypeBox } from './typescript-to-typebox'
import { Type, TSchema } from '@sinclair/typebox'
import { TypeBoxModel } from './model'
import { Type, TypeClone, TSchema } from '@sinclair/typebox'
import { TypeBoxModel } from '../model/model'
import * as ts from 'typescript'

// -------------------------------------------------------------------------------
// This code runs an evaluation pass over TypeBox script and produces a runtime
// model that encodes all types found within the script.
// -------------------------------------------------------------------------------
export namespace TypeScriptToModel {
const compilerOptions: ts.CompilerOptions = {
module: ts.ModuleKind.CommonJS,
module: ts.ModuleKind.CommonJS, // used for exports
target: ts.ScriptTarget.ES2015, // evaluation target
}
export function Exports(code: string): Map<string, TSchema | Function> {
const exports = {}
const evaluate = new Function('exports', 'Type', code)
evaluate(exports, Type)
const evaluate = new Function('exports', 'Type', 'TypeClone', code)
evaluate(exports, Type, TypeClone)
return new Map(globalThis.Object.entries(exports))
}
export function Types(exports: Map<string, TSchema | Function>): TSchema[] {
Expand Down
Loading

0 comments on commit b717629

Please sign in to comment.