Skip to content

Commit 472075a

Browse files
authored
update to latest effect/Schema (#2646)
1 parent da1238c commit 472075a

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

packages/quicktype-core/src/language/TypeScriptEffectSchema/TypeScriptEffectSchemaRenderer.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,15 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {
7676

7777
protected emitImports(): void {
7878
this.ensureBlankLine();
79-
this.emitLine(this.importStatement("* as S", '"@effect/schema/Schema"'));
79+
this.emitLine(this.importStatement("* as S", '"effect/Schema"'));
8080
}
8181

8282
private typeMapTypeForProperty(p: ClassProperty): Sourcelike {
83-
const typeMap = this.typeMapTypeFor(p.type);
84-
return p.isOptional ? ["S.optional(", typeMap, ")"] : typeMap;
83+
if (!p.isOptional) {
84+
return this.typeMapTypeFor(p.type);
85+
}
86+
87+
return ["S.optional(", this.typeMapTypeFor(p.type), ")"];
8588
}
8689

8790
private typeMapTypeFor(t: Type, required: boolean = true): Sourcelike {
@@ -104,13 +107,25 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {
104107
_stringType => "S.String",
105108
arrayType => ["S.Array(", this.typeMapTypeFor(arrayType.items, false), ")"],
106109
_classType => panic("Should already be handled."),
107-
_mapType => ["S.Record(S.String, ", this.typeMapTypeFor(_mapType.values, false), ")"],
110+
_mapType => ["S.Record({ key: S.String, value: ", this.typeMapTypeFor(_mapType.values, false), "})"],
108111
_enumType => panic("Should already be handled."),
109112
unionType => {
110-
const children = Array.from(unionType.getChildren()).map((type: Type) =>
111-
this.typeMapTypeFor(type, false)
112-
);
113-
return ["S.Union(", ...arrayIntercalate(", ", children), ")"];
113+
const types = Array.from(unionType.getChildren());
114+
let children: Sourcelike[] = [];
115+
let nullable = false;
116+
for (const type of types) {
117+
if (type.kind === "null") {
118+
nullable = true;
119+
} else {
120+
children.push(this.typeMapTypeFor(type, false));
121+
}
122+
}
123+
124+
if (nullable && children.length === 1) {
125+
return ["S.NullOr(", children[0], ")"];
126+
}
127+
128+
return ["S.Union(", ...arrayIntercalate(", ", children), nullable ? ", S.Null)" : ")"];
114129
},
115130
_transformedStringType => {
116131
return "S.String";

test/fixtures/typescript-effect-schema/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as TopLevel from "./TopLevel";
22
import fs from "fs";
33
import process from "process";
4-
import * as Schema from "@effect/schema/Schema";
4+
import * as Schema from "effect/Schema";
55

66
const sample = process.argv[2];
77
const json = fs.readFileSync(sample);

test/fixtures/typescript-effect-schema/package-lock.json

Lines changed: 13 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/typescript-effect-schema/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"typescript": "^5.4.0"
1414
},
1515
"dependencies": {
16-
"@effect/schema": "^0.66.5"
16+
"effect": "^3.10.0"
1717
}
1818
}

0 commit comments

Comments
 (0)