Skip to content

Commit 73da4ee

Browse files
committed
Merge pull request #185 from Sharpie/HI-127-escaped-lookups
(HI-127) Add 'literal' function for escaping lookups
2 parents 27e318c + c5041a6 commit 73da4ee

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/hiera/interpolate.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class Hiera::Interpolate
55
class << self
66
INTERPOLATION = /%\{([^\}]*)\}/
7-
METHOD_INTERPOLATION = /%\{(scope|hiera)\(['"]([^"']*)["']\)\}/
7+
METHOD_INTERPOLATION = /%\{(scope|hiera|literal)\(['"]([^"']*)["']\)\}/
88

99
def interpolate(data, scope, extra_data)
1010
if data.is_a?(String)
@@ -24,6 +24,10 @@ def do_interpolation(data, recurse_guard, scope, extra_data)
2424
recurse_guard.check(interpolation_variable) do
2525
interpolate_method, key = get_interpolation_method_and_key(data)
2626
interpolated_data = send(interpolate_method, data, key, scope, extra_data)
27+
28+
# Halt recursion if we encounter a literal.
29+
return interpolated_data if interpolate_method == :literal_interpolate
30+
2731
do_interpolation(interpolated_data, recurse_guard, scope, extra_data)
2832
end
2933
else
@@ -37,6 +41,7 @@ def get_interpolation_method_and_key(data)
3741
case match[1]
3842
when 'hiera' then [:hiera_interpolate, match[2]]
3943
when 'scope' then [:scope_interpolate, match[2]]
44+
when 'literal' then [:literal_interpolate, match[2]]
4045
end
4146
elsif (match = data.match(INTERPOLATION))
4247
[:scope_interpolate, match[1]]
@@ -58,5 +63,10 @@ def hiera_interpolate(data, key, scope, extra_data)
5863
Hiera::Backend.lookup(key, nil, scope, nil, :priority)
5964
end
6065
private :hiera_interpolate
66+
67+
def literal_interpolate(data, key, scope, extra_data)
68+
key
69+
end
70+
private :literal_interpolate
6171
end
6272
end

spec/unit/backend_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ class Hiera
268268

269269
Backend.parse_string(input, scope).should == "answer"
270270
end
271+
272+
it "replaces literal interpolations with their argument" do
273+
scope = {}
274+
input = "%{literal('%')}{rspec::data}"
275+
Backend.parse_string(input, scope).should == "%{rspec::data}"
276+
end
271277
end
272278

273279
describe "#parse_answer" do

0 commit comments

Comments
 (0)