Repository: comonicon/Comonicon.jl
Pull request #229 (2022-12-01 20:58:58)

Fixed a bug with optional Config parameters
PR fixes https://github.com/comonicon/Comonicon.jl/issues/228 and adds tests that fail on the current version, but work fine with the fix.

codecov[bot]: # [Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
Base: **83.71**% // Head: **1.03**% // Decreases project coverage by **`-82.67%`** :warning:
> Coverage data is based on head [(`fac6f1f`)](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) compared to base [(`67c7e60`)](https://codecov.io/gh/comonicon/Comonicon.jl/commit/67c7e6068b233ed4f05dfe7089ee776b9a08b5eb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> Patch coverage: 0.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #229       +/-   ##
==========================================
- Coverage   83.71%   1.03%   -82.68%     
==========================================
  Files          21      20        -1     
  Lines        1584    1547       -37     
==========================================
- Hits         1326      16     -1310     
- Misses        258    1531     +1273     
```


| [Impacted Files](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [src/codegen/julia.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvZGVnZW4vanVsaWEuamw=) | `0.00% <0.00%> (-89.14%)` | :arrow_down: |
| [src/compat.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvbXBhdC5qbA==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [src/exceptions.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2V4Y2VwdGlvbnMuamw=) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [src/builder/app.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvYXBwLmps) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [src/frontend/markdown.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2Zyb250ZW5kL21hcmtkb3duLmps) | `0.00% <0.00%> (-94.49%)` | :arrow_down: |
| [src/builder/install.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvaW5zdGFsbC5qbA==) | `0.00% <0.00%> (-93.25%)` | :arrow_down: |
| [src/codegen/zsh.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvZGVnZW4venNoLmps) | `0.00% <0.00%> (-92.60%)` | :arrow_down: |
| [src/codegen/bash.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvZGVnZW4vYmFzaC5qbA==) | `0.00% <0.00%> (-91.02%)` | :arrow_down: |
| [src/builder/cli.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvY2xpLmps) | `0.00% <0.00%> (-89.71%)` | :arrow_down: |
| [src/ast/utils.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2FzdC91dGlscy5qbA==) | `0.00% <0.00%> (-88.89%)` | :arrow_down: |
| ... and [11 more](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/229?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).


Patch:
diff --git a/src/codegen/julia.jl b/src/codegen/julia.jl
index 758a266..ce0cdc9 100644
--- a/src/codegen/julia.jl
+++ b/src/codegen/julia.jl
@@ -274,8 +274,10 @@ function emit_leaf_call(cmd::LeafCommand, ctx::EmitContext, args::Symbol, kwargs
         for option in values(cmd.options)
             Configurations.is_option(option.type) || continue
             push!(ret.args, quote
-                $kwargs[$(QuoteNode(option.sym))] =
-                    $Configurations.from_dict($(option.type), $kwargs[$(QuoteNode(option.sym))])
+                if $(QuoteNode(option.sym)) in Base.keys($kwargs)
+                    $kwargs[$(QuoteNode(option.sym))] =
+                        $Configurations.from_dict($(option.type), $kwargs[$(QuoteNode(option.sym))])
+                end
             end)
         end
     end
@@ -533,7 +535,7 @@ function emit_option_with_option_type(
     assign_field_to_dict = foreach_leaf_field(option.type, value, fields) do dict_ex, type
         quote
             $field_value = $(emit_parse_value(option, ctx, type, field_value))
-            $dict_ex = $field_value 
+            $dict_ex = $field_value
         end
     end
     assign_field_to_dict.otherwise = emit_error(option, ctx, :("cannot find field $($fields)"))


Test patch:
diff --git a/test/codegen/julia/config.jl b/test/codegen/julia/config.jl
index 2421413..bd4161f 100644
--- a/test/codegen/julia/config.jl
+++ b/test/codegen/julia/config.jl
@@ -5,8 +5,8 @@ using Comonicon
 using Configurations
 
 @option struct OptionA
-    a::Int
-    b::Int
+    a::Int = 2
+    b::Int = 2
 end
 
 @option struct OptionB
@@ -19,21 +19,31 @@ end
 
 - `-c, --config <path/to/option/or/specific field>`: config.
 """
-@main function run(;config::OptionB)
+@cast function run(;config::OptionB)
     @test config == OptionB(OptionA(1, 1), 1)
 end
 
+@cast function rundef(;config::OptionA=OptionA())
+    @test config == OptionA(2, 2)
+end
+
+@main
+
 @testset "config options" begin
-    TestConfigOption.command_main(["--config.c=1", "--config.option.a=1", "--config.option.b=1"])
+    TestConfigOption.command_main(["run", "--config.c=1", "--config.option.a=1", "--config.option.b=1"])
 
     opt = TestConfigOption.OptionB(TestConfigOption.OptionA(1, 1), 1)
     to_toml("config.toml", opt)
-    TestConfigOption.command_main(["--config", "config.toml"])
-    TestConfigOption.command_main(["-c", "config.toml"])
+    TestConfigOption.command_main(["run", "--config", "config.toml"])
+    TestConfigOption.command_main(["run", "-c", "config.toml"])
 
     opt = TestConfigOption.OptionB(TestConfigOption.OptionA(1, 1), 2)
     to_toml("config.toml", opt)
-    TestConfigOption.command_main(["--config", "config.toml", "--config.c=1"])
+    TestConfigOption.command_main(["run", "--config", "config.toml", "--config.c=1"])
+
+    TestConfigOption.command_main(["rundef"])
+    TestConfigOption.command_main(["rundef", "--config.a=2"])
+    TestConfigOption.command_main(["rundef", "--config.a=2", "--config.b=2"])
 end
 
 end