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

Handle resolving enum property symbol default value to proper enum member #372

Merged
merged 1 commit into from
Mar 27, 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 @@ -280,7 +280,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
CR
end

it "errors if nothing is configured, but a property is required", tags: "compiled" do
it "errors if nothing is configured, but a property is required" do
assert_error "Required configuration property 'test.id : Int32' must be provided.", <<-CR
require "../spec_helper"

Expand All @@ -295,7 +295,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
end
end

it "extension configuration value resolution", tags: "compiled" do
it "extension configuration value resolution" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper"

Expand All @@ -316,6 +316,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
property nilable : String?
property color_type : Color
property color_sym : Color
property color_default : Color = :green
property value : Hash(String, String)
property regex : Regex
end
Expand All @@ -340,14 +341,15 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
it { \\{{ADI::CONFIG["blah"]["nilable"]}}.should be_nil }
it { \\{{ADI::CONFIG["blah"]["color_type"]}}.should eq Color::Red }
it { \\{{ADI::CONFIG["blah"]["color_sym"]}}.should eq Color::Blue }
it { \\{{ADI::CONFIG["blah"]["color_default"]}}.should eq Color::Green }
it { \\{{ADI::CONFIG["blah"]["value"]}}.should eq({"id" => "10", "name" => "fred"}) }
it { \\{{ADI::CONFIG["blah"]["regex"]}}.should eq /foo/ }
end
end
CR
end

it "does not error if nothing is configured, but all properties have defaults or are nilable", tags: "compiled" do
it "does not error if nothing is configured, but all properties have defaults or are nilable" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper"

Expand All @@ -367,7 +369,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
CR
end

it "inherits type of arrays from property if not explicitly set", tags: "compiled" do
it "inherits type of arrays from property if not explicitly set" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper"

Expand All @@ -393,7 +395,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
CR
end

it "allows using NoReturn to type empty arrays in schema", tags: "compiled" do
it "allows using NoReturn to type empty arrays in schema" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper"

Expand All @@ -414,7 +416,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
CR
end

it "allows customizing values when using NoReturn to type empty arrays defaults in schema", tags: "compiled" do
it "allows customizing values when using NoReturn to type empty arrays defaults in schema" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper"

Expand All @@ -440,7 +442,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
CR
end

it "expands schema to include expected structure/defaults if not configuration is provided", tags: "compiled" do
it "expands schema to include expected structure/defaults if not configuration is provided" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper"

Expand Down Expand Up @@ -471,7 +473,7 @@ describe ADI::ServiceContainer::MergeExtensionConfig, tags: "compiled" do
CR
end

it "expands schema to include expected structure/defaults if not explicitly provided", tags: "compiled" do
it "expands schema to include expected structure/defaults if not explicitly provided" do
ASPEC::Methods.assert_success <<-CR, codegen: true
require "../spec_helper"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,17 @@ module Athena::DependencyInjection::ServiceContainer::MergeExtensionConfig
elsif prop["default"].is_a?(Path)
prop["default"].resolve
else
prop["default"]
default_value = prop["default"]

# Resolve symbol literals to enum members
if default_value.is_a?(SymbolLiteral) && (type = prop["type"]) <= ::Enum
config_value.raise "Unknown '#{type}' enum member '#{default_value}' for default value of property '#{([ext_name] + ext_path).join('.').id}.#{prop["name"]}'." unless type.constants.any?(&.downcase.id.==(default_value.id))

# Resolve symbol literals to enum members
default_value = "#{type}.new(#{default_value})".id
end

default_value
end
end

Expand Down