Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: glideapps/quicktype
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 740de7cea038033d9d2b00188c685ca3d4802773
Choose a base ref
...
head repository: glideapps/quicktype
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2dc22cb8dee03e613da69ca99ab2b4057f5bf68a
Choose a head ref
  • 1 commit
  • 3 files changed
  • 3 contributors

Commits on Jan 7, 2023

  1. Allow namespaces to be specified by namespace flag. (#1853)

    * Add namespace support
    
    * Fix else condition
    
    * Add support for multiples submodules
    
    * Fix indent
    
    * Test namespace option
    
    * Format
    
    * lint
    
    * Much better scoping
    
    Co-authored-by: David Siegel <dvdsgl@users.noreply.github.com>
    Co-authored-by: David Siegel <djsiegel@gmail.com>
    3 people authored Jan 7, 2023

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    marsam Mario Rodas
    Copy the full SHA
    2dc22cb View commit details
Showing with 84 additions and 50 deletions.
  1. +76 −44 src/quicktype-core/language/ruby/index.ts
  2. +7 −5 test/fixtures/ruby/main.rb
  3. +1 −1 test/languages.ts
120 changes: 76 additions & 44 deletions src/quicktype-core/language/ruby/index.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { Sourcelike, modifySource } from "../../Source";
import { Namer, Name } from "../../Naming";
import { ConvenienceRenderer, ForbiddenWordsInfo } from "../../ConvenienceRenderer";
import { TargetLanguage } from "../../TargetLanguage";
import { Option, BooleanOption, EnumOption, OptionValues, getOptionValues } from "../../RendererOptions";
import { Option, BooleanOption, EnumOption, OptionValues, getOptionValues, StringOption } from "../../RendererOptions";

import * as keywords from "./keywords";

@@ -47,7 +47,8 @@ export const rubyOptions = {
["strict", Strictness.Strict],
["coercible", Strictness.Coercible],
["none", Strictness.None]
])
]),
namespace: new StringOption("namespace", "Specify a wrapping Namespace", "NAME", "", "secondary")
};

export class RubyTargetLanguage extends TargetLanguage {
@@ -56,7 +57,7 @@ export class RubyTargetLanguage extends TargetLanguage {
}

protected getOptions(): Option<any>[] {
return [rubyOptions.justTypes, rubyOptions.strictness];
return [rubyOptions.justTypes, rubyOptions.strictness, rubyOptions.namespace];
}

get supportsOptionalClassProperties(): boolean {
@@ -384,6 +385,24 @@ export class RubyRenderer extends ConvenienceRenderer {
this.emitLine("end");
}

private emitModule(emit: () => void) {
const emitModuleInner = (moduleName: string) => {
const [firstModule, ...subModules] = moduleName.split("::");
if (subModules.length > 0) {
this.emitBlock(["module ", firstModule], () => {
emitModuleInner(subModules.join("::"));
});
} else {
this.emitBlock(["module ", moduleName], emit);
}
};
if (this._options.namespace !== undefined && this._options.namespace !== "") {
emitModuleInner(this._options.namespace);
} else {
emit();
}
}

private emitClass(c: ClassType, className: Name) {
this.emitDescription(this.descriptionForType(c));
this.emitBlock(["class ", className, " < Dry::Struct"], () => {
@@ -628,52 +647,65 @@ export class RubyRenderer extends ConvenienceRenderer {
this.emitLine("require 'dry-struct'");

this.ensureBlankLine();
this.emitTypesModule();

this.forEachDeclaration("leading-and-interposing", decl => {
if (decl.kind === "forward") {
this.emitCommentLines(["(forward declaration)"]);
this.emitLine("class ", this.nameForNamedType(decl.type), " < Dry::Struct; end");
}
});
this.emitModule(() => {
this.emitTypesModule();

this.forEachNamedType(
"leading-and-interposing",
(c: ClassType, n: Name) => this.emitClass(c, n),
(e, n) => this.emitEnum(e, n),
(u, n) => this.emitUnion(u, n)
);
this.forEachDeclaration("leading-and-interposing", decl => {
if (decl.kind === "forward") {
this.emitCommentLines(["(forward declaration)"]);
this.emitModule(() => {
this.emitLine("class ", this.nameForNamedType(decl.type), " < Dry::Struct; end");
});
}
});

if (!this._options.justTypes) {
this.forEachTopLevel(
this.forEachNamedType(
"leading-and-interposing",
(topLevel, name) => {
const self = modifySource(snakeCase, name);

// The json gem defines to_json on maps and primitives, so we only need to supply
// it for arrays.
const needsToJsonDefined = "array" === topLevel.kind;

this.emitBlock(["class ", name], () => {
this.emitBlock(["def self.from_json!(json)"], () => {
if (needsToJsonDefined) {
this.emitLine(
self,
" = ",
this.fromDynamic(topLevel, "JSON.parse(json, quirks_mode: true)")
);
this.emitBlock([self, ".define_singleton_method(:to_json) do"], () => {
this.emitLine("JSON.generate(", this.toDynamic(topLevel, "self"), ")");
(c: ClassType, n: Name) => this.emitClass(c, n),
(e, n) => this.emitEnum(e, n),
(u, n) => this.emitUnion(u, n)
);

if (!this._options.justTypes) {
this.forEachTopLevel(
"leading-and-interposing",
(topLevel, name) => {
const self = modifySource(snakeCase, name);

// The json gem defines to_json on maps and primitives, so we only need to supply
// it for arrays.
const needsToJsonDefined = "array" === topLevel.kind;

const classDeclaration = () => {
this.emitBlock(["class ", name], () => {
this.emitBlock(["def self.from_json!(json)"], () => {
if (needsToJsonDefined) {
this.emitLine(
self,
" = ",
this.fromDynamic(topLevel, "JSON.parse(json, quirks_mode: true)")
);
this.emitBlock([self, ".define_singleton_method(:to_json) do"], () => {
this.emitLine("JSON.generate(", this.toDynamic(topLevel, "self"), ")");
});
this.emitLine(self);
} else {
this.emitLine(
this.fromDynamic(topLevel, "JSON.parse(json, quirks_mode: true)")
);
}
});
this.emitLine(self);
} else {
this.emitLine(this.fromDynamic(topLevel, "JSON.parse(json, quirks_mode: true)"));
}
});
};

this.emitModule(() => {
classDeclaration();
});
});
},
t => this.namedTypeToNameForTopLevel(t) === undefined
);
}
},
t => this.namedTypeToNameForTopLevel(t) === undefined
);
}
});
}
}
12 changes: 7 additions & 5 deletions test/fixtures/ruby/main.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env ruby

require 'json'
require './TopLevel.rb'
module QuickType
require 'json'
require './TopLevel.rb'

json = File.read(ARGV[0])
top = TopLevel.from_json! json
json = File.read(ARGV[0])
top = TopLevel.from_json! json

puts top.to_json
puts top.to_json
end
2 changes: 1 addition & 1 deletion test/languages.ts
Original file line number Diff line number Diff line change
@@ -351,7 +351,7 @@ export const RubyLanguage: Language = {
],
skipMiscJSON: false,
rendererOptions: {},
quickTestRendererOptions: [],
quickTestRendererOptions: [["pokedex.json", { namespace: "QuickType" }]],
sourceFiles: ["src/language/ruby/index.ts"]
};