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.



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

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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