-
-
Notifications
You must be signed in to change notification settings - Fork 234
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
Allow symbol value via ENV #309
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,15 +59,25 @@ def load | |
|
||
# Try to convert string to a correct type | ||
def __value(v) | ||
case v | ||
when 'false' | ||
false | ||
when 'true' | ||
true | ||
if %w(true false).include? v | ||
eval(v) | ||
elsif v.strip =~ /^:[^:]/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new behavior is backwards-incompatible because it's possible that someone has an existing configuration where a value begins with I think we have to let users opt into the new behavior explicitly. Let's add a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed! I will add a new configuration |
||
convert_str_to_symbol(v) | ||
else | ||
Integer(v) rescue Float(v) rescue v | ||
end | ||
end | ||
|
||
# Remove all special characters from a string before converting into a symbol | ||
def convert_str_to_symbol(str) | ||
str. | ||
gsub(/[^a-z0-9\-_]+/i, "-"). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the transformations are expected. The least surprising behavior from a user's perspective is that their input is interpreted exactly as they specified it. What's the rationale for modifying the input? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Transformations are needed for Symbol representation because a String may contain whitespaces, may start with or contain invalid characters, etc.
Invalid Symbol representation for above stings are:
Whereas,
After transformation Symbol representations would look like:
Can you please suggest the preferred Symbol representation for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see why these transformation were added then. The behavior, I think, is just unexpected from a user's point-of-view. Right now the behavior is roughly,
This is unexpected for users because they likely have code that depends on the precise characters in the Symbol they specified in configuration. In other words, if they have code the depends on it 'should recognize quoted symbols as symbols' do
ENV['Settings.new_var'] = ':"hi, hello!"'
expect(config.new_var).to eq(:"hi, hello!")
end Instead, it fails because the returned Symbol is Instead of the current behavior, I'd expect the parsing logic to be
In code, I'd expect this spec to pass: it 'should return invalid symbols as strings' do
ENV['Settings.new_var'] = ':hello. this is a string'
expect(config.new_var).to eq(':hello. this is a string')
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If
A simple logic that doesn't have to invalidate string based on its format. Example:
From the above examples, we can notice that the string w/o Please correct me if I am wrong with the above conclusion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok yes I understand the confusion now after re-reading the comments in #286 I'll re-iterate that I don't find any value in supporting the conversion of strings like For now, |
||
gsub(/-{2,}/, "-"). | ||
gsub(/^-|-$/i, ""). | ||
tr("-", "_"). | ||
downcase. | ||
to_sym | ||
end | ||
end | ||
end | ||
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 prefer the explicit
case
s fortrue
andfalse
.eval
is a needlessly powerful and expensive tool to use here and reduces the readability of the code.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 will revert this change