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.



39
40
41
42
# File 'lib/puppet/pops/serialization/json_path.rb', line 39

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



49
50
51
52
53
# File 'lib/puppet/pops/serialization/json_path.rb', line 49

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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/pops/serialization/json_path.rb', line 59

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.



55
56
57
# File 'lib/puppet/pops/serialization/json_path.rb', line 55

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.



108
109
110
111
# File 'lib/puppet/pops/serialization/json_path.rb', line 108

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.



98
99
100
# File 'lib/puppet/pops/serialization/json_path.rb', line 98

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.



94
95
96
# File 'lib/puppet/pops/serialization/json_path.rb', line 94

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.



113
114
115
# File 'lib/puppet/pops/serialization/json_path.rb', line 113

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.



70
71
72
73
74
75
76
77
78
# File 'lib/puppet/pops/serialization/json_path.rb', line 70

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.



117
118
119
# File 'lib/puppet/pops/serialization/json_path.rb', line 117

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.



80
81
82
83
# File 'lib/puppet/pops/serialization/json_path.rb', line 80

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.



85
86
87
88
# File 'lib/puppet/pops/serialization/json_path.rb', line 85

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.



90
91
92
# File 'lib/puppet/pops/serialization/json_path.rb', line 90

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.



102
103
104
105
106
# File 'lib/puppet/pops/serialization/json_path.rb', line 102

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