Skip to content

Commit

Permalink
(PUP-11429) Make split() sensitive-aware
Browse files Browse the repository at this point in the history
Let split() accept Values of Type Sensitive.
In this Case the Returnvalue will also be of Type Sensitive.

(cherry picked from commit 940db71)
  • Loading branch information
cocker-cc authored and joshcooper committed Oct 5, 2023
1 parent 65f30af commit 89a0eb9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/puppet/functions/split.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
param 'Type[Regexp]', :pattern
end

dispatch :split_String_sensitive do
param 'Sensitive[String]', :sensitive
param 'String', :pattern
end

dispatch :split_Regexp_sensitive do
param 'Sensitive[String]', :sensitive
param 'Regexp', :pattern
end

dispatch :split_RegexpType_sensitive do
param 'Sensitive[String]', :sensitive
param 'Type[Regexp]', :pattern
end

def split_String(str, pattern)
str.split(Regexp.compile(pattern))
end
Expand All @@ -46,4 +61,16 @@ def split_Regexp(str, pattern)
def split_RegexpType(str, pattern)
str.split(pattern.regexp)
end
end

def split_String_sensitive(sensitive, pattern)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(split_String(sensitive.unwrap, pattern))
end

def split_Regexp_sensitive(sensitive, pattern)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(split_Regexp(sensitive.unwrap, pattern))
end

def split_RegexpType_sensitive(sensitive, pattern)
Puppet::Pops::Types::PSensitiveType::Sensitive.new(split_RegexpType(sensitive.unwrap, pattern))
end
end
6 changes: 6 additions & 0 deletions spec/unit/functions/split_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ def split(*args)
it 'should handle pattern in Regexp Type form with missing regular expression' do
expect(split('ab',type_parser.parse('Regexp'))).to eql(['a', 'b'])
end

it 'should handle sensitive String' do
expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), ',')).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), /,/)).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), type_parser.parse('Regexp[/,/]'))).to be_a(Puppet::Pops::Types::PSensitiveType::Sensitive)
end
end

0 comments on commit 89a0eb9

Please sign in to comment.