Class: Puppet::Pops::Types::PVariantType

Inherits:
PAnyType show all
Includes:
Enumerable
Defined in:
lib/puppet/pops/types/types.rb

Overview

A flexible type describing an any? of other types

Constant Summary collapse

DEFAULT =
PVariantType.new(EMPTY_ARRAY)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #iterable?, #iterable_type, #loader, #name, #new_function, new_function, #resolve, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Constructor Details

#initialize(types) ⇒ PVariantType

Returns a new instance of PVariantType.

Parameters:

  • types (Array[PAnyType])

    the variants


2904
2905
2906
# File 'lib/puppet/pops/types/types.rb', line 2904

def initialize(types)
  @types = types.freeze
end

Instance Attribute Details

#typesObject (readonly)


2889
2890
2891
# File 'lib/puppet/pops/types/types.rb', line 2889

def types
  @types
end

Class Method Details

.flatten_variants(types) ⇒ Object


2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
# File 'lib/puppet/pops/types/types.rb', line 2970

def self.flatten_variants(types)
  modified = false
  types = types.map do |t|
    if t.is_a?(PVariantType)
      modified = true
      t.types
    else
      t
    end
  end
  types.flatten! if modified
  types
end

.maybe_create(types) ⇒ PAnyType

Checks if the number of unique types in the given array is greater than one, and if so creates a Variant with those types and returns it. If only one unique type is found, that type is instead returned.

Parameters:

  • types (Array<PAnyType>)

    the variants

Returns:


2898
2899
2900
2901
# File 'lib/puppet/pops/types/types.rb', line 2898

def self.maybe_create(types)
  types = flatten_variants(types).uniq
  types.size == 1 ? types[0] : new(types)
end

.register_ptype(loader, ir) ⇒ Object


2885
2886
2887
# File 'lib/puppet/pops/types/types.rb', line 2885

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType', 'types' => PArrayType.new(PTypeType::DEFAULT))
end

Instance Method Details

#accept(visitor, guard) ⇒ Object


2908
2909
2910
2911
# File 'lib/puppet/pops/types/types.rb', line 2908

def accept(visitor, guard)
  super
  @types.each { |t| t.accept(visitor, guard) }
end

#assignable?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)

3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
# File 'lib/puppet/pops/types/types.rb', line 3011

def assignable?(o, guard = nil)
  # an empty Variant does not match Undef (it is void - not even undef)
  if o.is_a?(PUndefType) && types.empty?
    return false
  end

  return super unless o.is_a?(PVariantType)
  # If empty, all Variant types match irrespective of the types they hold (including being empty)
  return true if types.empty?
  # Since this variant is not empty, an empty Variant cannot match, because it matches nothing
  # otherwise all types in o must be assignable to this
  !o.types.empty? && o.types.all? { |vt| super(vt, guard) }
end

#eachObject


2913
2914
2915
2916
2917
2918
2919
# File 'lib/puppet/pops/types/types.rb', line 2913

def each
  if block_given?
    types.each { |t| yield t }
  else
    types.to_enum
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)

3005
3006
3007
# File 'lib/puppet/pops/types/types.rb', line 3005

def eql?(o)
  self.class == o.class && @types.size == o.types.size && (@types - o.types).empty?
end

#generalizeObject


2921
2922
2923
2924
2925
2926
2927
# File 'lib/puppet/pops/types/types.rb', line 2921

def generalize
  if self == DEFAULT
    self
  else
    alter_type_array(@types, :generalize) { |altered| PVariantType.maybe_create(altered) }
  end
end

#hashObject


2984
2985
2986
# File 'lib/puppet/pops/types/types.rb', line 2984

def hash
  @types.hash
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)

2988
2989
2990
2991
# File 'lib/puppet/pops/types/types.rb', line 2988

def instance?(o, guard = nil)
  # instance of variant if o is instance? of any of variant's types
  @types.any? { |type| type.instance?(o, guard) }
end

#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)

3001
3002
3003
# File 'lib/puppet/pops/types/types.rb', line 3001

def kind_of_callable?(optional = true, guard = nil)
  @types.all? { |type| type.kind_of_callable?(optional, guard) }
end

#normalize(guard = nil) ⇒ Object


2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
# File 'lib/puppet/pops/types/types.rb', line 2929

def normalize(guard = nil)
  if self == DEFAULT || @types.empty?
    self
  else
    # Normalize all contained types
    modified = false
    types = alter_type_array(@types, :normalize, guard)
    if types == self
      types = @types
    else
      modified = true
    end

    if types.size == 1
      types[0]
    elsif types.any? { |t| t.is_a?(PUndefType) || t.is_a?(POptionalType) }
      # Undef entry present. Use an OptionalType with a normalized Variant without Undefs and Optional wrappers
      POptionalType.new(PVariantType.maybe_create(types.reject { |t| t.is_a?(PUndefType) }.map { |t| t.is_a?(POptionalType) ? t.type : t })).normalize
    else
      # Merge all variants into this one
      types = PVariantType.flatten_variants(types)
      size_before_merge = types.size

      types = swap_not_undefs(types)
      types = merge_enums(types)
      types = merge_patterns(types)
      types = merge_version_ranges(types)
      types = merge_numbers(PIntegerType, types)
      types = merge_numbers(PFloatType, types)
      types = merge_numbers(PTimespanType, types)
      types = merge_numbers(PTimestampType, types)

      if types.size == 1
        types[0]
      else
        modified || types.size != size_before_merge ? PVariantType.maybe_create(types) : self
      end
    end
  end
end

#really_instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)

2993
2994
2995
2996
2997
2998
2999
# File 'lib/puppet/pops/types/types.rb', line 2993

def really_instance?(o, guard = nil)
  @types.reduce(-1) do |memo, type|
    ri = type.really_instance?(o, guard)
    break ri if ri > 0
    ri > memo ? ri : memo
  end
end