Merge pull request #723 from quicktype/fix-top-level-enum

Fix top level enum
This commit is contained in:
Mark Probst 2018-03-31 17:48:27 -07:00 коммит произвёл GitHub
Родитель 1853914ec4 059b002358
Коммит dbb5ada000
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 33 добавлений и 9 удалений

Просмотреть файл

@ -514,13 +514,19 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
this.emitLine("//");
this.emitLine("// using ", this.namespaceName, ";");
this.emitLine("//");
this.forEachTopLevel("none", (_, topLevelName) => {
this.forEachTopLevel("none", (t, topLevelName) => {
let rhs: Sourcelike;
if (t instanceof EnumType) {
rhs = ["JsonConvert.DeserializeObject<", topLevelName, ">(jsonString)"]
} else {
rhs = [topLevelName, ".FromJson(jsonString)"];
}
this.emitLine(
"// var ",
modifySource(camelCase, topLevelName),
" = ",
topLevelName,
".FromJson(jsonString);"
rhs,
";"
);
});
}
@ -532,6 +538,8 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
}
private emitFromJsonForTopLevel(t: Type, name: Name): void {
if (t instanceof EnumType) return;
let partial: string;
let typeKind: string;
const definedType = this.namedTypeToNameForTopLevel(t);

Просмотреть файл

@ -354,8 +354,8 @@ export class SwiftRenderer extends ConvenienceRenderer {
} else if (!this._justTypes) {
this.emitLine("// To parse the JSON, add this file to your project and do:");
this.emitLine("//");
this.forEachTopLevel("none", (_, name) => {
if (this._convenienceInitializers) {
this.forEachTopLevel("none", (t, name) => {
if (this._convenienceInitializers && !(t instanceof EnumType)) {
this.emitLine("// let ", modifySource(camelCase, name), " = try ", name, "(json)");
} else {
this.emitLine(
@ -978,6 +978,7 @@ class JSONAny: Codable {
this.emitAlamofireExtension();
}
// FIXME: We emit only the MARK line for top-level-enum.schema
if (this._convenienceInitializers) {
this.ensureBlankLine();
this.emitMark("Convenience initializers");

Просмотреть файл

@ -0,0 +1 @@
"one"

Просмотреть файл

@ -0,0 +1,8 @@
{
"type": "string",
"enum": [
"one",
"three",
"two"
]
}

Просмотреть файл

@ -49,7 +49,9 @@ export const CSharpLanguage: Language = {
"31189.json" // JSON.NET doesn't accept year 0000 as 1BC, though it should
],
skipMiscJSON: false,
skipSchema: [],
skipSchema: [
"top-level-enum.schema" // The code we generate for top-level enums is incompatible with the driver
],
rendererOptions: {},
quickTestRendererOptions: [{ "array-type": "list" }, { "csharp-version": "5" }, { density: "dense" }],
sourceFiles: ["src/Language/CSharp.ts"]
@ -177,7 +179,9 @@ export const RubyLanguage: Language = {
skipJSON: [],
skipSchema: [
// FIXME: I don't know what the issue is here
"implicit-class-array-union.schema"
"implicit-class-array-union.schema",
// We don't generate a convenience method for top-level enums
"top-level-enum.schema"
],
skipMiscJSON: false,
rendererOptions: {},
@ -362,8 +366,10 @@ export const SwiftLanguage: Language = {
],
skipMiscJSON: false,
skipSchema: [
// The top-level is a union
"implicit-class-array-union.schema"
// The top-level is a union, which Swift's JSON types don't support
"implicit-class-array-union.schema",
// The code we generate for top-level enums is incompatible with the driver
"top-level-enum.schema"
],
rendererOptions: {},
quickTestRendererOptions: [{ "struct-or-class": "class" }, { density: "dense" }, { density: "normal" }],