When I defined an alias for a NamedTuple, is it possible to make some of the fields as optional? Does that even make sense?
require "json"
alias Person = NamedTuple(
name: String,
email: String?,
)
p1_str = %[{"name": "Foo", "email": "foo@bar.com"}]
p1 = Person.from_json(p1_str)
p p1
p2_str = %[{"name": "Bar"}]
p2 = Person.from_json(p2_str)
p p2
p3_str = %[{"name": "Bar", "email": null}]
p3 = Person.from_json(p3_str)
p p3
The first json string has both “name” and “email” fields, the second json only has a “name” field. (This is a simplified version of responses from an API.) The 3rd one has an email field with null in it.
If I try to run this code without the ?
after the String
then I get a run-time exception:
# Unhandled exception: Missing json attribute: email
If I include the ?
as it is in the example above then I make the field Nil-able and the result will be:
{name: "Foo", email: "foo@bar.com"}
{name: "Bar", email: nil}
{name: "Bar", email: nil}
This means I cant tell apart the 2nd and 3rd cases.
I wonder if it would be possible to make the whole field optional so the result of the code would be:
{name: "Foo", email: "foo@bar.com"}
{name: "Bar"}
{name: "Bar", email: nil}