833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
|
# File 'lib/puppet/pops/types/types.rb', line 833
def self.new_function(type)
@new_function ||= Puppet::Functions.create_loaded_function(:new_numeric, type.loader) do
local_types do
type 'Convertible = Variant[Undef, Integer, Float, Boolean, String, Timespan, Timestamp]'
type 'NamedArgs = Struct[{from => Convertible, Optional[abs] => Boolean}]'
end
dispatch :from_args do
param 'Convertible', :from
optional_param 'Boolean', :abs
end
dispatch :from_hash do
param 'NamedArgs', :hash_args
end
def from_args(from, abs = false)
result = from_convertible(from)
abs ? result.abs : result
end
def from_hash(args_hash)
from_args(args_hash['from'], args_hash['abs'] || false)
end
def from_convertible(from)
case from
when NilClass
throw :undefined_value
when Float
from
when Integer
from
when Time::TimeData
from.to_f
when TrueClass
1
when FalseClass
0
when String
begin
if from[0] == '0' && (from[1].downcase == 'b' || from[1].downcase == 'x')
Integer(from)
else
Puppet::Pops::Utils.to_n(from)
end
rescue TypeError => e
raise TypeConversionError.new(e.message)
rescue ArgumentError => e
raise TypeConversionError.new(e.message)
end
else
t = Puppet::Pops::Types::TypeCalculator.singleton.infer(from).generalize
raise TypeConversionError.new("Value of type '#{t}' cannot be converted to Numeric")
end
end
end
end
|