Skip to content

Commit

Permalink
(PUP-11429) Make split() sensitive-aware
Browse files Browse the repository at this point in the history
  • Loading branch information
cocker-cc committed Aug 4, 2023
1 parent fc70183 commit 8950b94
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 @@ -36,6 +36,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 @@ -47,4 +62,16 @@ def split_Regexp(str, pattern)
def split_RegexpType(str, pattern)
str.split(pattern.regexp)
end
end

def split_String_sensitive(sensitive, pattern)
split_String(sensitive.unwrap, pattern)
end

def split_Regexp_sensitive(sensitive, pattern)
split_Regexp(sensitive.unwrap, pattern)
end

def split_RegexpType_sensitive(sensitive, pattern)
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 eql(['a', 'b'])
expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), /,/)).to eql(['a', 'b'])
expect(split(Puppet::Pops::Types::PSensitiveType::Sensitive.new('a,b'), type_parser.parse('Regexp[/,/]'))).to eql(['a', 'b'])
end
end

0 comments on commit 8950b94

Please sign in to comment.