Skip to content

Commit

Permalink
UUID in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
schani committed Jul 12, 2018
1 parent 22f0f61 commit 7f6a3d3
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/quicktype-core/language/Python.ts
Expand Up @@ -129,6 +129,7 @@ export class PythonTargetLanguage extends TargetLanguage {
mapping.set("date", dateTimeType);
mapping.set("time", dateTimeType);
mapping.set("date-time", dateTimeType);
mapping.set("uuid", "uuid");
mapping.set("integer-string", "integer-string");
mapping.set("bool-string", "bool-string");
return mapping;
Expand Down Expand Up @@ -345,6 +346,9 @@ export class PythonRenderer extends ConvenienceRenderer {
if (transformedStringType.kind === "date-time") {
return this.withImport("datetime", "datetime");
}
if (transformedStringType.kind === "uuid") {
return this.withImport("uuid", "UUID");
}
return panic(`Transformed type ${transformedStringType.kind} not supported`);
}
);
Expand Down Expand Up @@ -868,6 +872,9 @@ export class JSONPythonRenderer extends PythonRenderer {
if (transformedStringType.kind === "date-time") {
return this.withImport("datetime", "datetime");
}
if (transformedStringType.kind === "uuid") {
return this.withImport("uuid", "UUID");
}
return undefined;
}
);
Expand Down Expand Up @@ -930,6 +937,12 @@ export class JSONPythonRenderer extends PythonRenderer {
case "date-time":
vol = this.convFn("from-datetime", inputTransformer);
break;
case "uuid":
vol = compose(
inputTransformer,
v => [this.withImport("uuid", "UUID"), "(", v, ")"]
);
break;
default:
return panic(`Parsing of ${immediateTargetType.kind} in a transformer is not supported`);
}
Expand Down Expand Up @@ -959,6 +972,12 @@ export class JSONPythonRenderer extends PythonRenderer {
v => [v, ".isoformat()"]
);
break;
case "uuid":
vol = compose(
inputTransformer,
v => ["str(", v, ")"]
);
break;
default:
return panic(`Parsing of ${xfer.sourceType.kind} in a transformer is not supported`);
}
Expand Down Expand Up @@ -1030,6 +1049,12 @@ export class JSONPythonRenderer extends PythonRenderer {
if (transformedStringType.kind === "date-time") {
return this.convFn("from-datetime", value);
}
if (transformedStringType.kind === "uuid") {
return compose(
value,
v => [this.withImport("uuid", "UUID"), "(", v, ")"]
);
}
return panic(`Transformed type ${transformedStringType.kind} not supported`);
}
);
Expand Down Expand Up @@ -1099,6 +1124,12 @@ export class JSONPythonRenderer extends PythonRenderer {
v => [v, ".isoformat()"]
);
}
if (transformedStringType.kind === "uuid") {
return compose(
value,
v => ["str(", v, ")"]
);
}
return panic(`Transformed type ${transformedStringType.kind} not supported`);
}
);
Expand Down

0 comments on commit 7f6a3d3

Please sign in to comment.