Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 17 additions & 4 deletions src/quicktype-core/language/JavaScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from "../Type";
import { matchType, directlyReachableSingleNamedType } from "../TypeUtils";
import { acronymOption, acronymStyle, AcronymStyleOptions } from "../support/Acronyms";
import { convertersOption, ConvertersOptions } from "../support/Converters";

import {
utf16LegalizeCharacters,
utf16StringEscape,
Expand All @@ -33,7 +35,8 @@ import { isES3IdentifierPart, isES3IdentifierStart } from "./JavaScriptUnicodeMa

export const javaScriptOptions = {
acronymStyle: acronymOption(AcronymStyleOptions.Pascal),
runtimeTypecheck: new BooleanOption("runtime-typecheck", "Verify JSON.parse results at runtime", true)
runtimeTypecheck: new BooleanOption("runtime-typecheck", "Verify JSON.parse results at runtime", true),
converters: convertersOption()
};

export type JavaScriptTypeAnnotations = {
Expand All @@ -56,7 +59,7 @@ export class JavaScriptTargetLanguage extends TargetLanguage {
}

protected getOptions(): Option<any>[] {
return [javaScriptOptions.runtimeTypecheck, javaScriptOptions.acronymStyle];
return [javaScriptOptions.runtimeTypecheck, javaScriptOptions.acronymStyle, javaScriptOptions.converters];
}

get stringTypeMapping(): StringTypeMapping {
Expand Down Expand Up @@ -255,7 +258,7 @@ export class JavaScriptRenderer extends ConvenienceRenderer {
}

protected emitConvertModuleBody(): void {
this.forEachTopLevel("interposing", (t, name) => {
const converter = (t: Type, name: Name) => {
const typeMap = this.typeMapTypeFor(t);
this.emitBlock([this.deserializerFunctionLine(t, name), " "], "", () => {
if (!this._jsOptions.runtimeTypecheck) {
Expand All @@ -273,7 +276,17 @@ export class JavaScriptRenderer extends ConvenienceRenderer {
this.emitLine("return JSON.stringify(uncast(value, ", typeMap, "), null, 2);");
}
});
});
};

switch (this._jsOptions.converters) {
case ConvertersOptions.AllObjects:
this.forEachObject("interposing", converter);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#naming-is-hard

Is All the right name for this? My assumption is this wont include enums?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to AllObjects.

break;

default:
this.forEachTopLevel("interposing", converter);
break;
}
}

protected emitConvertModuleHelpers(): void {
Expand Down
3 changes: 2 additions & 1 deletion src/quicktype-core/language/TypeScriptFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export abstract class TypeScriptFlowBaseTargetLanguage extends JavaScriptTargetL
tsFlowOptions.nicePropertyNames,
tsFlowOptions.declareUnions,
tsFlowOptions.runtimeTypecheck,
tsFlowOptions.acronymStyle
tsFlowOptions.acronymStyle,
tsFlowOptions.converters
];
}

Expand Down
19 changes: 19 additions & 0 deletions src/quicktype-core/support/Converters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { EnumOption } from "../RendererOptions";

export enum ConvertersOptions {
TopLevel = "top-level",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to TopLevel.

AllObjects = "all-objects"
}

export function convertersOption() {
return new EnumOption(
"converters",
"Which converters to generate (top-level by default)",
[
[ConvertersOptions.TopLevel, ConvertersOptions.TopLevel],
[ConvertersOptions.AllObjects, ConvertersOptions.AllObjects]
],
ConvertersOptions.TopLevel,
"secondary"
);
}
5 changes: 3 additions & 2 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ export const TypeScriptLanguage: Language = {
{ "runtime-typecheck": "false" },
{ "nice-property-names": "true" },
{ "declare-unions": "true" },
{ "acronym-style": "pascal" }
{ "acronym-style": "pascal" },
{ converters: "all-objects" }
],
sourceFiles: ["src/language/TypeScript.ts"]
};
Expand All @@ -619,7 +620,7 @@ export const JavaScriptLanguage: Language = {
skipMiscJSON: false,
skipSchema: ["keyword-unions.schema"], // can't handle "constructor" property
rendererOptions: {},
quickTestRendererOptions: [{ "runtime-typecheck": "false" }],
quickTestRendererOptions: [{ "runtime-typecheck": "false" }, { converters: "top-level" }],
sourceFiles: ["src/language/JavaScript.ts"]
};

Expand Down