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
38 def initialize
39   @parser = Parser::Parser.new
40   @visitor = Visitor.new(nil, 'resolve', 2, 2)
41 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
48 def resolve(context, path)
49   factory = @parser.parse_string(path)
50   v = resolve_any(factory.model.body, context, path)
51   v.is_a?(Builder) ? v.resolve : v
52 end
resolve_AccessExpression(ast, context, path) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
58 def resolve_AccessExpression(ast, context, path)
59   bad_json_path(path) unless ast.keys.size == 1
60   receiver = resolve_any(ast.left_expr, context, path)
61   key = resolve_any(ast.keys[0], context, path)
62   if receiver.is_a?(Types::PuppetObject)
63     PCORE_TYPE_KEY == key ? receiver._pcore_type : receiver.send(key)
64   else
65     receiver[key]
66   end
67 end
resolve_CallMethodExpression(ast, context, path) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
107 def resolve_CallMethodExpression(ast, context, path)
108   bad_json_path(path) unless ast.arguments.empty?
109   resolve_any(ast.functor_expr, context, path)
110 end
resolve_LiteralDefault(_, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
97 def resolve_LiteralDefault(_, _, _)
98   'default'
99 end
resolve_LiteralUndef(_, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
93 def resolve_LiteralUndef(_, _, _)
94   'undef'
95 end
resolve_LiteralValue(ast, _, _) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
112 def resolve_LiteralValue(ast, _, _)
113   ast.value
114 end
resolve_NamedAccessExpression(ast, context, path) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
69 def resolve_NamedAccessExpression(ast, context, path)
70   receiver = resolve_any(ast.left_expr, context, path)
71   key = resolve_any(ast.right_expr, context, path)
72   if receiver.is_a?(Types::PuppetObject)
73     PCORE_TYPE_KEY == key ? receiver._pcore_type : receiver.send(key)
74   else
75     receiver[key]
76   end
77 end
resolve_Object(ast, _, path) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
116 def resolve_Object(ast, _, path)
117   bad_json_path(path)
118 end
resolve_QualifiedName(ast, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
79 def resolve_QualifiedName(ast, _, _)
80   v = ast.value
81   'null' == v ? nil : v
82 end
resolve_QualifiedReference(ast, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
84 def resolve_QualifiedReference(ast, _, _)
85   v = ast.cased_value
86   'null'.casecmp(v) == 0 ? nil : v
87 end
resolve_ReservedWord(ast, _, _) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
89 def resolve_ReservedWord(ast, _, _)
90   ast.word
91 end
resolve_VariableExpression(ast, context, path) click to toggle source
    # File lib/puppet/pops/serialization/json_path.rb
101 def resolve_VariableExpression(ast, context, path)
102   # A single '$' means root, i.e. the context.
103   bad_json_path(path) unless EMPTY_STRING == resolve_any(ast.expr, context, path)
104   context
105 end
resolve_any(ast, context, path) click to toggle source
   # File lib/puppet/pops/serialization/json_path.rb
54 def resolve_any(ast, context, path)
55   @visitor.visit_this_2(self, ast, context, path)
56 end

Private Instance Methods

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