-
Notifications
You must be signed in to change notification settings - Fork 143
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
Customizable key-value separator and quoting to be machine-readable #130
base: master
Are you sure you want to change the base?
Conversation
@sj26 I would love to get a review on this PR when you have a chance. Or let me know if there's anything I can do to help get this reviewed. |
Kindly pinging the marginalia authors (@arthurnn @jeremy) - is there anything I can do to help get this PR reviewed? Also pinging @balachandr and @odeke-em, as you both had comments in the original PR: #89 I did my best to write up a concise description of the new behavior, and demonstrate that it's all backwards compatible. |
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.
Would approach this as configuring a formatter rather than as configuring the low-level details of key/value output.
lib/marginalia.rb
Outdated
ensure | ||
prev.each { |(key, value)| Marginalia::Comment.send(:"#{key}=", value) } | ||
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.
Appears to be a test helper. Would move this to a utility method on the test case rather than introduce new toplevel API.
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.
Thanks for catching this 💯
I ended up removing this test helper, since it introduces indirection that doesn't seem worthwhile.
lib/marginalia/comment.rb
Outdated
def self.construct_comment | ||
ret = String.new | ||
self.components.each do |c| | ||
component_value = self.send(c) | ||
if component_value.present? | ||
ret << "#{c}:#{component_value}," | ||
ret << "#{c}#{key_value_separator}"\ | ||
"#{quote_value(component_value)}," |
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.
Suggest introducing a configurable formatter
block rather than separate low-level config points, including :default
and :sqlcommenter
shorthands for "standard" formatters.
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.
Done 👍
I added support for configuring "standard" formatters like :sqlcommenter
directly via Marginalia::Comment.update_formatter!(:sqlcommenter)
, and I removed configuration for key_value_separator
and quote_value
directly on the Comment
module since it seemed like YAGNI.
I agree that those low-level config points should be managed together, so I introduced a FormatterFactory
that handles the composition of these configs instead. I think this results in a better separation of concerns between the formatting and commenting logic as well. Open to feedback, of course :)
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.
Thanks @jeremy - I appreciate the great feedback!
Your suggestions led me to think deeper on the formatting logic, resulting in improvements to the API and code organization. Looking forward to your thoughts on the latest changes.
lib/marginalia.rb
Outdated
ensure | ||
prev.each { |(key, value)| Marginalia::Comment.send(:"#{key}=", value) } | ||
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.
Thanks for catching this 💯
I ended up removing this test helper, since it introduces indirection that doesn't seem worthwhile.
lib/marginalia/comment.rb
Outdated
def self.construct_comment | ||
ret = String.new | ||
self.components.each do |c| | ||
component_value = self.send(c) | ||
if component_value.present? | ||
ret << "#{c}:#{component_value}," | ||
ret << "#{c}#{key_value_separator}"\ | ||
"#{quote_value(component_value)}," |
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.
Done 👍
I added support for configuring "standard" formatters like :sqlcommenter
directly via Marginalia::Comment.update_formatter!(:sqlcommenter)
, and I removed configuration for key_value_separator
and quote_value
directly on the Comment
module since it seemed like YAGNI.
I agree that those low-level config points should be managed together, so I introduced a FormatterFactory
that handles the composition of these configs instead. I think this results in a better separation of concerns between the formatting and commenting logic as well. Open to feedback, of course :)
end | ||
|
||
desc "test sqlite3 driver" | ||
task :sqlite do | ||
sh "DRIVER=sqlite3 bundle exec ruby -Ilib -Itest test/*_test.rb" | ||
sh "DRIVER=sqlite3 bundle exec ruby -Ilib -Itest test/query_comments_test.rb" |
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 changed these globs from test/*_test.rb
to test/query_comments_test.rb
since that's the only file that needs to be matched for the database tests. It also allows us to run the formatting tests separately.
This pull request surfaces a new optional configuration to format the Marginalia comments in a machine-readable manner. See the changes to the README in this PR for a summary of the new feature.
Note that this PR is backwards-compatible with existing code. The new setting is optional, and if unset, the default values will retain all existing behavior. Unit tests have also be added to ensure there are no regressions.
Further notes:
TODO:
Note that some changes were made to the API of this feature over the course of this PR. Here is the original description, for posterity: