Class: Puppet::Pops::Serialization::JsonPath::Resolver Private

Inherits:
Object
  • Object
show all
Extended by:
Concurrent::ThreadLocalSingleton
Defined in:
lib/puppet/pops/serialization/json_path.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Resolver for JSON path that uses the Puppet parser to create the AST. The path must start with ‘$’ which denotes the value that is passed into the parser. This parser can easily be extended with more elaborate resolution mechanisms involving document sets.

The parser is limited to constructs generated by the JsonPath#to_json_path method.

Instance Method Summary collapse

Methods included from Concurrent::ThreadLocalSingleton

singleton

Constructor Details

#initializeResolver

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Resolver.

 40 41 42 43
# File 'lib/puppet/pops/serialization/json_path.rb', line 40 def initialize @parser = Parser::Parser.new @visitor = Visitor.new(nil, 'resolve', 2, 2) end

Instance Method Details

#resolve(context, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve the given path in the given context.

Parameters:

  • context (Object)

    the context used for resolution

  • path (String)

    the json path

Returns:

  • (Object)

    the resolved value

 50 51 52 53 54
# File 'lib/puppet/pops/serialization/json_path.rb', line 50 def resolve(context, path) factory = @parser.parse_string(path) v = resolve_any(factory.model.body, context, path) v.is_a?(Builder) ? v.resolve : v end

#resolve_AccessExpression(ast, context, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 60 61 62 63 64 65 66 67 68 69
# File 'lib/puppet/pops/serialization/json_path.rb', line 60 def resolve_AccessExpression(ast, context, path) bad_json_path(path) unless ast.keys.size == 1 receiver = resolve_any(ast.left_expr, context, path) key = resolve_any(ast.keys[0], context, path) if receiver.is_a?(Types::PuppetObject) PCORE_TYPE_KEY == key ? receiver._pcore_type : receiver.send(key) else receiver[key] end end

#resolve_any(ast, context, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 56 57 58
# File 'lib/puppet/pops/serialization/json_path.rb', line 56 def resolve_any(ast, context, path) @visitor.visit_this_2(self, ast, context, path) end

#resolve_CallMethodExpression(ast, context, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 109 110 111 112
# File 'lib/puppet/pops/serialization/json_path.rb', line 109 def resolve_CallMethodExpression(ast, context, path) bad_json_path(path) unless ast.arguments.empty? resolve_any(ast.functor_expr, context, path) end

#resolve_LiteralDefault(_, _, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 99 100 101
# File 'lib/puppet/pops/serialization/json_path.rb', line 99 def resolve_LiteralDefault(_, _, _) 'default' end

#resolve_LiteralUndef(_, _, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 95 96 97
# File 'lib/puppet/pops/serialization/json_path.rb', line 95 def resolve_LiteralUndef(_, _, _) 'undef' end

#resolve_LiteralValue(ast, _, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 114 115 116
# File 'lib/puppet/pops/serialization/json_path.rb', line 114 def resolve_LiteralValue(ast, _, _) ast.value end

#resolve_NamedAccessExpression(ast, context, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 71 72 73 74 75 76 77 78 79
# File 'lib/puppet/pops/serialization/json_path.rb', line 71 def resolve_NamedAccessExpression(ast, context, path) receiver = resolve_any(ast.left_expr, context, path) key = resolve_any(ast.right_expr, context, path) if receiver.is_a?(Types::PuppetObject) PCORE_TYPE_KEY == key ? receiver._pcore_type : receiver.send(key) else receiver[key] end end

#resolve_Object(ast, _, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 118 119 120
# File 'lib/puppet/pops/serialization/json_path.rb', line 118 def resolve_Object(ast, _, path) bad_json_path(path) end

#resolve_QualifiedName(ast, _, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 81 82 83 84
# File 'lib/puppet/pops/serialization/json_path.rb', line 81 def resolve_QualifiedName(ast, _, _) v = ast.value 'null' == v ? nil : v end

#resolve_QualifiedReference(ast, _, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 86 87 88 89
# File 'lib/puppet/pops/serialization/json_path.rb', line 86 def resolve_QualifiedReference(ast, _, _) v = ast.cased_value 'null'.casecmp(v) == 0 ? nil : v end

#resolve_ReservedWord(ast, _, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 91 92 93
# File 'lib/puppet/pops/serialization/json_path.rb', line 91 def resolve_ReservedWord(ast, _, _) ast.word end

#resolve_VariableExpression(ast, context, path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 103 104 105 106 107
# File 'lib/puppet/pops/serialization/json_path.rb', line 103 def resolve_VariableExpression(ast, context, path) # A single '$' means root, i.e. the context.  bad_json_path(path) unless EMPTY_STRING == resolve_any(ast.expr, context, path) context end