Skip to content
Snippets Groups Projects
Select Git revision
  • e9b57bb89bb4267efd3d1a4a7089cd166bf9751a
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

generate.ts

Blame
  • generate.ts 3.05 KiB
    /**
     * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author Alexander Rose <alexander.rose@weirdbyte.de>
     */
    
    import { validate } from './validate'
    import { Database, getTypeAndArgs, Filter } from './json-schema'
    
    function header (name: string, importDatabasePath = 'mol-data/db') {
        return `/**
     * Copyright (c) 2017-2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * Code-generated '${name}' schema file
     *
     * @author mol-star package (src/apps/schema-generator/generate)
     */
    
    import { Database, Column } from '${importDatabasePath}'
    
    import Schema = Column.Schema
    
    const str = Schema.str;
    const int = Schema.int;
    const float = Schema.float;
    const coord = Schema.coord;
    
    const Aliased = Schema.Aliased;
    const Matrix = Schema.Matrix;
    const Vector = Schema.Vector;
    const List = Schema.List;`
    }
    
    function footer (name: string) {
        return `
    export type ${name}_Schema = typeof ${name}_Schema;
    export interface ${name}_Database extends Database<${name}_Schema> { }`
    }
    
    const value: { [k: string]: (...args: any[]) => string } = {
        enum: function (type: string, values: string[]) {
            return `Aliased<'${values.join(`' | '`)}'>(${type})`
        },
        matrix: function (rows: number, cols: number) {
            return `Matrix(${rows}, ${cols})`
        },
        vector: function (dim: number) {
            return `Vector(${dim})`
        },
        list: function (type: 'str'|'int'|'float', separator: string) {
            if (type === 'int') {
                return `List('${separator}', x => parseInt(x, 10))`
            } else if (type === 'float') {
                return `List('${separator}', x => parseFloat(x))`
            } else {
                return `List('${separator}', x => x)`
            }
        }
    }
    
    const reSafePropertyName = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/
    function safePropertyString(name: string) {
        return name.match(reSafePropertyName) ? name : `'${name}'`
    }
    
    export function generate (name: string, schema: Database, fields?: Filter, importDatabasePath?: string) {
        const validationResult = validate(schema)
        if (validationResult !== true) {
            throw validationResult
        }