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

Extend postgresql_conf to support multiple config files using ParsedFile #1542

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ekohl
Copy link
Collaborator

@ekohl ekohl commented Oct 23, 2023

To avoid a custom postgresql_conf implementation, this reverts back to ParsedFile. It relies on puppetlabs/puppet#9130 since there were bugs in it, which is why it's now a draft.

newparam(:key) do
desc 'The Postgresql parameter to manage.'

newparam(:key, namevar: true) do
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept key instead of name to force me to implement ParsedFile agnostic of the name parameter. At this point I think it could revert back to name.

Comment on lines +11 to +12
# TODO: make target optional
#[ /^([\w.]+)$/m, [ [:key] ] ],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have time to figure out how title_patterns works with matching, but it probably isn't hard.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that really depends on the point of view :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, couldn't help myself. I think the commented code should work. The reason it's commented is that I didn't know if it would really work. But this part suggests the patterns are correct:

require 'test/unit'

TITLE_PATTERNS = [ 
  [ /^(.+\.conf):([\w.]+)$/m, [ [:target], [:key] ] ],
  [ /^([\w.]+)$/m, [ [:key] ] ],
]

def match(title)
  h = {}
  TITLE_PATTERNS.each do |regexp, symbols_and_lambdas|
    captures = regexp.match(title.to_s)
    if captures
      symbols_and_lambdas.zip(captures[1..-1]).each do |symbol_and_lambda,capture|
        symbol, proc = symbol_and_lambda
        # Many types pass "identity" as the proc; we might as well give
        # them a shortcut to delivering that without the extra cost.
        # 
        # Especially because the global type defines title_patterns and
        # uses the identity patterns.
        # 
        # This was worth about 8MB of memory allocation saved in my
        # testing, so is worth the complexity for the API.
        if proc then
          h[symbol] = proc.call(capture)
        else
          h[symbol] = capture
        end
      end
      return h 
    end
  end
end

class MatcherTest < Test::Unit::TestCase
  def test_with_conf
    assert_equal({target: '/postgresql.conf', key: 'foo'}, match('/postgresql.conf:foo'))
  end

  def test_without_conf
    assert_equal({key: 'foo'}, match('foo'))
  end
end
$ ruby title_patterns.rb 
Loaded suite title_patterns
Started
Finished in 0.000252056 seconds.
--------------------------------------------------------------------------------------
2 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
--------------------------------------------------------------------------------------
7934.74 tests/s, 7934.74 assertions/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants