Skip to content

Commit

Permalink
Handle allow_nil and one_of
Browse files Browse the repository at this point in the history
  • Loading branch information
azimux committed Feb 9, 2024
1 parent 50aeb43 commit 5e89777
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
139 changes: 139 additions & 0 deletions spec/fixtures/foobara-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,17 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
},
"required": [
Expand Down Expand Up @@ -1074,7 +1084,17 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
},
"required": [
Expand Down Expand Up @@ -1134,7 +1154,17 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
},
"required": [
Expand Down Expand Up @@ -1348,8 +1378,18 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
},
"referral_channel": {
"type": "string"
},
Expand Down Expand Up @@ -1400,8 +1440,18 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
},
"referral_channel": {
"type": "string"
},
Expand Down Expand Up @@ -1620,6 +1670,7 @@
"processor_manifest_data": {
"casting": {
"cast_to": {
"allow_nil": true,
"type": "string"
}
}
Expand Down Expand Up @@ -1653,6 +1704,44 @@
},
"symbol": "missing_required_attribute"
},
"data.preferred_contact_method.cannot_cast": {
"category": "data",
"error": "Value::Processor::Casting::CannotCastError",
"key": "data.preferred_contact_method.cannot_cast",
"path": [
"preferred_contact_method"
],
"processor_manifest_data": {
"casting": {
"cast_to": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
}
},
"symbol": "cannot_cast"
},
"data.preferred_contact_method.value_not_valid": {
"category": "data",
"error": "duck::SupportedValidators::OneOf::ValueNotValidError",
"key": "data.preferred_contact_method.value_not_valid",
"path": [
"preferred_contact_method"
],
"processor_manifest_data": {
"one_of": [
"email",
"phonecall",
"sms"
]
},
"symbol": "value_not_valid"
},
"data.referral_channel.cannot_cast": {
"category": "data",
"error": "Value::Processor::Casting::CannotCastError",
Expand Down Expand Up @@ -1691,7 +1780,17 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
},
"required": [
Expand Down Expand Up @@ -1729,8 +1828,18 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
},
"referral_channel": {
"type": "string"
},
Expand Down Expand Up @@ -7775,7 +7884,17 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
},
"required": [
Expand All @@ -7799,7 +7918,17 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
},
"required": [
Expand Down Expand Up @@ -7850,7 +7979,17 @@
"type": "integer"
},
"last_name": {
"allow_nil": true,
"type": "string"
},
"preferred_contact_method": {
"allow_nil": true,
"one_of": [
"email",
"phonecall",
"sms"
],
"type": "symbol"
}
},
"required": [
Expand Down
10 changes: 10 additions & 0 deletions src/remote_generator/services/base_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ def foobara_type_to_ts_type(
end

if type_string
if type_declaration.one_of
type_string = type_declaration.one_of.map(&:inspect).join(" | ")

if type_declaration.allows_nil?
type_string = "#{type_string} | undefined"
end
elsif type_declaration.allows_nil?
type_string = "#{type_string}?"
end

name ? "#{name} = #{type_string}" : type_string
else
# :nocov:
Expand Down

0 comments on commit 5e89777

Please sign in to comment.