Skip to content

Commit 5e89777

Browse files
committed
Handle allow_nil and one_of
1 parent 50aeb43 commit 5e89777

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

spec/fixtures/foobara-manifest.json

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,17 @@
264264
"type": "integer"
265265
},
266266
"last_name": {
267+
"allow_nil": true,
267268
"type": "string"
269+
},
270+
"preferred_contact_method": {
271+
"allow_nil": true,
272+
"one_of": [
273+
"email",
274+
"phonecall",
275+
"sms"
276+
],
277+
"type": "symbol"
268278
}
269279
},
270280
"required": [
@@ -1074,7 +1084,17 @@
10741084
"type": "integer"
10751085
},
10761086
"last_name": {
1087+
"allow_nil": true,
10771088
"type": "string"
1089+
},
1090+
"preferred_contact_method": {
1091+
"allow_nil": true,
1092+
"one_of": [
1093+
"email",
1094+
"phonecall",
1095+
"sms"
1096+
],
1097+
"type": "symbol"
10781098
}
10791099
},
10801100
"required": [
@@ -1134,7 +1154,17 @@
11341154
"type": "integer"
11351155
},
11361156
"last_name": {
1157+
"allow_nil": true,
11371158
"type": "string"
1159+
},
1160+
"preferred_contact_method": {
1161+
"allow_nil": true,
1162+
"one_of": [
1163+
"email",
1164+
"phonecall",
1165+
"sms"
1166+
],
1167+
"type": "symbol"
11381168
}
11391169
},
11401170
"required": [
@@ -1348,8 +1378,18 @@
13481378
"type": "integer"
13491379
},
13501380
"last_name": {
1381+
"allow_nil": true,
13511382
"type": "string"
13521383
},
1384+
"preferred_contact_method": {
1385+
"allow_nil": true,
1386+
"one_of": [
1387+
"email",
1388+
"phonecall",
1389+
"sms"
1390+
],
1391+
"type": "symbol"
1392+
},
13531393
"referral_channel": {
13541394
"type": "string"
13551395
},
@@ -1400,8 +1440,18 @@
14001440
"type": "integer"
14011441
},
14021442
"last_name": {
1443+
"allow_nil": true,
14031444
"type": "string"
14041445
},
1446+
"preferred_contact_method": {
1447+
"allow_nil": true,
1448+
"one_of": [
1449+
"email",
1450+
"phonecall",
1451+
"sms"
1452+
],
1453+
"type": "symbol"
1454+
},
14051455
"referral_channel": {
14061456
"type": "string"
14071457
},
@@ -1620,6 +1670,7 @@
16201670
"processor_manifest_data": {
16211671
"casting": {
16221672
"cast_to": {
1673+
"allow_nil": true,
16231674
"type": "string"
16241675
}
16251676
}
@@ -1653,6 +1704,44 @@
16531704
},
16541705
"symbol": "missing_required_attribute"
16551706
},
1707+
"data.preferred_contact_method.cannot_cast": {
1708+
"category": "data",
1709+
"error": "Value::Processor::Casting::CannotCastError",
1710+
"key": "data.preferred_contact_method.cannot_cast",
1711+
"path": [
1712+
"preferred_contact_method"
1713+
],
1714+
"processor_manifest_data": {
1715+
"casting": {
1716+
"cast_to": {
1717+
"allow_nil": true,
1718+
"one_of": [
1719+
"email",
1720+
"phonecall",
1721+
"sms"
1722+
],
1723+
"type": "symbol"
1724+
}
1725+
}
1726+
},
1727+
"symbol": "cannot_cast"
1728+
},
1729+
"data.preferred_contact_method.value_not_valid": {
1730+
"category": "data",
1731+
"error": "duck::SupportedValidators::OneOf::ValueNotValidError",
1732+
"key": "data.preferred_contact_method.value_not_valid",
1733+
"path": [
1734+
"preferred_contact_method"
1735+
],
1736+
"processor_manifest_data": {
1737+
"one_of": [
1738+
"email",
1739+
"phonecall",
1740+
"sms"
1741+
]
1742+
},
1743+
"symbol": "value_not_valid"
1744+
},
16561745
"data.referral_channel.cannot_cast": {
16571746
"category": "data",
16581747
"error": "Value::Processor::Casting::CannotCastError",
@@ -1691,7 +1780,17 @@
16911780
"type": "integer"
16921781
},
16931782
"last_name": {
1783+
"allow_nil": true,
16941784
"type": "string"
1785+
},
1786+
"preferred_contact_method": {
1787+
"allow_nil": true,
1788+
"one_of": [
1789+
"email",
1790+
"phonecall",
1791+
"sms"
1792+
],
1793+
"type": "symbol"
16951794
}
16961795
},
16971796
"required": [
@@ -1729,8 +1828,18 @@
17291828
"type": "integer"
17301829
},
17311830
"last_name": {
1831+
"allow_nil": true,
17321832
"type": "string"
17331833
},
1834+
"preferred_contact_method": {
1835+
"allow_nil": true,
1836+
"one_of": [
1837+
"email",
1838+
"phonecall",
1839+
"sms"
1840+
],
1841+
"type": "symbol"
1842+
},
17341843
"referral_channel": {
17351844
"type": "string"
17361845
},
@@ -7775,7 +7884,17 @@
77757884
"type": "integer"
77767885
},
77777886
"last_name": {
7887+
"allow_nil": true,
77787888
"type": "string"
7889+
},
7890+
"preferred_contact_method": {
7891+
"allow_nil": true,
7892+
"one_of": [
7893+
"email",
7894+
"phonecall",
7895+
"sms"
7896+
],
7897+
"type": "symbol"
77797898
}
77807899
},
77817900
"required": [
@@ -7799,7 +7918,17 @@
77997918
"type": "integer"
78007919
},
78017920
"last_name": {
7921+
"allow_nil": true,
78027922
"type": "string"
7923+
},
7924+
"preferred_contact_method": {
7925+
"allow_nil": true,
7926+
"one_of": [
7927+
"email",
7928+
"phonecall",
7929+
"sms"
7930+
],
7931+
"type": "symbol"
78037932
}
78047933
},
78057934
"required": [
@@ -7850,7 +7979,17 @@
78507979
"type": "integer"
78517980
},
78527981
"last_name": {
7982+
"allow_nil": true,
78537983
"type": "string"
7984+
},
7985+
"preferred_contact_method": {
7986+
"allow_nil": true,
7987+
"one_of": [
7988+
"email",
7989+
"phonecall",
7990+
"sms"
7991+
],
7992+
"type": "symbol"
78547993
}
78557994
},
78567995
"required": [

src/remote_generator/services/base_generator.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ def foobara_type_to_ts_type(
315315
end
316316

317317
if type_string
318+
if type_declaration.one_of
319+
type_string = type_declaration.one_of.map(&:inspect).join(" | ")
320+
321+
if type_declaration.allows_nil?
322+
type_string = "#{type_string} | undefined"
323+
end
324+
elsif type_declaration.allows_nil?
325+
type_string = "#{type_string}?"
326+
end
327+
318328
name ? "#{name} = #{type_string}" : type_string
319329
else
320330
# :nocov:

0 commit comments

Comments
 (0)