-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When generating types, distinguish missing and nothing #166
base: main
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## main #166 +/- ##
==========================================
+ Coverage 88.76% 88.81% +0.05%
==========================================
Files 9 9
Lines 1531 1538 +7
==========================================
+ Hits 1359 1366 +7
Misses 172 172
Continue to review full report at Codecov.
|
Hmmmm, I don't know that I love this use of |
So would the types of those fields be A toy example: [{"a": null, "b": 1},
{"b": null},
{"a": 1, "b": 3}] would turn into: mutable struct Root
a::Union{Some{Union{Int, Nothing}}, Nothing} # both might be missing and have null data
b::Union{Int,Nothing} # only null data
function Root()
x = new()
x.a = nothing
end
end This feels a little clunky to me as the How widely used is |
|
||
@test res[3].d == 10 | ||
@test ismissing(res[2].d) | ||
@test res[1].d === nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we reorder the asserts to be the same order as the input data, and also check for missing and nothing in the same way? Eg,
@test res[1].d === nothing
@test res[2].d === missing
@test res[3].d == 10
Regarding the approach, I think one problem is there is no general consensus in the Julia community on how to handle missing values. Different projects do it different. Doubly so for projects which get their json data from other projects which may have different opinions, which I suspect is quite common. At any rate, I think the user should have a way to configure the behavior for their structs, probably via |
Instead of treating a missing field as a
Nothing
type when generating types, instead useMissing
. If the struct is mutable, default the fields withMissing
types tomissing
on initialization. (Is there a better way to build that constructor?)This can lead to a case where there's a
Union{Missing, Nothing...}
type in the generated structs, so I added a check in theread
function for this case so that it prefers readingnull
intonothing
overmissing
when both types are present.I'm not sure this is the right approach, but wanted to start the PR to discuss.
closes #143