Class: Puppet::Pops::Types::PHashType
Constant Summary
collapse
- DEFAULT =
PHashType.new(nil, nil)
- KEY_PAIR_TUPLE_SIZE =
PIntegerType.new(2, 2)
- DEFAULT_KEY_PAIR_TUPLE =
PTupleType.new([PUnitType::DEFAULT, PUnitType::DEFAULT], KEY_PAIR_TUPLE_SIZE)
- EMPTY =
PHashType.new(PUnitType::DEFAULT, PUnitType::DEFAULT, PIntegerType.new(0, 0))
Puppet::Pops::Types::PCollectionType::DEFAULT_SIZE, Puppet::Pops::Types::PCollectionType::NOT_EMPTY_SIZE, Puppet::Pops::Types::PCollectionType::ZERO_SIZE
Instance Attribute Summary collapse
#size_type
Class Method Summary
collapse
Instance Method Summary
collapse
#has_empty_range?, #size_range
Methods inherited from PAnyType
#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #kind_of_callable?, #loader, #name, #new_function, #really_instance?, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s
_pcore_type, create_ptype, register_ptypes
#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s
Constructor Details
#initialize(key_type, value_type, size_type = nil) ⇒ PHashType
Returns a new instance of PHashType.
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
|
# File 'lib/puppet/pops/types/types.rb', line 2688
def initialize(key_type, value_type, size_type = nil)
super(size_type)
if !size_type.nil? && size_type.from == 0 && size_type.to == 0
@key_type = PUnitType::DEFAULT
@value_type = PUnitType::DEFAULT
else
@key_type = key_type.nil? ? PAnyType::DEFAULT : key_type
@value_type = value_type.nil? ? PAnyType::DEFAULT : value_type
end
end
|
Instance Attribute Details
2686
2687
2688
|
# File 'lib/puppet/pops/types/types.rb', line 2686
def key_type
@key_type
end
|
#value_type ⇒ Object
2686
2687
2688
|
# File 'lib/puppet/pops/types/types.rb', line 2686
def value_type
@value_type
end
|
Class Method Details
.array_as_hash(value) ⇒ Object
2778
2779
2780
2781
2782
2783
2784
|
# File 'lib/puppet/pops/types/types.rb', line 2778
def self.array_as_hash(value)
return value unless value.is_a?(Array)
result = {}
value.each_with_index { |v, idx| result[idx] = array_as_hash(v) }
result
end
|
.new_function(type) ⇒ Object
Returns a new function that produces a Hash
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
|
# File 'lib/puppet/pops/types/types.rb', line 2788
def self.new_function(type)
@new_function ||= Puppet::Functions.create_loaded_function(:new_hash, type.loader) do
local_types do
type 'KeyValueArray = Array[Tuple[Any,Any],1]'
type 'TreeArray = Array[Tuple[Array,Any],1]'
type 'NewHashOption = Enum[tree, hash_tree]'
end
dispatch :from_tree do
param 'TreeArray', :from
optional_param 'NewHashOption', :build_option
end
dispatch :from_tuples do
param 'KeyValueArray', :from
end
dispatch :from_array do
param 'Any', :from
end
def from_tuples(tuple_array)
tuple_array.to_h
end
def from_tree(tuple_array, build_option = nil)
if build_option.nil?
return from_tuples(tuple_array)
end
all_hashes = build_option == 'hash_tree'
result = {}
tuple_array.each do |entry|
path = entry[0]
value = entry[1]
if path.empty?
if value.is_a?(Array)
value.each_with_index { |v, idx| result[idx] = v }
else
result.merge!(value)
end
else
r = path[0..-2].reduce(result) { |memo, idx| (memo.is_a?(Array) || memo.has_key?(idx)) ? memo[idx] : memo[idx] = {} }
r[path[-1]] = (all_hashes ? PHashType.array_as_hash(value) : value)
end
end
result
end
def from_array(from)
case from
when Array
if from.size == 0
{}
else
unless from.size.even?
raise TypeConversionError, _('odd number of arguments for Hash')
end
Hash[*from]
end
when Hash
from
else
if PIterableType::DEFAULT.instance?(from)
Hash[*Iterable.on(from).to_a]
else
t = TypeCalculator.singleton.infer(from).generalize
raise TypeConversionError, _("Value of type %{type} cannot be converted to Hash") % { type: t }
end
end
end
end
end
|
.register_ptype(loader, ir) ⇒ Object
Instance Method Details
#accept(visitor, guard) ⇒ Object
2699
2700
2701
2702
2703
|
# File 'lib/puppet/pops/types/types.rb', line 2699
def accept(visitor, guard)
super
@key_type.accept(visitor, guard)
@value_type.accept(visitor, guard)
end
|
#element_type ⇒ Object
2705
2706
2707
2708
2709
2710
2711
2712
|
# File 'lib/puppet/pops/types/types.rb', line 2705
def element_type
if Puppet[:strict] != :off
Puppet.warn_once('deprecations', 'Puppet::Pops::Types::PHashType#element_type',
_('Puppet::Pops::Types::PHashType#element_type is deprecated, use #value_type instead'))
end
@value_type
end
|
#eql?(o) ⇒ Boolean
2764
2765
2766
|
# File 'lib/puppet/pops/types/types.rb', line 2764
def eql?(o)
super && @key_type == o.key_type && @value_type == o.value_type
end
|
#generalize ⇒ Object
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
|
# File 'lib/puppet/pops/types/types.rb', line 2714
def generalize
if self == DEFAULT || self == EMPTY
self
else
key_t = @key_type
key_t = key_t.generalize
value_t = @value_type
value_t = value_t.generalize
@size_type.nil? && @key_type.equal?(key_t) && @value_type.equal?(value_t) ? self : PHashType.new(key_t, value_t, nil)
end
end
|
2736
2737
2738
|
# File 'lib/puppet/pops/types/types.rb', line 2736
def hash
super ^ @key_type.hash ^ @value_type.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
|
# File 'lib/puppet/pops/types/types.rb', line 2740
def instance?(o, guard = nil)
return false unless o.instance_of?(Hash)
if o.keys.all? { |key| @key_type.instance?(key, guard) } && o.values.all? { |value| @value_type.instance?(value, guard) }
size_t = size_type
size_t.nil? || size_t.instance?(o.size, guard)
else
false
end
end
|
#is_the_empty_hash? ⇒ Boolean
2768
2769
2770
|
# File 'lib/puppet/pops/types/types.rb', line 2768
def is_the_empty_hash?
self == EMPTY
end
|
#iterable?(guard = nil) ⇒ Boolean
2752
2753
2754
|
# File 'lib/puppet/pops/types/types.rb', line 2752
def iterable?(guard = nil)
true
end
|
#iterable_type(guard = nil) ⇒ Object
#normalize(guard = nil) ⇒ Object
2726
2727
2728
2729
2730
2731
2732
2733
2734
|
# File 'lib/puppet/pops/types/types.rb', line 2726
def normalize(guard = nil)
if self == DEFAULT || self == EMPTY
self
else
key_t = @key_type.normalize(guard)
value_t = @value_type.normalize(guard)
@size_type.nil? && @key_type.equal?(key_t) && @value_type.equal?(value_t) ? self : PHashType.new(key_t, value_t, @size_type)
end
end
|
#resolve(loader) ⇒ Object
2772
2773
2774
2775
2776
|
# File 'lib/puppet/pops/types/types.rb', line 2772
def resolve(loader)
rkey_type = @key_type.resolve(loader)
rvalue_type = @value_type.resolve(loader)
rkey_type.equal?(@key_type) && rvalue_type.equal?(@value_type) ? self : self.class.new(rkey_type, rvalue_type, @size_type)
end
|