Skip to content

Commit

Permalink
update to latest effect/Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Oct 24, 2024
1 parent 29bb816 commit 6470fa3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {

protected emitImports(): void {
this.ensureBlankLine();
this.emitLine(this.importStatement("* as S", '"@effect/schema/Schema"'));
this.emitLine(this.importStatement("* as S", '"effect/Schema"'));
}

private typeMapTypeForProperty(p: ClassProperty): Sourcelike {
const typeMap = this.typeMapTypeFor(p.type);
return p.isOptional ? ["S.optional(", typeMap, ")"] : typeMap;
if (!p.isOptional) {
return this.typeMapTypeFor(p.type);
}

return ["S.optional(", this.typeMapTypeFor(p.type), ")"];
}

private typeMapTypeFor(t: Type, required: boolean = true): Sourcelike {
Expand All @@ -104,13 +107,25 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {
_stringType => "S.String",
arrayType => ["S.Array(", this.typeMapTypeFor(arrayType.items, false), ")"],
_classType => panic("Should already be handled."),
_mapType => ["S.Record(S.String, ", this.typeMapTypeFor(_mapType.values, false), ")"],
_mapType => ["S.Record({ key: S.String, value: ", this.typeMapTypeFor(_mapType.values, false), "})"],
_enumType => panic("Should already be handled."),
unionType => {
const children = Array.from(unionType.getChildren()).map((type: Type) =>
this.typeMapTypeFor(type, false)
);
return ["S.Union(", ...arrayIntercalate(", ", children), ")"];
const types = Array.from(unionType.getChildren());
let children: Sourcelike[] = [];
let nullable = false;
for (const type of types) {
if (type.kind === "null") {
nullable = true;
} else {
children.push(this.typeMapTypeFor(type, false));
}
}

if (nullable && children.length === 1) {
return ["S.NullOr(", children[0], ")"];
}

return ["S.Union(", ...arrayIntercalate(", ", children), nullable ? ", S.Null)" : ")"];
},
_transformedStringType => {
return "S.String";
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/typescript-effect-schema/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as TopLevel from "./TopLevel";
import fs from "fs";
import process from "process";
import * as Schema from "@effect/schema/Schema";
import * as Schema from "effect/Schema";

const sample = process.argv[2];
const json = fs.readFileSync(sample);
Expand Down
32 changes: 13 additions & 19 deletions test/fixtures/typescript-effect-schema/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/typescript-effect-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"typescript": "^5.4.0"
},
"dependencies": {
"@effect/schema": "^0.66.5"
"effect": "^3.10.0"
}
}

0 comments on commit 6470fa3

Please sign in to comment.