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

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

merged 1 commit into from
Nov 10, 2023

Conversation

evgeni
Copy link
Member

@evgeni evgeni commented Nov 10, 2023

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

@@ -24,6 +24,16 @@ module Kafo
it { _(parser.types).must_equal({'Ipv4' => 'Pattern[/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/]'}) }
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

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

What if we use puppet-strings to parse it? There's this part in the JSON:

  "data_type_aliases": [
    {
      "name": "Candlepin::LogLevel",
      "file": "types/loglevel.pp",
      "line": 2,
      "docstring": {
        "text": "",
        "tags": [
          {
            "tag_name": "summary",
            "text": "A log4j log level"
          }
        ]
      },
      "alias_of": "Enum['ALL', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'OFF', 'TRACE']"
    }
  ],

In #343 I started to utilize the bulk parsing approach. Current kafo_parsers calls puppet strings for every file. The bulk parsing approach is to call it for multiple files (or even for a whole module at once) and then extract the data from that file.

Since the data type aliases are also in there, we could leverage that and avoid writing our own parser.

line = line.force_encoding("UTF-8").strip
next if line.start_with?('#')

line = line.split('#').first.strip
Copy link
Member

Choose a reason for hiding this comment

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

This will break if you use a Pattern with # in it.

Copy link
Member Author

@evgeni evgeni Nov 10, 2023

Choose a reason for hiding this comment

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

So will the current regex, no?

TYPE_DEFINITION = /^type\s+([^\s=]+)\s*=\s*(.+?)(\s+#.*)?\s*$/

Ah, it ensures at least one space before the #

Copy link
Member Author

Choose a reason for hiding this comment

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

However, https://www.puppet.com/docs/puppet/7/lang_comments doesn't say anything about leading spaces

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 for the one Pattern I could find using a # inside, Stdlib::Email :)

@evgeni
Copy link
Member Author

evgeni commented Nov 10, 2023

What if we use puppet-strings to parse it?

This sounds like the correct approach long term, but it also seems like a bigger re-work is required to get that going correctly?

@evgeni evgeni force-pushed the i36912 branch 2 times, most recently from 4928690 to a90cc79 Compare November 10, 2023 11:13
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

This sounds like the correct approach long term, but it also seems like a bigger re-work is required to get that going correctly?

Probably. The patches were written quite some time ago, but never really finished. It did give a massive speed up in building the installer so perhaps it's the excuse I need to properly wrap it up.

But short term this at least allows us to proceed.


line = line.split(' #').first.strip
if line =~ TYPE_DEFINITION
lines << last_line
Copy link
Member

Choose a reason for hiding this comment

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

What's the idea behind the last_line here? When is that ever relevant for a type definition?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, it's a buffer where I store all the lines in one string to drop the newlines.

I renamed the variable now, to make it more obvious

lib/kafo/data_type_parser.rb Outdated Show resolved Hide resolved
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
@ekohl
Copy link
Member

ekohl commented Nov 10, 2023

Let's see what theforeman/foreman-installer@66f3669 does in CI.

@evgeni
Copy link
Member Author

evgeni commented Nov 10, 2023

it didn't explode

image

@ekohl ekohl merged commit 1bd5fe5 into master Nov 10, 2023
3 checks passed
@evgeni evgeni deleted the i36912 branch November 10, 2023 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants