Skip to content

Commit da96b10

Browse files
author
Helen Campbell
committed
(WIP) Addition of validate legacy function
1 parent e39fe01 commit da96b10

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Puppet::Functions.create_function(:validate_legacy, Puppet::Functions::InternalFunction) do
2+
dispatch :validate_legacy do
3+
param 'Type', :target_type
4+
param 'String', :previous_validation #Needs to be the name of the function previously used
5+
param 'NotUndef', :value #Really not sure what all this could be
6+
optional_param 'Any', :args # /need special stuff for any number of args herei
7+
end
8+
dispatch :validate_legacy_s do
9+
scope_param
10+
param 'String', :type_string
11+
param 'String', :previous_validation
12+
param 'NotUndef', :value
13+
optional_param 'Any', :args
14+
end
15+
16+
def validate_legacy_s(scope, type_string, *args)
17+
t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope)
18+
validate_legacy(t, *args)
19+
end
20+
21+
def validate_legacy(target_type, previous_validation, value, *prev_args)
22+
if assert_type(target_type, value)
23+
if previous_validation(previous_validation, value, *prev_args)
24+
# silently passes
25+
else
26+
# succeed with warning "Accepting previously invalid value for target_type"
27+
warning("Accepting previously invalid value for target_type '#{target_type}'")
28+
end
29+
else
30+
inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
31+
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch('@@validate_legacy', target_type, inferred_type)
32+
if previous_validation(previous_validation, value, *prev_args)
33+
# succeed with deprecation warning (use new error message)
34+
call_function('warning', error_msg)
35+
else
36+
# fail with new error message
37+
call_function('fail', error_msg)
38+
end
39+
end
40+
end
41+
42+
def previous_validation(previous_validation, value, *prev_args)
43+
# Call the previous validation function and catch any errors
44+
begin
45+
call_function(previous_validation, value, *prev_args)
46+
rescue Puppet::ParseError
47+
false
48+
end
49+
end
50+
51+
def assert_type(type, value)
52+
Puppet::Pops::Types::TypeCalculator.instance?(type, value)
53+
end
54+
end

0 commit comments

Comments
 (0)