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

Fixes #36912 - support multi-line type definitions #367

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,]'}) }
Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if those trailing comma will break something 😁

Copy link
Member Author

Choose a reason for hiding this comment

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

added a test to ensure that it's fine

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