Class: Puppet::Pops::Types::PHashType

Inherits:
PCollectionType show all
Defined in:
lib/puppet/pops/types/types.rb

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)
DATA =
PHashType.new(PScalarType::DEFAULT, PDataType::DEFAULT, DEFAULT_SIZE)
EMPTY =
PHashType.new(PUnitType::DEFAULT, PUnitType::DEFAULT, PIntegerType.new(0, 0))

Constants inherited from PCollectionType

Puppet::Pops::Types::PCollectionType::DEFAULT_SIZE, Puppet::Pops::Types::PCollectionType::NOT_EMPTY_SIZE, Puppet::Pops::Types::PCollectionType::ZERO_SIZE

Instance Attribute Summary collapse

Attributes inherited from PCollectionType

#element_type, #size_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PCollectionType

#has_empty_range?, #size_range

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #check_self_recursion, #kind_of_callable?, #name, #new_function, #really_instance?, #simple_name, simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from PuppetObject

#_ptype

Constructor Details

#initialize(key_type, value_type, size_type = nil) ⇒ PHashType

Returns a new instance of PHashType.



2353
2354
2355
2356
2357
2358
2359
2360
# File 'lib/puppet/pops/types/types.rb', line 2353

def initialize(key_type, value_type, size_type = nil)
  super(value_type, size_type)
  if !size_type.nil? && size_type.from == 0 && size_type.to == 0
    @key_type = PUnitType::DEFAULT
  else
    @key_type = key_type
  end
end

Instance Attribute Details

#key_typeObject



2350
2351
2352
# File 'lib/puppet/pops/types/types.rb', line 2350

def key_type
  @key_type
end

Class Method Details

.new_function(_, loader) ⇒ Object

Returns a new function that produces a Hash



2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
# File 'lib/puppet/pops/types/types.rb', line 2438

def self.new_function(_, loader)
  @new_function ||= Puppet::Functions.create_loaded_function(:new_hash, loader) do
    local_types do
      type 'KeyValueArray = Array[Tuple[Any,Any],1]'
    end

    dispatch :from_tuples do
      param           'KeyValueArray',  :from
    end

    dispatch :from_array do
      param           'Any',  :from
    end

    def from_tuples(tuple_array)
      Hash[tuple_array]
    end

    def from_array(from)
      case from
      when NilClass
        throw :undefined_value
      when Array
        if from.size == 0
          {}
        else
          unless from.size % 2 == 0
            raise TypeConversionError.new("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 = Puppet::Pops::Types::TypeCalculator.singleton.infer(from).generalize
          raise TypeConversionError.new("Value of type '#{t}' cannot be converted to Hash")
        end
      end
    end
  end
end

.register_ptype(loader, ir) ⇒ Object



2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
# File 'lib/puppet/pops/types/types.rb', line 2332

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'CollectionType',
    'key_type' => {
      KEY_TYPE => POptionalType.new(PType::DEFAULT),
      KEY_VALUE => nil
    },
    'element_type' => {
      KEY_TYPE => POptionalType.new(PType::DEFAULT),
      KEY_VALUE => nil,
      KEY_OVERRIDE => true
    },
    'size_type' => {
      KEY_TYPE => POptionalType.new(PType.new(PIntegerType::DEFAULT)),
      KEY_VALUE => nil
    }
  )
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



2362
2363
2364
2365
# File 'lib/puppet/pops/types/types.rb', line 2362

def accept(visitor, guard)
  super
  @key_type.accept(visitor, guard) unless @key_type.nil?
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


2420
2421
2422
# File 'lib/puppet/pops/types/types.rb', line 2420

def eql?(o)
  super && @key_type == o.key_type
end

#generalizeObject



2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
# File 'lib/puppet/pops/types/types.rb', line 2367

def generalize
  if self == DEFAULT || self == DATA || self == EMPTY
    self
  else
    key_t = @key_type
    key_t = key_t.generalize unless key_t.nil?
    value_t = @element_type
    value_t = value_t.generalize unless value_t.nil?
    @size_type.nil? && @key_type.equal?(key_t) && @element_type.equal?(value_t) ? self : PHashType.new(key_t, value_t, nil)
  end
end

#hashObject



2391
2392
2393
# File 'lib/puppet/pops/types/types.rb', line 2391

def hash
  super ^ @key_type.hash
end

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

Returns:

  • (Boolean)


2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
# File 'lib/puppet/pops/types/types.rb', line 2395

def instance?(o, guard = nil)
  return false unless o.is_a?(Hash)
  key_t = key_type
  element_t = element_type
  if (key_t.nil? || o.keys.all? {|key| key_t.instance?(key, guard) }) &&
      (element_t.nil? || o.values.all? {|value| element_t.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

Returns:

  • (Boolean)


2424
2425
2426
# File 'lib/puppet/pops/types/types.rb', line 2424

def is_the_empty_hash?
  self == EMPTY
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


2408
2409
2410
# File 'lib/puppet/pops/types/types.rb', line 2408

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



2412
2413
2414
2415
2416
2417
2418
# File 'lib/puppet/pops/types/types.rb', line 2412

def iterable_type(guard = nil)
  if self == DEFAULT || self == EMPTY
    PIterableType.new(DEFAULT_KEY_PAIR_TUPLE)
  else
    PIterableType.new(PTupleType.new([@key_type, @element_type], KEY_PAIR_TUPLE_SIZE))
  end
end

#normalize(guard = nil) ⇒ Object



2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
# File 'lib/puppet/pops/types/types.rb', line 2379

def normalize(guard = nil)
  if self == DEFAULT || self == DATA || self == EMPTY
    self
  else
    key_t = @key_type
    key_t = key_t.normalize(guard) unless key_t.nil?
    value_t = @element_type
    value_t = value_t.normalize(guard) unless value_t.nil?
    @size_type.nil? && @key_type.equal?(key_t) && @element_type.equal?(value_t) ? self : PHashType.new(key_t, value_t, nil)
  end
end

#resolve(type_parser, loader) ⇒ Object



2428
2429
2430
2431
2432
2433
2434
# File 'lib/puppet/pops/types/types.rb', line 2428

def resolve(type_parser, loader)
  rkey_type = @key_type
  rkey_type = rkey_type.resolve(type_parser, loader) unless rkey_type.nil?
  rvalue_type = @element_type
  rvalue_type = rvalue_type.resolve(type_parser, loader) unless rvalue_type.nil?
  rkey_type.equal?(@key_type) && rvalue_type.equal?(@element_type) ? self : self.class.new(rkey_type, rvalue_type, @size_type)
end