-
Notifications
You must be signed in to change notification settings - Fork 351
Default configuations + Option Normalization #692
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
Default configuations + Option Normalization #692
Conversation
eksperimental
commented
Mar 8, 2017
- Store in mix.exs the defaults for various options
- Add function to convert to nil when an empty string is passes in the command line arguments
- Add function to set default arguments in case they are not set
I am wondering if we should have functions |
@eksperimental this is backwards. :) Your code should not depend on the Mixfile. The Mixfile may not even be loaded when the code is running as a dependency. |
@josevalim I sort of remember that we talked about it in the past, and it was when I implemented |
lib/ex_doc.ex
Outdated
extras: [], | ||
filter_prefix: nil, | ||
formatter: "html", | ||
formatter: Mix.Project.config[:default][:formatter], |
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.
This is the same issue. It is bad practice to invoke anything from Mix in your lib
code because Mix is a build tool and it may not be available after the software is built. Even though you are using it only at compile time, doing this here means it is very easy for someone to accidentally depend on this at runtime.
What is this PR trying to achieve? Are the values being duplicated somewhere?
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.
If there is duplication, we should handle it on this file exclusively.
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.
sorry, I was too tired yesterday and overlooked this.
Please have a look again at the rebased commit
e12109d
to
9a34965
Compare
lib/ex_doc.ex
Outdated
@@ -1,3 +1,20 @@ | |||
defmodule ExDoc.Default do |
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.
Modules should have a @moduledoc tag.
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.
We don't need a new module, it can just be in ExDoc
. Don't complicate the feature. :)
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.
the reason is that ExDoc
module depends on ExDoc.Config
, and ExDoc.Config
depends on the default config function. I tried creating ExDoc.Config.deafult/0-1
functions an undefined function error. that was why,
If you can find a way that this will work without the extra module, please let me know.. cuz I couldn't
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []}, | ||
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, | ||
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}, | ||
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5", "2e73e068cd6393526f9fa6d399353d7c9477d6886ba005f323b592d389fb47be", [:make], []}} |
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.
Don't commit mix.lock unless you are changing deps.
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.
@ericmj may I ask why the lock file change if no dependencies have been updated.
and what's the best way to deal with it, use git stash
?
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.
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.
@eksperimental It's because a newer version of Hex was used to build the mix.lock.
9a34965
to
afb1a8f
Compare
ping |
I don't believe those changes belong here. If a source can give invalid input, such as empty strings, then those need to be removed and validated at the point of entry and not internally in the code. |
end | ||
|
||
defp normalize_options(options) do | ||
pattern = options[:source_url_pattern] || guess_url(options[:source_url], options[:source_ref] || "master") |
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.
This code is doing exactly what the old code does, except the new code is much more verbose. Why change it?
lib/ex_doc.ex
Outdated
end | ||
end | ||
|
||
defp normalize_option(options, field, default) do |
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.
This function can be written as: options[field] || default
. Which is how it was being used before.
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.
Reason was that I was dealing with empty strings as well
afb1a8f
to
ad4f841
Compare
@josevalim Please have a look at the new approach. |
lib/ex_doc.ex
Outdated
|
||
@spec config :: map | ||
def config, | ||
do: @config |
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.
I think we tend to avoid the , do:
style when there's only one function clause and go with do ... end
.
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.
I didn't know about that.
lib/ex_doc.ex
Outdated
options = | ||
options | ||
|> normalize_directory([:assets, :output, :source_root]) | ||
|> normalize_source_url_pattern |
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.
Parens here?
lib/ex_doc.ex
Outdated
pattern = options[:source_url_pattern] || guess_url(options[:source_url], options[:source_ref] || "master") | ||
options = Keyword.put(options, :source_url_pattern, pattern) | ||
def normalize_directory(options, field) when is_atom(field) do | ||
if is_bitstring(options[field]) do |
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.
Was this meant to be is_binary
? 🤔
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.
oh yeah, that's right
lib/ex_doc.ex
Outdated
end | ||
|
||
@doc """ | ||
Updates `field` in `options`, if its value is `nil`. |
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.
note to myself: remove the comma
Store defaults for various options in ExDoc.Default
…elpers. Replace ExDoc.normalize_options/1 with a more general function named ExDoc.normalize_options/3 Introduce: - ExDoc.nilify_options/1 - ExDoc.normalize_source_url_pattern/2 - ExDoc.normalize_directory/2
ad4f841
to
1c099cd
Compare
Suggestions addressed |
Sorry @eksperimental but I am still having a hard time to understand why this pull request is addressing. Can we break it apart into smaller pull requests? I don't understand why we have to nilify options. If someone passes |
@josevalim I'm addressing your suggestions and submitting two new PRs |
Thank you! ❤️ |