/**
 * Generate typescript interface from table schema
 * Created by xiamx on 2016-08-10.
 */ import * as _ from 'lodash' ; import { TableDefinition } from './schemaInterfaces' ; import Options from './options' ; function nameIsReservedKeyword ( name ) { const reservedKeywords = [ 'string' , 'number' , 'package' ] ; return reservedKeywords . indexOf ( name ) !== - 1 ; } function normalizeName ( name , options ) { if ( nameIsReservedKeyword ( name ) ) { return name + '_' ; } else { return name ; } } export function generateTableInterface ( tableNameRaw , tableDefinition , options ) { const tableName = options . transformTypeName ( tableNameRaw ) ; let members = '' ; Object . keys ( tableDefinition ) . map ( c => options . transformColumnName ( c ) ) . forEach ( ( columnName ) => { members += `${ columnName }: ${ tableName }Fields.${ normalizeName ( columnName , options ) };\n` ; } ) ; return `
        export interface ${ normalizeName ( tableName , options ) } {
        ${ members }
        }
    ` ; } export function generateEnumType ( enumObject , options ) { let enumString = '' ; for ( let enumNameRaw in enumObject ) { const enumName = options . transformTypeName ( enumNameRaw ) ; enumString += `export type ${ enumName } = ` ; enumString += enumObject [ enumNameRaw ] . map ( ( v ) => `'${ v }'` ) . join ( ' | ' ) ; enumString += ';\n' ; } return enumString ; } export function generateTableTypes ( tableNameRaw , tableDefinition , options ) { const tableName = options . transformTypeName ( tableNameRaw ) ; let fields = '' ; Object . keys ( tableDefinition ) . forEach ( ( columnNameRaw ) => { let type = tableDefinition [ columnNameRaw ] . tsType ; let nullable = tableDefinition [ columnNameRaw ] . nullable ? '| null' : '' ; const columnName = options . transformColumnName ( columnNameRaw ) ; fields += `export type ${ normalizeName ( columnName , options ) } = ${ type }${ nullable };\n` ; } ) ; return `
        export namespace ${ tableName }Fields {
        ${ fields }
        }
    ` ; }