Uploaded image for project: 'Puppet'
  1. Puppet
  2. PUP-7364

Parser result no longer contains current method

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: PUP 5.0.0
    • Fix Version/s: PUP 5.1.0
    • Component/s: Language
    • Labels:
      None
    • Template:
    • Epic Link:
    • Sub-team:
    • Team:
      Platform Core
    • Story Points:
      1
    • Sprint:
      Platform Core 2017-08-08
    • Release Notes:
      Not Needed
    • Release Notes Summary:
      Advanced feature in Ruby API of the parser.
    • QA Risk Assessment:
      No Action

      Description

      The puppet debugger uses the AST model to get a model object in order to import itself into the known resource types. In order to make this work with puppet 5 I had to refactor my code slightly to account for the missing method. Not a huge deal but this might break other third party code.

      Previously I was calling parse_result.current to get the model. However, with puppet 5 I actually don't need to do that anymore since the model object is the parsed result.

      But because puppet-debugger supports puppet 3-5 I had to write the following code to work around the issue. Puppet 5 should add a current method to the parsed_result object that points to itself, thus keeping third party integrations working.

      Example:

      # adds backward compatible call so we don't break third party programs.
      def current
        self
      end
      

      Puppet debugger code that was modified

      parse_result = parser.parse_string(string, '')
      model = parse_result.respond_to?(:current) ? parse_result.current : parse_result
      

      Bigger Code snippet from puppet-debugger

      # @param String - any valid puppet language code
          # @return Hostclass - a puppet Program object which is considered the main class
          def generate_ast(string = nil)
            parse_result = parser.parse_string(string, '')
            # the parse_result may be
            # * empty / nil (no input)
            # * a Model::Program
            # * a Model::Expression
            #
            # should return nil or Puppet::Pops::Model::Program
            # puppet 5 does not have the method current
            model = parse_result.respond_to?(:current) ? parse_result.current : parse_result
            args = {}
            ::Puppet::Pops::Model::AstTransformer.new('').merge_location(args, model)
       
            ast_code =
              if model.is_a? ::Puppet::Pops::Model::Program
                ::Puppet::Parser::AST::PopsBridge::Program.new(model, args)
              else
                args[:value] = model
                ::Puppet::Parser::AST::PopsBridge::Expression.new(args)
              end
            # Create the "main" class for the content - this content will get merged with all other "main" content
            ::Puppet::Parser::AST::Hostclass.new('', code: ast_code)
          end
       
          # @param String - any valid puppet language code
          # @return Object - returns either a string of the result or object from puppet evaulation
          def puppet_eval(input)
            # in order to add functions to the scope the loaders must be created
            # in order to call native functions we need to set the global_scope
            ast = generate_ast(input)
            # record the input for puppet to retrieve and reference later
            file = Tempfile.new(['puppet_debugger_input', '.pp'])
            File.open(file, 'w') do |f|
              f.write(input)
            end
            Puppet.override({ code: input, global_scope: scope, loaders: scope.compiler.loaders }, 'For puppet-debugger') do
              # because the repl is not a module we leave the modname blank
              scope.environment.known_resource_types.import_ast(ast, '')
              parser.evaluate_string(scope, input, File.expand_path(file))
            end
          end
      

      https://github.com/nwops/puppet-debugger

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              cosman2001 Corey Osman
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Zendesk Support