Skip to content
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

Fix default values overriding manually set values #382

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,40 @@ describe ADI::ServiceContainer::ProcessParameters, tags: "compiled" do
end
CR
end

it "does not override value of manually wired up parameters with default value" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper.cr"

class SomeService
def initialize(@id : Int32 = 123); end
end

module MyExtension
macro included
macro finished
{% verbatim do %}
{%
SERVICE_HASH["some_service"] = {
class: SomeService,
parameters: {
id: {value: 999}
}
}
%}
{% end %}
end
end
end

ADI.add_compiler_pass MyExtension, :before_optimization, 1028
ADI::ServiceContainer.new

macro finished
macro finished
it { \\{{ADI::ServiceContainer::SERVICE_HASH["some_service"]["parameters"]["id"]["value"].stringify}}.should eq "999" }
end
end
CR
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module Athena::DependencyInjection::ServiceContainer::ProcessParameters

# Set the default value is there is one.
if !(dv = initializer_arg.default_value).is_a?(Nop)
default_value = value = dv
default_value = dv
end

parameters[initializer_arg.name.id.stringify] = {
Expand All @@ -47,7 +47,7 @@ module Athena::DependencyInjection::ServiceContainer::ProcessParameters
restriction: initializer_arg.restriction,
resolved_restriction: ((r = initializer_arg.restriction).is_a?(Nop) ? nil : r.resolve),
default_value: default_value,
value: value || default_value,
value: value.nil? ? default_value : value,
}
end
end
Expand Down