Skip to content

Commit

Permalink
Due to a change in the upcoming Crystal version, Nil has to be checke…
Browse files Browse the repository at this point in the history
…d differently in macros. Fixes #88
  • Loading branch information
jwoertink committed Jun 2, 2024
1 parent 5fd8d7b commit e7e8dd1
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/habitat.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ class Habitat
# Habitat.raise_if_missing_settings!
# ```
def self.raise_if_missing_settings!
{% # https://github.com/crystal-lang/crystal/pull/14490

if compare_versions(Crystal::VERSION, "1.13.0-dev") >= 0
nil_type_id = "::Nil".id
else
nil_type_id = "Nil".id
end %}
{% for type in TYPES_WITH_HABITAT %}
{% for setting in type.constant(:HABITAT_SETTINGS) %}
{% if !setting[:decl].type.is_a?(Union) ||
(setting[:decl].type.is_a?(Union) && !setting[:decl].type.types.map(&.id).includes?(Nil.id)) %}
(setting[:decl].type.is_a?(Union) && !setting[:decl].type.types.map(&.id).includes?(nil_type_id)) %}
if {{ type }}.settings.{{ setting[:decl].var }}?.nil?
raise MissingSettingError.new {{ type }}, setting_name: {{ setting[:decl].var.stringify }}, example: {{ setting[:example] }}
end
Expand Down Expand Up @@ -226,12 +233,19 @@ class Habitat
{% end %}
{% end %}

{% # https://github.com/crystal-lang/crystal/pull/14490
if compare_versions(Crystal::VERSION, "1.13.0-dev") >= 0
nil_type_id = "::Nil".id
else
nil_type_id = "Nil".id
end %}

{% for opt in type_with_habitat.constant(:HABITAT_SETTINGS) %}
{% decl = opt[:decl] %}
# NOTE: We can't use the macro level `type.resolve.nilable?` here because
# there's a few declaration types that don't respond to it which would make the logic
# more complex. Metaclass, and Proc types are the main, but there may be more.
{% if decl.type.is_a?(Union) && decl.type.types.map(&.id).includes?(Nil.id) %}
{% if decl.type.is_a?(Union) && decl.type.types.map(&.id).includes?(nil_type_id) %}
{% nilable = true %}
{% else %}
{% nilable = false %}
Expand Down

0 comments on commit e7e8dd1

Please sign in to comment.