Skip to content

Commit 4dfaa25

Browse files
committed
fix(ruby): gate check_type on runtimeTypeChecking
1 parent dbe2c81 commit 4dfaa25

2 files changed

Lines changed: 3012 additions & 387 deletions

File tree

packages/jsii-pacmak/lib/targets/ruby.ts

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ class RubyGenerator extends Generator {
307307
`value = ::${structType}.new(**value) if value.is_a?(Hash)`,
308308
);
309309
}
310-
this.code.line(
311-
`Jsii::Type.check_type(value, Object.const_get('JSON').parse(%q{${prop.type.spec ? JSON.stringify(prop.type.spec) : '{"primitive":"any"}'}}), "${prop.name}")`,
312-
);
310+
if (this.runtimeTypeChecking) {
311+
this.code.line(
312+
`Jsii::Type.check_type(value, Object.const_get('JSON').parse(%q{${prop.type.spec ? JSON.stringify(prop.type.spec) : '{"primitive":"any"}'}}), "${prop.name}")`,
313+
);
314+
}
313315
this.code.line(` jsii_set_property("${prop.name}", value)`);
314316
this.code.close('end');
315317
this.code.line('');
@@ -347,16 +349,20 @@ class RubyGenerator extends Generator {
347349
);
348350
}
349351
}
350-
if (p.variadic) {
351-
this.code.line(`${rubyParam}.each_with_index do |item, index|`);
352-
this.code.line(
353-
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
354-
);
355-
this.code.line(`end`);
356-
} else {
357-
this.code.line(
358-
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
359-
);
352+
if (this.runtimeTypeChecking) {
353+
if (p.variadic) {
354+
this.code.line(
355+
`${rubyParam}.each_with_index do |item, index|`,
356+
);
357+
this.code.line(
358+
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
359+
);
360+
this.code.line(`end`);
361+
} else {
362+
this.code.line(
363+
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
364+
);
365+
}
360366
}
361367
}
362368
this.code.line(
@@ -455,16 +461,18 @@ class RubyGenerator extends Generator {
455461

456462
for (const p of initializer.parameters) {
457463
const rubyParam = this.rubyName(p.name);
458-
if (p.variadic) {
459-
this.code.line(`${rubyParam}.each_with_index do |item, index|`);
460-
this.code.line(
461-
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
462-
);
463-
this.code.line(`end`);
464-
} else {
465-
this.code.line(
466-
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
467-
);
464+
if (this.runtimeTypeChecking) {
465+
if (p.variadic) {
466+
this.code.line(`${rubyParam}.each_with_index do |item, index|`);
467+
this.code.line(
468+
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
469+
);
470+
this.code.line(`end`);
471+
} else {
472+
this.code.line(
473+
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
474+
);
475+
}
468476
}
469477
}
470478

@@ -555,16 +563,18 @@ class RubyGenerator extends Generator {
555563
);
556564
}
557565
}
558-
if (p.variadic) {
559-
this.code.line(`${rubyParam}.each_with_index do |item, index|`);
560-
this.code.line(
561-
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
562-
);
563-
this.code.line(`end`);
564-
} else {
565-
this.code.line(
566-
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
567-
);
566+
if (this.runtimeTypeChecking) {
567+
if (p.variadic) {
568+
this.code.line(`${rubyParam}.each_with_index do |item, index|`);
569+
this.code.line(
570+
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
571+
);
572+
this.code.line(`end`);
573+
} else {
574+
this.code.line(
575+
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
576+
);
577+
}
568578
}
569579
}
570580
this.code.line(
@@ -597,9 +607,11 @@ class RubyGenerator extends Generator {
597607
`value = ::${structType}.new(**value) if value.is_a?(Hash)`,
598608
);
599609
}
600-
this.code.line(
601-
`Jsii::Type.check_type(value, Object.const_get('JSON').parse(%q{${prop.type.spec ? JSON.stringify(prop.type.spec) : '{"primitive":"any"}'}}), "${prop.name}")`,
602-
);
610+
if (this.runtimeTypeChecking) {
611+
this.code.line(
612+
`Jsii::Type.check_type(value, Object.const_get('JSON').parse(%q{${prop.type.spec ? JSON.stringify(prop.type.spec) : '{"primitive":"any"}'}}), "${prop.name}")`,
613+
);
614+
}
603615
this.code.line(
604616
`Jsii::Kernel.instance.set_static("${typeSpec.fqn}", "${prop.name}", value)`,
605617
);
@@ -623,9 +635,11 @@ class RubyGenerator extends Generator {
623635
`value = ::${structType}.new(**value) if value.is_a?(Hash)`,
624636
);
625637
}
626-
this.code.line(
627-
`Jsii::Type.check_type(value, Object.const_get('JSON').parse(%q{${prop.type.spec ? JSON.stringify(prop.type.spec) : '{"primitive":"any"}'}}), "${prop.name}")`,
628-
);
638+
if (this.runtimeTypeChecking) {
639+
this.code.line(
640+
`Jsii::Type.check_type(value, Object.const_get('JSON').parse(%q{${prop.type.spec ? JSON.stringify(prop.type.spec) : '{"primitive":"any"}'}}), "${prop.name}")`,
641+
);
642+
}
629643
this.code.line(`jsii_set_property("${prop.name}", value)`);
630644
this.code.close('end');
631645
this.code.line('');
@@ -669,16 +683,18 @@ class RubyGenerator extends Generator {
669683
);
670684
}
671685
}
672-
if (p.variadic) {
673-
this.code.line(`${rubyParam}.each_with_index do |item, index|`);
674-
this.code.line(
675-
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
676-
);
677-
this.code.line(`end`);
678-
} else {
679-
this.code.line(
680-
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
681-
);
686+
if (this.runtimeTypeChecking) {
687+
if (p.variadic) {
688+
this.code.line(`${rubyParam}.each_with_index do |item, index|`);
689+
this.code.line(
690+
` Jsii::Type.check_type(item, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}[#{index}]")`,
691+
);
692+
this.code.line(`end`);
693+
} else {
694+
this.code.line(
695+
`Jsii::Type.check_type(${rubyParam}, Object.const_get('JSON').parse(%q{${p.type.spec ? JSON.stringify(p.type.spec) : '{"primitive":"any"}'}}), "${p.name}")`,
696+
);
697+
}
682698
}
683699
}
684700
if (method.async) {

0 commit comments

Comments
 (0)