Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class KotlinJacksonRenderer extends KotlinRenderer {
// and enums in the same union
(_enumType) => "is TextNode",
(_unionType) => mustNotHappen(),
(_transformedStringType) => "is TextNode",
);
}

Expand Down
49 changes: 48 additions & 1 deletion packages/quicktype-core/src/language/Kotlin/KotlinRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { iterableSome } from "collection-utils";

import {
anyTypeIssueAnnotation,
nullTypeIssueAnnotation,
Expand All @@ -19,7 +21,7 @@ import {
type EnumType,
MapType,
type ObjectType,
type PrimitiveType,
PrimitiveType,
type Type,
type UnionType,
} from "../../Type";
Expand Down Expand Up @@ -158,6 +160,15 @@ export class KotlinRenderer extends ConvenienceRenderer {
];
}

protected haveTransformedStringType(
kind: "date-time" | "date" | "time",
): boolean {
return iterableSome(
this.typeGraph.allTypesUnordered(),
(t) => t instanceof PrimitiveType && t.kind === kind,
);
}

protected kotlinType(
t: Type,
withIssues = false,
Expand Down Expand Up @@ -194,6 +205,21 @@ export class KotlinRenderer extends ConvenienceRenderer {
return [this.kotlinType(nullable, withIssues), optional];
return this.nameForNamedType(unionType);
},
(transformedStringType) => {
if (transformedStringType.kind === "date-time") {
return "OffsetDateTime";
}

if (transformedStringType.kind === "date") {
return "LocalDate";
}

if (transformedStringType.kind === "time") {
return "OffsetTime";
}

return "String";
},
);
}

Expand All @@ -211,6 +237,27 @@ export class KotlinRenderer extends ConvenienceRenderer {
this.ensureBlankLine();
this.emitLine("package ", this._kotlinOptions.packageName);
this.ensureBlankLine();

// Check if we need to import java.time classes
const usesDateTime = this.haveTransformedStringType("date-time");
const usesDate = this.haveTransformedStringType("date");
const usesTime = this.haveTransformedStringType("time");

if (usesDateTime) {
this.emitLine("import java.time.OffsetDateTime");
}

if (usesDate) {
this.emitLine("import java.time.LocalDate");
}

if (usesTime) {
this.emitLine("import java.time.OffsetTime");
}

if (usesDateTime || usesDate || usesTime) {
this.ensureBlankLine();
}
}

protected emitTopLevelPrimitive(t: PrimitiveType, name: Name): void {
Expand Down
14 changes: 14 additions & 0 deletions packages/quicktype-core/src/language/Kotlin/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
import { AcronymStyleOptions, acronymOption } from "../../support/Acronyms";
import { assertNever } from "../../support/Support";
import { TargetLanguage } from "../../TargetLanguage";
import type {
PrimitiveStringTypeKind,
TransformedStringTypeKind,
} from "../../Type";
import type { StringTypeMapping } from "../../Type/TypeBuilderUtils";
import type { LanguageName, RendererOptions } from "../../types";

import { KotlinJacksonRenderer } from "./KotlinJacksonRenderer";
Expand Down Expand Up @@ -56,6 +61,15 @@ export class KotlinTargetLanguage extends TargetLanguage<
return true;
}

public get stringTypeMapping(): StringTypeMapping {
const mapping: Map<TransformedStringTypeKind, PrimitiveStringTypeKind> =
new Map();
mapping.set("date", "date");
mapping.set("time", "time");
mapping.set("date-time", "date-time");
return mapping;
}

protected makeRenderer<Lang extends LanguageName = "kotlin">(
renderContext: RenderContext,
untypedOptionValues: RendererOptions<Lang>,
Expand Down
4 changes: 2 additions & 2 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ export const KotlinLanguage: Language = {
"76ae1.json",
],
allowMissingNull: true,
features: ["enum", "union", "no-defaults"],
features: ["enum", "union", "no-defaults", "date-time"],
output: "TopLevel.kt",
topLevel: "TopLevel",
skipJSON: [
Expand Down Expand Up @@ -1178,7 +1178,7 @@ export const KotlinJacksonLanguage: Language = {
"76ae1.json",
],
allowMissingNull: true,
features: ["enum", "union", "no-defaults"],
features: ["enum", "union", "no-defaults", "date-time"],
output: "TopLevel.kt",
topLevel: "TopLevel",
skipJSON: [
Expand Down