class Puppet::Pops::Serialization::JsonPath::Resolver

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.

@api private

Public Class Methods

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

Public Instance Methods

resolve(context, path) click to toggle source

Resolve the given path in the given context. @param context [Object] the context used for resolution @param path [String] the json path @return [Object] the resolved value

   # File lib/puppet/pops/serialization/json_path.rb
49 def resolve(context, path)
50   factory = @parser.parse_string(path)
51   v = resolve_any(factory.model.body, context, path)
52   v.is_a?(Builder) ? v.resolve : v
53 end
resolve_AccessExpression(ast, context, path) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
59 def resolve_AccessExpression(ast, context, path)
60   bad_json_path(path) unless ast.keys.size == 1
61   receiver = resolve_any(ast.left_expr, context, path)
62   key = resolve_any(ast.keys[0], context, path)
63   if receiver.is_a?(Types::PuppetObject)
64     PCORE_TYPE_KEY == key ? receiver._pcore_type : receiver.send(key)
65   else
66     receiver[key]
67   end
68 end
resolve_CallMethodExpression(ast, context, path) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
108 def resolve_CallMethodExpression(ast, context, path)
109   bad_json_path(path) unless ast.arguments.empty?
110   resolve_any(ast.functor_expr, context, path)
111 end
resolve_LiteralDefault(_, _, _) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
 98 def resolve_LiteralDefault(_, _, _)
 99   'default'
100 end
resolve_LiteralUndef(_, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
94 def resolve_LiteralUndef(_, _, _)
95   'undef'
96 end
resolve_LiteralValue(ast, _, _) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
113 def resolve_LiteralValue(ast, _, _)
114   ast.value
115 end
resolve_NamedAccessExpression(ast, context, path) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
70 def resolve_NamedAccessExpression(ast, context, path)
71   receiver = resolve_any(ast.left_expr, context, path)
72   key = resolve_any(ast.right_expr, context, path)
73   if receiver.is_a?(Types::PuppetObject)
74     PCORE_TYPE_KEY == key ? receiver._pcore_type : receiver.send(key)
75   else
76     receiver[key]
77   end
78 end
resolve_Object(ast, _, path) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
117 def resolve_Object(ast, _, path)
118   bad_json_path(path)
119 end
resolve_QualifiedName(ast, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
80 def resolve_QualifiedName(ast, _, _)
81   v = ast.value
82   'null' == v ? nil : v
83 end
resolve_QualifiedReference(ast, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
85 def resolve_QualifiedReference(ast, _, _)
86   v = ast.cased_value
87   'null'.casecmp(v) == 0 ? nil : v
88 end
resolve_ReservedWord(ast, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
90 def resolve_ReservedWord(ast, _, _)
91   ast.word
92 end
resolve_VariableExpression(ast, context, path) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
102 def resolve_VariableExpression(ast, context, path)
103   # A single '$' means root, i.e. the context.
104   bad_json_path(path) unless EMPTY_STRING == resolve_any(ast.expr, context, path)
105   context
106 end
resolve_any(ast, context, path) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
55 def resolve_any(ast, context, path)
56   @visitor.visit_this_2(self, ast, context, path)
57 end

Private Instance Methods

bad_json_path(path) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
121 def bad_json_path(path)
122   raise SerializationError, _('Unable to parse jsonpath "%{path}"') % { :path => path }
123 end