Skip to content

Commit

Permalink
feat: Export resolve/validator APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-greene-ck committed Apr 16, 2019
1 parent aec1de1 commit 7d719f8
Show file tree
Hide file tree
Showing 30 changed files with 721 additions and 637 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"semi": false,
"singleQuote": true,
"arrowParens": "always"
}
}
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"clean": "rimraf ./coverage ./dist ./**/codegen",
"clean:all": "npm run clean && rimraf ./node_modules package-lock.json",
"codegen": "node ./dist/main/bin/index.js --sourceDir ./src/tests/integration/thrift --outDir ./src/tests/integration/apache/codegen",
"codegen": "node ./dist/main/bin/index.js --target apache --sourceDir ./src/tests/integration/thrift --outDir ./src/tests/integration/apache/codegen",
"prebuild": "npm run clean && npm run lint && npm run format",
"build": "npm run build:only",
"build:only": "tsc",
Expand Down Expand Up @@ -57,7 +57,7 @@
"chai": "^4.2.0",
"mocha": "^5.2.0",
"nyc": "^13.3.0",
"prettier": "^1.15.3",
"prettier": "^1.17.0",
"rimraf": "^2.6.2",
"source-map-support": "^0.5.9",
"thrift": "^0.11.0",
Expand Down
2 changes: 1 addition & 1 deletion src/main/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { rendererForTarget } from '../render'
import { processStatements, renderStatement } from './iterator'

import { exportsForFile } from '../resolver/utils'
import { exportsForFile } from '../resolver'
import {
IGeneratedFile,
INamespace,
Expand Down
188 changes: 128 additions & 60 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as path from 'path'
import { parseFromSource, parseThriftFile } from './parser'

import {
CompileTarget,
IFileExports,
IGeneratedFile,
IMakeOptions,
Expand Down Expand Up @@ -32,9 +31,19 @@ import { print } from './printer'
import { readThriftFile } from './reader'
import { rendererForTarget } from './render'
import { resolveFile } from './resolver'
import { exportsForFile } from './resolver/utils'
import { exportsForFile } from './resolver'
import { validateFile } from './validator'

import * as Parser from './parser'
import * as Resolver from './resolver'
import * as Validator from './validator'
import * as Utils from './utils'

export { Resolver }
export { Parser }
export { Validator }
export { Utils }

/**
* This function is mostly for testing purposes. It does not support includes.
* Given a string of Thrift IDL it will return a string of TypeScript. If the
Expand All @@ -45,29 +54,19 @@ import { validateFile } from './validator'
*/
export function make(
source: string,
target: CompileTarget = 'thrift-server',
strictUnions: boolean = false,
options: Partial<IMakeOptions> = {},
): string {
const options: IMakeOptions = mergeWithDefaults({
target,
strictUnions,
})
const parsedFile: IParsedFile = parseFromSource(source, options)
const resolvedFile: IResolvedFile = resolveFile(parsedFile, {}, '', options)
const validatedFile: IResolvedFile = validateFile(resolvedFile, {}, '')

if (validatedFile.errors.length > 0) {
throw new Error(`Shit broke`)
}
const mergedOptions: IMakeOptions = mergeWithDefaults(options)
const validatedFile: IResolvedFile = parseThriftSource(source, options)

const fileExports: IFileExports = exportsForFile(resolvedFile.body)
const fileExports: IFileExports = exportsForFile(validatedFile.body)
const state: IRenderState = {
options,
options: mergedOptions,
currentNamespace: {
type: 'Namespace',
namespace: emptyNamespace(),
files: {
[resolvedFile.sourceFile.fullPath]: resolvedFile,
[validatedFile.sourceFile.fullPath]: validatedFile,
},
exports: fileExports,
includedNamespaces: {},
Expand All @@ -85,41 +84,53 @@ export function make(
sourceDir: '',
outDir: '',
namespaces: {},
options,
options: mergedOptions,
},
}

return print(
processStatements(resolvedFile.body, state, rendererForTarget(target)),
processStatements(
validatedFile.body,
state,
rendererForTarget(mergedOptions.target),
),
)
}

export async function generate(options: Partial<IMakeOptions>): Promise<void> {
export function parseThriftSource(
source: string,
options: Partial<IMakeOptions> = {},
): IResolvedFile {
const mergedOptions: IMakeOptions = mergeWithDefaults(options)

// Root at which we operate relative to
const rootDir: string = path.resolve(process.cwd(), mergedOptions.rootDir)

// Where do we save generated files
const outDir: string = path.resolve(rootDir, mergedOptions.outDir)

// Where do we read source files
const sourceDir: string = path.resolve(rootDir, mergedOptions.sourceDir)

const fileNames: Array<string> = collectSourceFiles(
sourceDir,
mergedOptions,
const parsedFile: IParsedFile = parseFromSource(
source,
mergedOptions.fallbackNamespace,
)

const thriftFiles: Array<ISourceFile> = await Promise.all(
fileNames.map((next: string) => {
return readThriftFile(next, [sourceDir])
}),
const resolvedFile: IResolvedFile = resolveFile(
parsedFile,
{},
'',
mergedOptions.fallbackNamespace,
)
const validatedFile: IResolvedFile = validateFile(resolvedFile, {}, '')

if (validatedFile.errors.length > 0) {
throw new Error(`Unable to validate thrift source`)
}

return validatedFile
}

export async function parseThriftFiles(
thriftFiles: Array<ISourceFile>,
options: {
sourceDir: string
fallbackNamespace: string
},
): Promise<Array<IResolvedFile>> {
const parsedFiles: Array<IParsedFile> = thriftFiles.map(
(next: ISourceFile) => {
const parsed = parseThriftFile(next, mergedOptions)
const parsed = parseThriftFile(next, options.fallbackNamespace)
return parsed
},
)
Expand All @@ -134,7 +145,12 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {

const resolvedFiles: Array<IResolvedFile> = parsedFiles.map(
(next: IParsedFile) => {
return resolveFile(next, parsedFileMap, sourceDir, mergedOptions)
return resolveFile(
next,
parsedFileMap,
options.sourceDir,
options.fallbackNamespace,
)
},
)

Expand All @@ -144,7 +160,7 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {

if (resolvedInvalidFiles.length > 0) {
printErrors(resolvedInvalidFiles)
process.exitCode = 1
throw new Error(`Unable to parse Thrift files`)
} else {
const resolvedFileMap: ResolvedFileMap = resolvedFiles.reduce(
(acc: ResolvedFileMap, next: IResolvedFile) => {
Expand All @@ -155,7 +171,7 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {
)
const validatedFiles: Array<IResolvedFile> = resolvedFiles.map(
(next: IResolvedFile) => {
return validateFile(next, resolvedFileMap, sourceDir)
return validateFile(next, resolvedFileMap, options.sourceDir)
},
)

Expand All @@ -165,24 +181,76 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {

if (validatedInvalidFiles.length > 0) {
printErrors(validatedInvalidFiles)
process.exitCode = 1
throw new Error(`Unable to parse Thrift files`)
} else {
const namespaces: INamespaceMap = organizeByNamespace(resolvedFiles)

const thriftProject: IThriftProject = {
type: 'ThriftProject',
rootDir,
outDir,
sourceDir,
namespaces,
options: mergedOptions,
}

const generatedFiles: Array<IGeneratedFile> = generateProject(
thriftProject,
)

saveFiles(generatedFiles, outDir)
return validatedFiles
}
}
}

export async function readThriftFiles(options: {
rootDir: string
sourceDir: string
files?: Array<string>
}): Promise<Array<ISourceFile>> {
// Root at which we operate relative to
const rootDir: string = path.resolve(process.cwd(), options.rootDir)

// Where do we read source files
const sourceDir: string = path.resolve(rootDir, options.sourceDir)

const fileNames: Array<string> = collectSourceFiles(
sourceDir,
options.files,
)

const thriftFiles: Array<ISourceFile> = await Promise.all(
fileNames.map((next: string) => {
return readThriftFile(next, [sourceDir])
}),
)

return thriftFiles
}

export async function generate(options: Partial<IMakeOptions>): Promise<void> {
const mergedOptions: IMakeOptions = mergeWithDefaults(options)

// Root at which we operate relative to
const rootDir: string = path.resolve(process.cwd(), mergedOptions.rootDir)

// Where do we save generated files
const outDir: string = path.resolve(rootDir, mergedOptions.outDir)

// Where do we read source files
const sourceDir: string = path.resolve(rootDir, mergedOptions.sourceDir)

const thriftFiles: Array<ISourceFile> = await readThriftFiles({
rootDir,
sourceDir,
files: mergedOptions.files,
})

const validatedFiles: Array<IResolvedFile> = await parseThriftFiles(
thriftFiles,
{
sourceDir,
fallbackNamespace: mergedOptions.fallbackNamespace,
},
)

const namespaces: INamespaceMap = organizeByNamespace(validatedFiles)

const thriftProject: IThriftProject = {
type: 'ThriftProject',
rootDir,
outDir,
sourceDir,
namespaces,
options: mergedOptions,
}

const generatedFiles: Array<IGeneratedFile> = generateProject(thriftProject)

saveFiles(generatedFiles, outDir)
}
14 changes: 8 additions & 6 deletions src/main/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import {
ThriftDocument,
ThriftErrors,
} from '@creditkarma/thrift-parser'
import { exportsForFile } from '../resolver/utils'
import { exportsForFile } from '../resolver'
import {
IFileExports,
IFileIncludes,
IMakeOptions,
INamespacePath,
IParsedFile,
ISourceFile,
Expand All @@ -28,7 +27,7 @@ export function parseThriftString(source: string): ThriftDocument {

export function parseFromSource(
source: string,
options: IMakeOptions,
fallbackNamespace: string,
): IParsedFile {
const sourceFile: ISourceFile = {
type: 'SourceFile',
Expand All @@ -38,16 +37,19 @@ export function parseFromSource(
source,
}

return parseThriftFile(sourceFile, options)
return parseThriftFile(sourceFile, fallbackNamespace)
}

export function parseThriftFile(
file: ISourceFile,
options: IMakeOptions,
fallbackNamespace: string,
): IParsedFile {
const thriftDoc: ThriftDocument = parseThriftString(file.source)
const exports: IFileExports = exportsForFile(thriftDoc.body)
const namespace: INamespacePath = namespaceForFile(thriftDoc.body, options)
const namespace: INamespacePath = namespaceForFile(
thriftDoc.body,
fallbackNamespace,
)
const includes: IFileIncludes = includesForFile(thriftDoc.body, file)

return {
Expand Down
2 changes: 1 addition & 1 deletion src/main/render/apache/service/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
typeNodeForFieldType,
} from '../types'

import { resolveIdentifierName } from '../../../resolver/utils'
import { resolveIdentifierName } from '../../../resolver'
import { IRenderState } from '../../../types'

export function renderClient(
Expand Down
2 changes: 1 addition & 1 deletion src/main/render/apache/service/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import {
resolveIdentifierDefinition,
resolveIdentifierName,
} from '../../../resolver/utils'
} from '../../../resolver'
import { IRenderState } from '../../../types'

import {
Expand Down
2 changes: 1 addition & 1 deletion src/main/render/apache/struct/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { THRIFT_IDENTIFIERS, THRIFT_TYPES } from '../identifiers'
import {
resolveIdentifierDefinition,
resolveIdentifierName,
} from '../../../resolver/utils'
} from '../../../resolver'
import { READ_METHODS } from './methods'

/**
Expand Down
Loading

0 comments on commit 7d719f8

Please sign in to comment.