Make date-time inference optional

This commit is contained in:
David Siegel 2018-02-15 10:05:48 -08:00
Родитель 90c17644d3
Коммит f50d25490a
3 изменённых файлов: 19 добавлений и 5 удалений

Просмотреть файл

@ -74,7 +74,11 @@ function canBeEnumCase(s: string): boolean {
}
export class TypeInference {
constructor(private readonly _typeBuilder: TypeBuilder, private readonly _inferEnums: boolean) {}
constructor(
private readonly _typeBuilder: TypeBuilder,
private readonly _inferEnums: boolean,
private readonly _inferDates: boolean
) {}
inferType(
cjson: CompressedJSON,
@ -124,13 +128,13 @@ export class TypeInference {
unionBuilder.addArray(cjson.getArrayForValue(value));
break;
case Tag.Date:
unionBuilder.addStringType("date");
unionBuilder.addStringType(this._inferDates ? "date" : "string");
break;
case Tag.Time:
unionBuilder.addStringType("time");
unionBuilder.addStringType(this._inferDates ? "time" : "string");
break;
case Tag.DateTime:
unionBuilder.addStringType("date-time");
unionBuilder.addStringType(this._inferDates ? "date-time" : "string");
break;
default:
return assertNever(t);

Просмотреть файл

@ -56,6 +56,7 @@ export interface CLIOptions {
noMaps: boolean;
noEnums: boolean;
noDateTimes: boolean;
alphabetizeProperties: boolean;
allPropertiesOptional: boolean;
noCombineClasses: boolean;
@ -235,6 +236,7 @@ function inferOptions(opts: Partial<CLIOptions>): CLIOptions {
topLevel: opts.topLevel || inferTopLevel(opts),
noMaps: !!opts.noMaps,
noEnums: !!opts.noEnums,
noDateTimes: !!opts.noDateTimes,
noCombineClasses: !!opts.noCombineClasses,
noRender: !!opts.noRender,
alphabetizeProperties: !!opts.alphabetizeProperties,
@ -344,6 +346,11 @@ const optionDefinitions: OptionDefinition[] = [
type: Boolean,
description: "Don't infer enums, always use strings."
},
{
name: "no-date-times",
type: Boolean,
description: "Don't infer dates or times."
},
{
name: "no-render",
type: Boolean,
@ -640,6 +647,7 @@ export async function main(args: string[] | Partial<CLIOptions>) {
sources,
inferMaps: !options.noMaps,
inferEnums: !options.noEnums,
inferDates: !options.noDateTimes,
alphabetizeProperties: options.alphabetizeProperties,
allPropertiesOptional: options.allPropertiesOptional,
combineClasses: !options.noCombineClasses,

Просмотреть файл

@ -80,6 +80,7 @@ export interface Options {
findSimilarClassesSchema: string | undefined;
inferMaps: boolean;
inferEnums: boolean;
inferDates: boolean;
alphabetizeProperties: boolean;
allPropertiesOptional: boolean;
combineClasses: boolean;
@ -97,6 +98,7 @@ const defaultOptions: Options = {
findSimilarClassesSchema: undefined,
inferMaps: true,
inferEnums: true,
inferDates: true,
alphabetizeProperties: false,
allPropertiesOptional: false,
combineClasses: true,
@ -178,7 +180,7 @@ export class Run {
// JSON
const doInferEnums = this._options.inferEnums;
if (Object.keys(this._allInputs.samples).length > 0) {
const inference = new TypeInference(typeBuilder, doInferEnums);
const inference = new TypeInference(typeBuilder, doInferEnums, this._options.inferDates);
Map(this._allInputs.samples).forEach((cjson, name) => {
typeBuilder.addTopLevel(