Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/main/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class ThriftFileConverter {

generateFunction = (fn: FunctionDefinition) =>
`${fn.id.name}: (${
fn.fields.length ? this.generateStructContents([...fn.fields]) : ''
fn.fields.length ? this.generateStructContents([...fn.fields], true) : ''
}) => ${this.convertType(fn.returns)}`;

generateTypedef = (def: Typedef) => {
Expand Down Expand Up @@ -362,18 +362,18 @@ export class ThriftFileConverter {
generateStruct = ({id: {name}, fields}: Struct | Exception) =>
`export type ${name} = ${this.generateStructContents(fields)};`;

generateStructContents = (fields: Array<Field>) =>
`{|${fields
generateStructContents = (fields: Array<Field>, readOnly?: boolean) =>
`${readOnly === true ? '$ReadOnly<' : ''}{|${fields
.map((field: Field) => {
const valueType = field.valueType;
let optionalPrefix = this.isOptional(field) ? '?' : '';
let value =
valueType.type === 'Identifier'
? this.getIdentifier(valueType.name, 'type')
: this.convertType(valueType);
return `${field.name}${optionalPrefix}: ${optionalPrefix}${value};`;
: this.convertType(valueType, null, readOnly);
return `${field.name}${optionalPrefix}:${optionalPrefix}${value};`;
})
.join('\n')}|}`;
.join('\n')}|}${readOnly === true ? '>' : ''}`;

generateUnion = ({id: {name}, fields}: Union) =>
`export type ${name} = ${this.generateUnionContents(fields)};`;
Expand Down Expand Up @@ -557,7 +557,7 @@ export class ThriftFileConverter {
return false;
};

convertBaseType(t: AstNode, def?: Definition): string | void {
convertBaseType(t: AstNode, def?: ?Definition): string | void {
if (t.type !== 'BaseType') {
return undefined;
}
Expand Down Expand Up @@ -605,7 +605,7 @@ export class ThriftFileConverter {
return undefined;
}

convertMapType(t: AstNode, def?: Definition): string | void {
convertMapType(t: AstNode, def?: ?Definition, readOnly?: ?boolean): string | void {
if (t.type === 'Map' && def) {
const valueType = this.convertType(t.valueType);
if (def.type === 'Const' && def.value.type === 'ConstMap') {
Expand All @@ -627,7 +627,7 @@ export class ThriftFileConverter {
);
}
} else if (entry.key.type === 'Literal') {
return `'${entry.key.value}': ${valueType}`;
return readOnly === true ? `'${entry.key.value}': $ReadOnly<${valueType}>` : `'${entry.key.value}': ${valueType}`;
} else {
throw new Error('unsupported');
}
Expand All @@ -651,13 +651,13 @@ export class ThriftFileConverter {
return this.identifiersTable[def.name].type === 'Enum';
}

convertType(t: AstNode, def?: Definition): string {
convertType(t: AstNode, def?: ?Definition, readOnly?: boolean): string {
if (!t) {
throw new Error(`Assertion failed. t is not defined.`);
}
let type: string | void =
this.convertArrayType(t) ||
this.convertMapType(t, def) ||
this.convertMapType(t, def, readOnly) ||
this.convertEnumType(t) ||
this.convertBaseType(t, def);
if (type !== undefined) {
Expand Down