Skip to content

Commit

Permalink
fix: Correctly handle annotations that are not valid identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hayes committed Nov 20, 2018
1 parent 444d9d4 commit c504b4a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/main/render/thrift-server/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import {
THRIFT_IDENTIFIERS,
} from './identifiers'

const validIdentifierPattern = /^[a-z$_][0-9a-z$_]*$/i

function renderAnnotationValue(annotations?: Annotations): ts.ObjectLiteralExpression {
return ts.createObjectLiteral(
(
annotations !== undefined
? annotations.annotations.map((annotation: Annotation) => {
const name = annotation.name.value
const identifier = validIdentifierPattern.test(name) ? name : `'${annotation.name.value}'`
return ts.createPropertyAssignment(
ts.createIdentifier(annotation.name.value),
identifier,
annotation.value !== undefined
? ts.createLiteral(annotation.value.value)
: ts.createLiteral(''),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,17 @@ export class MyException extends thrift.StructLike implements IMyException {
public readonly _annotations: thrift.IThriftAnnotations = {
foo: "bar",
two: "three",
alone: ""
alone: "",
'dot.foo': "bar",
'dot.lonely': ""
};
public readonly _fieldAnnotations: thrift.IFieldAnnotations = {
message: {
foo: "bar",
two: "three",
lonely: ""
lonely: "",
'dot.foo': "bar",
'dot.lonely': ""
}
};
constructor(args: IMyExceptionArgs = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,18 @@ export namespace MyService {
export const annotations: thrift.IThriftAnnotations = {
foo: "bar",
two: "three",
alone: ""
alone: "",
'dot.foo': "bar",
'dot.lonely': ""
};
export const methodAnnotations: thrift.IMethodAnnotations = {
getUser: {
annotations: {
foo: "bar",
two: "three",
lonely: ""
lonely: "",
'dot.foo': "bar",
'dot.lonely': ""
},
fieldAnnotations: {}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ export class MyStruct extends thrift.StructLike implements IMyStruct {
public readonly _annotations: thrift.IThriftAnnotations = {
foo: "bar",
two: "three",
alone: ""
alone: "",
'dot.foo': "bar",
'dot.lonely': ""
};
public readonly _fieldAnnotations: thrift.IFieldAnnotations = {
id: {
foo: "bar",
two: "three",
lonely: ""
lonely: "",
'dot.foo': "bar",
'dot.lonely': ""
}
};
constructor(args: IMyStructArgs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ export class MyUnion extends thrift.StructLike implements IMyUnion {
public readonly _annotations: thrift.IThriftAnnotations = {
foo: "bar",
two: "three",
alone: ""
alone: "",
'dot.foo': "bar",
'dot.lonely': ""
};
public readonly _fieldAnnotations: thrift.IFieldAnnotations = {
field1: {
foo: "bar",
two: "three",
lonely: ""
lonely: "",
'dot.foo': "bar",
'dot.lonely': ""
}
};
constructor(args: IMyUnionArgs = {}) {
Expand Down
16 changes: 8 additions & 8 deletions src/tests/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ describe('Thrift TypeScript Generator', () => {
it('should correctly generate a struct with annotations', () => {
const content: string = `
struct MyStruct {
1: required i32 id = 45 ( foo = "bar", two = "three", lonely )
1: required i32 id = 45 ( foo = "bar", two = "three", lonely, dot.foo = "bar", dot.lonely )
2: required i64 bigID = 23948234
} ( foo = "bar", two = "three", alone )
} ( foo = "bar", two = "three", alone, dot.foo = "bar", dot.lonely )
`
const expected: string = readSolution('annotations_struct', 'thrift-server')
const actual: string = make(content)
Expand Down Expand Up @@ -161,9 +161,9 @@ describe('Thrift TypeScript Generator', () => {
it('should correctly generate a union with annotations', () => {
const content: string = `
union MyUnion {
1: i32 field1 ( foo = "bar", two = "three", lonely )
1: i32 field1 ( foo = "bar", two = "three", lonely, dot.foo = "bar", dot.lonely )
2: i64 field2
} ( foo = "bar", two = "three", alone )
} ( foo = "bar", two = "three", alone, dot.foo = "bar", dot.lonely )
`
const expected: string = readSolution('annotations_union', 'thrift-server')
const actual: string = make(content)
Expand Down Expand Up @@ -215,9 +215,9 @@ describe('Thrift TypeScript Generator', () => {
it('should correctly generate an exception with annotations', () => {
const content: string = `
exception MyException {
1: string message ( foo = "bar", two = "three", lonely )
1: string message ( foo = "bar", two = "three", lonely, dot.foo = "bar", dot.lonely )
2: i32 code = 200
} ( foo = "bar", two = "three", alone )
} ( foo = "bar", two = "three", alone, dot.foo = "bar", dot.lonely )
`
const expected: string = readSolution('annotations_exception', 'thrift-server')
const actual: string = make(content)
Expand Down Expand Up @@ -283,10 +283,10 @@ describe('Thrift TypeScript Generator', () => {
}
service MyService {
User getUser(1: i32 id) ( foo = "bar", two = "three", lonely )
User getUser(1: i32 id) ( foo = "bar", two = "three", lonely, dot.foo = "bar", dot.lonely )
void saveUser(1: User user)
void ping()
} ( foo = "bar", two = "three", alone )
} ( foo = "bar", two = "three", alone, dot.foo = "bar", dot.lonely )
`
const expected: string = readSolution('annotations_service', 'thrift-server')
const actual: string = make(content)
Expand Down

0 comments on commit c504b4a

Please sign in to comment.