Skip to content

Commit

Permalink
feat: Expose annotations for structs and services
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-greene-ck committed Aug 31, 2018
1 parent 1d8e4a9 commit 32d3a99
Show file tree
Hide file tree
Showing 17 changed files with 1,722 additions and 604 deletions.
286 changes: 151 additions & 135 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,24 @@
"url": "https://github.com/creditkarma/thrift-typescript"
},
"dependencies": {
"@creditkarma/thrift-parser": "~1.1.3",
"glob": "^7.1.2",
"typescript": "~2.9.1"
"@creditkarma/thrift-parser": "~1.1.4",
"glob": "^7.1.3",
"typescript": "2.9.x"
},
"devDependencies": {
"@types/chai": "^4.1.2",
"@types/glob": "^5.0.33",
"@types/mocha": "^5.0.0",
"@types/chai": "^4.1.4",
"@types/glob": "^5.0.35",
"@types/mocha": "^5.2.5",
"@types/node": "^8.0.32",
"@types/rimraf": "^2.0.2",
"@types/thrift": "^0.10.7",
"chai": "^4.1.2",
"mocha": "^5.0.5",
"mocha": "^5.2.0",
"nyc": "^11.1.0",
"rimraf": "^2.6.2",
"source-map-support": "^0.5.4",
"source-map-support": "^0.5.9",
"thrift": "^0.11.0",
"tslint": "^5.10.0"
"tslint": "^5.11.0"
},
"nyc": {
"include": [
Expand Down
2 changes: 2 additions & 0 deletions src/main/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export function print(statements: Array<ts.Statement>, includePreface: boolean =
ts.ScriptKind.TS,
)
const bodyFile: ts.SourceFile = ts.updateSourceFileNode(rawSourceFile, statements)

if (includePreface) {
generatePreface(statements[0])
}

return printer.printBundle(ts.createBundle([bodyFile]))
}
3 changes: 3 additions & 0 deletions src/main/render/shared/identifiers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as ts from 'typescript'

export const COMMON_IDENTIFIERS = {
fieldAnnotations: ts.createIdentifier('_fieldAnnotations'),
methodAnnotations: ts.createIdentifier('_methodAnnotations'),
annotations: ts.createIdentifier('_annotations'),
break: ts.createIdentifier('break'),
success: ts.createIdentifier('success'),
handler: ts.createIdentifier('handler'),
Expand Down
109 changes: 109 additions & 0 deletions src/main/render/thrift-server/annotations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import {
Annotation,
Annotations,
FieldDefinition,
FunctionDefinition,
ServiceDefinition,
} from '@creditkarma/thrift-parser'
import * as ts from 'typescript'

import {
COMMON_IDENTIFIERS,
THRIFT_IDENTIFIERS,
} from './identifiers'

function renderAnnotationValue(annotations?: Annotations): ts.ObjectLiteralExpression {
return ts.createObjectLiteral(
(
annotations !== undefined
? annotations.annotations.map((annotation: Annotation) => {
return ts.createPropertyAssignment(
ts.createIdentifier(annotation.name.value),
annotation.value !== undefined
? ts.createLiteral(annotation.value.value)
: ts.createLiteral(''),
)
})
: []
),
true,
)
}

export function renderAnnotations(annotations?: Annotations): ts.PropertyDeclaration {
return ts.createProperty(
undefined,
[
ts.createToken(ts.SyntaxKind.PublicKeyword),
ts.createToken(ts.SyntaxKind.ReadonlyKeyword),
],
COMMON_IDENTIFIERS.annotations,
undefined,
ts.createTypeReferenceNode(THRIFT_IDENTIFIERS.IThriftAnnotations, undefined),
renderAnnotationValue(annotations),
)
}

function renderFieldAnnotationValue(fields: Array<FieldDefinition>): ts.ObjectLiteralExpression {
return ts.createObjectLiteral(
fields.filter((field: FieldDefinition) => {
return field.annotations !== undefined
}).map((field: FieldDefinition) => {
return ts.createPropertyAssignment(
ts.createIdentifier(field.name.value),
renderAnnotationValue(field.annotations),
)
}),
true,
)
}

export function renderFieldAnnotations(fields: Array<FieldDefinition>): ts.PropertyDeclaration {
return ts.createProperty(
undefined,
[
ts.createToken(ts.SyntaxKind.PublicKeyword),
ts.createToken(ts.SyntaxKind.ReadonlyKeyword),
],
COMMON_IDENTIFIERS.fieldAnnotations,
undefined,
ts.createTypeReferenceNode(THRIFT_IDENTIFIERS.IFieldAnnotations, undefined),
renderFieldAnnotationValue(fields),
)
}

function renderMethodAnnotationValue(service: ServiceDefinition): ts.ObjectLiteralExpression {
return ts.createObjectLiteral(
service.functions.map((func: FunctionDefinition) => {
return ts.createPropertyAssignment(
ts.createIdentifier(func.name.value),
ts.createObjectLiteral([
ts.createPropertyAssignment(
ts.createIdentifier('annotations'),
renderAnnotationValue(func.annotations),
),
ts.createPropertyAssignment(
ts.createIdentifier('fieldAnnotations'),
renderFieldAnnotationValue(func.fields),
),
], true),
// renderAnnotationValue(func.annotations),
)
}),
true,
)
}

export function renderMethodAnnotations(service: ServiceDefinition): ts.PropertyDeclaration {
return ts.createProperty(
undefined,
[
ts.createToken(ts.SyntaxKind.PublicKeyword),
ts.createToken(ts.SyntaxKind.ReadonlyKeyword),
],
COMMON_IDENTIFIERS.methodAnnotations,
undefined,
ts.createTypeReferenceNode(THRIFT_IDENTIFIERS.IMethodAnnotations, undefined),
renderMethodAnnotationValue(service),
)
}
3 changes: 3 additions & 0 deletions src/main/render/thrift-server/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as ts from 'typescript'
export * from '../shared/identifiers'

export const THRIFT_IDENTIFIERS = {
IThriftAnnotations: ts.createIdentifier('thrift.IThriftAnnotations'),
IFieldAnnotations: ts.createIdentifier('thrift.IFieldAnnotations'),
IMethodAnnotations: ts.createIdentifier('thrift.IMethodAnnotations'),
IStructCodec: ts.createIdentifier('thrift.IStructCodec'),
IThriftConnection: ts.createIdentifier('thrift.IThriftConnection'),
ProtocolConstructor: ts.createIdentifier('thrift.IProtocolConstructor'),
Expand Down
23 changes: 15 additions & 8 deletions src/main/render/thrift-server/service/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ import {
IIdentifierMap,
} from '../../../types'

import { renderAnnotations, renderMethodAnnotations } from '../annotations'
import {
codecName,
looseName,
strictName,
} from '../struct/utils'

export function renderClient(node: ServiceDefinition, identifiers: IIdentifierMap): ts.ClassDeclaration {
export function renderClient(service: ServiceDefinition, identifiers: IIdentifierMap): ts.ClassDeclaration {
// private _requestId: number;
const requestId: ts.PropertyDeclaration = createProtectedProperty(
'_requestId',
Expand Down Expand Up @@ -86,6 +87,10 @@ export function renderClient(node: ServiceDefinition, identifiers: IIdentifierMa
createConnectionType(),
)

const annotations: ts.PropertyDeclaration = renderAnnotations(service.annotations)

const methodAnnotations: ts.PropertyDeclaration = renderMethodAnnotations(service)

/**
* constructor(connection: ThriftConnection) {
* super(connection)
Expand All @@ -103,7 +108,7 @@ export function renderClient(node: ServiceDefinition, identifiers: IIdentifierMa
),
], // parameters
[
...createSuperCall(node),
...createSuperCall(service),
createAssignmentStatement(
ts.createIdentifier('this._requestId'),
ts.createLiteral(0),
Expand Down Expand Up @@ -135,20 +140,20 @@ export function renderClient(node: ServiceDefinition, identifiers: IIdentifierMa
ts.createBlock([
ts.createReturn(
ts.createBinary(
ts.createIdentifier('this._requestId'),
ts.SyntaxKind.PlusEqualsToken,
ts.createLiteral(1),
ts.createIdentifier('this._requestId'),
ts.SyntaxKind.PlusEqualsToken,
ts.createLiteral(1),
),
),
], true),
)

const baseMethods: Array<ts.MethodDeclaration> = node.functions.map((func: FunctionDefinition) => {
const baseMethods: Array<ts.MethodDeclaration> = service.functions.map((func: FunctionDefinition) => {
return createBaseMethodForDefinition(func, identifiers)
})

const heritage: Array<ts.HeritageClause> = (
(node.extends !== null) ?
(service.extends !== null) ?
[
ts.createHeritageClause(
ts.SyntaxKind.ExtendsKeyword,
Expand All @@ -160,7 +165,7 @@ export function renderClient(node: ServiceDefinition, identifiers: IIdentifierMa
undefined,
),
],
ts.createIdentifier(`${node.extends.value}.Client`),
ts.createIdentifier(`${service.extends.value}.Client`),
),
],
),
Expand All @@ -186,6 +191,8 @@ export function renderClient(node: ServiceDefinition, identifiers: IIdentifierMa
transport,
protocol,
connection,
annotations,
methodAnnotations,
ctor,
incrementRequestIdMethod,
...baseMethods,
Expand Down
24 changes: 15 additions & 9 deletions src/main/render/thrift-server/service/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
typeNodeForFieldType,
} from '../types'

import { renderAnnotations, renderMethodAnnotations } from '../annotations'
import {
codecName, strictName,
} from '../struct/utils'
Expand Down Expand Up @@ -80,47 +81,50 @@ function handlerType(node: ServiceDefinition): ts.TypeNode {
)
}

export function renderProcessor(node: ServiceDefinition, identifiers: IIdentifierMap): ts.ClassDeclaration {
export function renderProcessor(service: ServiceDefinition, identifiers: IIdentifierMap): ts.ClassDeclaration {
// private _handler
const handler: ts.PropertyDeclaration = ts.createProperty(
undefined,
[ ts.createToken(ts.SyntaxKind.PublicKeyword) ],
'_handler',
undefined,
handlerType(node),
handlerType(service),
undefined,
)

const annotations: ts.PropertyDeclaration = renderAnnotations(service.annotations)
const methodAnnotations: ts.PropertyDeclaration = renderMethodAnnotations(service)

const ctor: ts.ConstructorDeclaration = createClassConstructor(
[
createFunctionParameter(
ts.createIdentifier('handler'),
handlerType(node),
handlerType(service),
),
],
[
...createSuperCall(node, identifiers),
...createSuperCall(service, identifiers),
createAssignmentStatement(
ts.createIdentifier('this._handler'),
ts.createIdentifier('handler'),
),
],
)

const processMethod: ts.MethodDeclaration = createProcessMethod(node, identifiers)
const processFunctions: Array<ts.MethodDeclaration> = node.functions.map((next: FunctionDefinition) => {
return createProcessFunctionMethod(node, next, identifiers)
const processMethod: ts.MethodDeclaration = createProcessMethod(service, identifiers)
const processFunctions: Array<ts.MethodDeclaration> = service.functions.map((next: FunctionDefinition) => {
return createProcessFunctionMethod(service, next, identifiers)
})

const heritage: Array<ts.HeritageClause> = (
(node.extends !== null) ?
(service.extends !== null) ?
[
ts.createHeritageClause(
ts.SyntaxKind.ExtendsKeyword,
[
ts.createExpressionWithTypeArguments(
[ ts.createTypeReferenceNode(COMMON_IDENTIFIERS.Context, undefined) ],
ts.createIdentifier(`${node.extends.value}.Processor`),
ts.createIdentifier(`${service.extends.value}.Processor`),
),
],
),
Expand All @@ -145,6 +149,8 @@ export function renderProcessor(node: ServiceDefinition, identifiers: IIdentifie
heritage, // heritage
[
handler,
annotations,
methodAnnotations,
ctor,
processMethod,
...processFunctions,
Expand Down
16 changes: 15 additions & 1 deletion src/main/render/thrift-server/struct/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
IResolvedIdentifier,
} from '../../../types'

import {
renderAnnotations,
renderFieldAnnotations,
} from '../annotations'

import {
COMMON_IDENTIFIERS,
THRIFT_IDENTIFIERS,
Expand Down Expand Up @@ -230,9 +235,18 @@ export function createInputParameter(): ts.ParameterDeclaration {
}

export function createFieldsForStruct(node: InterfaceWithFields, identifiers: IIdentifierMap): Array<ts.PropertyDeclaration> {
return node.fields.map((field: FieldDefinition) => {
const userFields: Array<ts.PropertyDeclaration> = node.fields.map((field: FieldDefinition) => {
return renderFieldDeclarations(field, identifiers)
})

const annotations: ts.PropertyDeclaration = renderAnnotations(node.annotations)
const fieldAnnotations: ts.PropertyDeclaration = renderFieldAnnotations(node.fields)

return [
...userFields,
annotations,
fieldAnnotations,
]
}

/**
Expand Down
Loading

0 comments on commit 32d3a99

Please sign in to comment.