Skip to content

Commit

Permalink
Fixes #36912 - support multi-line type definitions
Browse files Browse the repository at this point in the history
This strips line breaks from type definitions, thus making them
parseable again.

This will probably horribly break if type manifests would contain
anything else but type definitions, but Puppet recommends [1]
to store them in separate files and only have one type alias per file.

[1] https://www.puppet.com/docs/puppet/7/lang_type_aliases
  • Loading branch information
evgeni authored and ekohl committed Nov 10, 2023
1 parent 4b44b57 commit 1bd5fe5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/kafo/data_type_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@ class DataTypeParser

def initialize(manifest)
@logger = KafoConfigure.logger
@types = {}

lines = []
type_line_without_newlines = +''
manifest.each_line do |line|
if (type = TYPE_DEFINITION.match(line.force_encoding("UTF-8")))
line = line.force_encoding("UTF-8").strip
next if line.start_with?('#')

line = line.split(' #').first.strip
if line =~ TYPE_DEFINITION
lines << type_line_without_newlines
type_line_without_newlines = line
else
type_line_without_newlines << line
end
end
lines << type_line_without_newlines

@types = {}
lines.each do |line|
if (type = TYPE_DEFINITION.match(line))
@types[type[1]] = type[2]
end
end
Expand Down
24 changes: 24 additions & 0 deletions test/kafo/data_type_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ module Kafo
it { _(parser.types).must_equal({'Ipv4' => 'Pattern[/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/]'}) }
end

describe "parse pattern with a hash inside" do
let (:file) do
<<~'PUPPET'
type Stdlib::Email = Pattern[/\A[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z/]
PUPPET
end
it { _(parser.types).must_equal({"Stdlib::Email"=>"Pattern[/\\A[a-zA-Z0-9.!\#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\\z/]"}) }
end

describe "parse multiline alias" do
let(:file) { "type IP = Variant[\n IPv4,\n IPv6,\n]" }
it { _(parser.types).must_equal({'IP' => 'Variant[IPv4,IPv6,]'}) }
end

describe "parse multiline alias with EOL comment" do
let(:file) { "type IP = Variant[\n IPv4, # Legacy IP\n IPv6,\n]" }
it { _(parser.types).must_equal({'IP' => 'Variant[IPv4,IPv6,]'}) }
end

describe "parse multiple multiline aliases" do
let(:file) { "# We need IP\ntype IP = Variant[\n IPv4, # Legacy IP\n IPv6,\n]\n# We also need IPProto\ntype IPProto = Variant[\n TCP,\n UDP,\n]" }
it { _(parser.types).must_equal({"IP"=>"Variant[IPv4,IPv6,]", "IPProto"=>"Variant[TCP,UDP,]"}) }
end

describe "#register" do
after { DataType.unregister_type('Test') }
let(:file) { 'type Test = String' }
Expand Down
5 changes: 5 additions & 0 deletions test/kafo/data_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ module Kafo
_(DataType.new_from_string('Example[1,Float, /(regexp)/, Enum["foo", \'bar\'],-2]')).must_equal 'instance'
end

it 'instantiates type with trailing comma' do
data_type.expect(:new, 'instance', ['1', 'Float'])
_(DataType.new_from_string('Example[1,Float,]')).must_equal 'instance'
end

it 'instantiates type with multiple nested arguments' do
data_type.expect(:new, 'instance', ['Hash[String, String]', 'Hash[String, String]'])
_(DataType.new_from_string('Example[Hash[String, String], Hash[String, String]]')).must_equal 'instance'
Expand Down

0 comments on commit 1bd5fe5

Please sign in to comment.