BBlack has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/274400

Change subject: stdlib: add function hash_select_re
......................................................................

stdlib: add function hash_select_re

Bug: T127481
Change-Id: I6c6928fc0800670213cfa977b2bede8573f39061
---
A modules/stdlib/lib/puppet/parser/functions/hash_select_re.rb
A modules/stdlib/spec/unit/puppet/parser/functions/hash_select_re_spec.rb
2 files changed, 62 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/00/274400/1

diff --git a/modules/stdlib/lib/puppet/parser/functions/hash_select_re.rb 
b/modules/stdlib/lib/puppet/parser/functions/hash_select_re.rb
new file mode 100644
index 0000000..7cde12e
--- /dev/null
+++ b/modules/stdlib/lib/puppet/parser/functions/hash_select_re.rb
@@ -0,0 +1,34 @@
+#
+# hash_select_re.rb
+#
+
+module Puppet::Parser::Functions
+  newfunction(:hash_select_re, :type => :rvalue, :doc => <<-EOS
+This function creates a new hash from the input hash, filtering out keys which
+do not match the provided regex.
+
+*Examples:*
+
+    $in = { 'abc' => 1, 'def' => 2, 'asdf' => 3 }
+    $out = hash_select_re('^a', $in);
+    # $out == { 'abc' => 1, 'asdf' => 3 }
+    $out2 = hash_select_re('^(?!)a', $in);
+    # $out2 == { 'def' => 2 }
+
+    EOS
+  ) do |arguments|
+
+    raise(Puppet::ParseError, "hash_select_re(): Wrong number of arguments " +
+      "given (#{arguments.size} for 2)") if arguments.size != 2
+
+    pattern = Regexp.new(arguments[0])
+    in_hash = arguments[1]
+    unless in_hash.is_a?(Hash)
+      raise(Puppet::ParseError, 'hash_select_re(): Argument 2 must be a hash')
+    end
+
+    in_hash.select { |k,v| pattern.match(k) }
+  end
+end
+
+# vim: set ts=2 sw=2 et :
diff --git 
a/modules/stdlib/spec/unit/puppet/parser/functions/hash_select_re_spec.rb 
b/modules/stdlib/spec/unit/puppet/parser/functions/hash_select_re_spec.rb
new file mode 100644
index 0000000..2662591
--- /dev/null
+++ b/modules/stdlib/spec/unit/puppet/parser/functions/hash_select_re_spec.rb
@@ -0,0 +1,28 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the hash_select_re function" do
+  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+  it "should exist" do
+    Puppet::Parser::Functions.function("hash_select_re").should == 
"function_hash_select_re"
+  end
+
+  it "should raise a ParseError if there are less than 2 arguments" do
+    lambda { scope.function_hash_select_re(['a']) }.should( 
raise_error(Puppet::ParseError))
+  end
+
+  it "should raise a ParseError if there are more than 2 arguments" do
+    lambda { scope.function_hash_select_re(['a', 'b', 'c']) }.should( 
raise_error(Puppet::ParseError))
+  end
+
+  it "should select the right keys (simple)" do
+    result = 
scope.function_hash_select_re(['^a',{'abc'=>1,'def'=>2,'asdf'=>3}])
+    result.should(eq({'abc'=>1,'asdf'=>3}))
+  end
+
+  it "should select the right keys (neg lookahead)" do
+    result = 
scope.function_hash_select_re(['^(?!)a',{'abc'=>1,'def'=>2,'asdf'=>3}])
+    result.should(eq({'def'=>2}))
+  end
+end

-- 
To view, visit https://gerrit.wikimedia.org/r/274400
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6c6928fc0800670213cfa977b2bede8573f39061
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: BBlack <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to