Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
HighLiuk authored Jan 11, 2024
2 parents 6114e72 + 838c5e0 commit 7c32154
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 81 deletions.
4 changes: 3 additions & 1 deletion packages/quicktype-core/src/language/Golang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const goOptions = {
justTypesAndPackage: new BooleanOption("just-types-and-package", "Plain types with package only", false),
packageName: new StringOption("package", "Generated package name", "NAME", "main"),
multiFileOutput: new BooleanOption("multi-file-output", "Renders each top-level object in its own Go file", false),
fieldTags: new StringOption("field-tags", "list of tags which should be generated for fields", "TAGS", "json")
fieldTags: new StringOption("field-tags", "list of tags which should be generated for fields", "TAGS", "json"),
omitEmpty: new BooleanOption("omit-empty", "If set, all non-required objects will be tagged with ,omitempty", false)
};

export class GoTargetLanguage extends TargetLanguage {
Expand Down Expand Up @@ -88,6 +89,7 @@ function isValueType(t: Type): boolean {

function canOmitEmpty(cp: ClassProperty): boolean {
if (!cp.isOptional) return false;
if (goOptions.omitEmpty) return true;
const t = cp.type;
return ["union", "null", "any"].indexOf(t.kind) < 0;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/quicktype-core/src/language/Python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ export class PythonRenderer extends ConvenienceRenderer {
this.emitLine("pass");
} else {
this.forEachClassProperty(t, "none", (name, jsonName, cp) => {
this.emitDescription(this.descriptionForClassProperty(t, jsonName));
this.emitLine(name, this.typeHint(": ", this.pythonType(cp.type, true)));
this.emitDescription(this.descriptionForClassProperty(t, jsonName));
});
}
this.ensureBlankLine();
Expand Down
5 changes: 4 additions & 1 deletion packages/quicktype-core/src/language/Rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ const keywords = [
"default",
"dyn",
"'static",
"union"
"union",

// Conflict between `std::Option` and potentially generated Option
"option"
];

const isAsciiLetterOrUnderscoreOrDigit = (codePoint: number): boolean => {
Expand Down
49 changes: 32 additions & 17 deletions packages/quicktype-core/src/language/TypeScriptEffectSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,39 +100,39 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {

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

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

typeMapTypeFor(t: Type, required: boolean = true): Sourcelike {
if (["class", "object", "enum"].indexOf(t.kind) >= 0) {
return ["Schema.lazy(() => ", this.nameForNamedType(t), "Schema)"];
return ["S.lazy(() => ", this.nameForNamedType(t), ")"];
}

const match = matchType<Sourcelike>(
t,
_anyType => "Schema.any",
_nullType => "Schema.null",
_boolType => "Schema.boolean",
_integerType => "Schema.number",
_doubleType => "Schema.number",
_stringType => "Schema.string",
arrayType => ["Schema.array(", this.typeMapTypeFor(arrayType.items, false), ")"],
_anyType => "S.any",
_nullType => "S.null",
_boolType => "S.boolean",
_integerType => "S.number",
_doubleType => "S.number",
_stringType => "S.string",
arrayType => ["S.array(", this.typeMapTypeFor(arrayType.items, false), ")"],
_classType => panic("Should already be handled."),
_mapType => ["Schema.record(Schema.string, ", this.typeMapTypeFor(_mapType.values, false), ")"],
_mapType => ["S.record(S.string, ", 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 ["Schema.union(", ...arrayIntercalate(", ", children), ")"];
return ["S.union(", ...arrayIntercalate(", ", children), ")"];
},
_transformedStringType => {
return "Schema.string";
return "S.string";
}
);

Expand All @@ -145,22 +145,37 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {

private emitObject(name: Name, t: ObjectType) {
this.ensureBlankLine();
this.emitLine("\nexport const ", name, "Schema = ", "Schema.struct({");
if (this._options.justSchema) {
this.emitLine("\nexport const ", name, " = S.struct({");
} else {
this.emitLine("\nconst ", name, "_ = S.struct({");
}
this.indent(() => {
this.forEachClassProperty(t, "none", (_, jsonName, property) => {
this.emitLine(`"${utf16StringEscape(jsonName)}"`, ": ", this.typeMapTypeForProperty(property), ",");
});
});
this.emitLine("});");
if (!this._options.justSchema) {
this.emitLine("export type ", name, " = Schema.From<typeof ", name, "Schema>;");
this.emitLine("export interface ", name, " extends S.Schema.To<typeof ", name, "_> {}");
this.emitLine(
"export const ",
name,
": S.Schema<S.Schema.From<typeof ",
name,
"_>, ",
name,
"> = ",
name,
"_;"
);
}
}

private emitEnum(e: EnumType, enumName: Name): void {
this.ensureBlankLine();
this.emitDescription(this.descriptionForType(e));
this.emitLine("\nexport const ", enumName, "Schema = ", "Schema.enums({");
this.emitLine("\nexport const ", enumName, " = ", "S.enums({");
this.indent(() =>
this.forEachEnumCase(e, "none", (_, jsonName) => {
const name = stringEscape(jsonName);
Expand All @@ -169,7 +184,7 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {
);
this.emitLine("});");
if (!this._options.justSchema) {
this.emitLine("export type ", enumName, " = Schema.From<typeof ", enumName, "Schema>;");
this.emitLine("export type ", enumName, " = S.Schema.To<typeof ", enumName, ">;");
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/quicktype-core/src/language/TypeScriptFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export abstract class TypeScriptFlowBaseTargetLanguage extends JavaScriptTargetL
tsFlowOptions.converters,
tsFlowOptions.rawType,
tsFlowOptions.preferUnions,
tsFlowOptions.preferTypes
tsFlowOptions.preferTypes,
tsFlowOptions.preferConstValues
];
}

Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/typescript-effect-schema/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const sample = process.argv[2];
const json = fs.readFileSync(sample);

const value = JSON.parse(json.toString());
let schema = TopLevel.TopLevelSchema ?? TopLevel.TopLevelElementSchema;
let schema = TopLevel.TopLevel ?? TopLevel.TopLevelElement;
if (!schema) {
// Sometimes key is prefixed with funPrefixes (e.g. 2df80.json)
Object.keys(TopLevel).some(key => {
if (key.endsWith("TopLevelSchema") || key.endsWith("TopLevelElementSchema")) {
if (key.endsWith("TopLevel") || key.endsWith("TopLevelElement")) {
schema = TopLevel[key];
return true;
}
Expand All @@ -25,11 +25,11 @@ if (!schema) {
let backToJson: string;
if (Array.isArray(value)) {
const parsedValue = value.map(v => {
return Schema.parse(schema)(v);
return Schema.parseSync(schema)(v);
});
backToJson = JSON.stringify(parsedValue, null, 2);
} else {
const parsedValue = Schema.parse(schema)(value);
const parsedValue = Schema.parseSync(schema)(value);
backToJson = JSON.stringify(parsedValue, null, 2);
}

Expand Down
95 changes: 40 additions & 55 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": "^4.3.5"
},
"dependencies": {
"@effect/schema": "^0.21.1"
"@effect/schema": "^0.47.1"
}
}

0 comments on commit 7c32154

Please sign in to comment.