class Puppet::Pops::Types::PStringType

@api public

Constants

DEFAULT
ITERABLE_TYPE

Iterates over each character of the string

NON_EMPTY

Attributes

size_type_or_value[R]

Public Class Methods

new(size_type_or_value, deprecated_multi_args = EMPTY_ARRAY) click to toggle source
     # File lib/puppet/pops/types/types.rb
1509 def initialize(size_type_or_value, deprecated_multi_args = EMPTY_ARRAY)
1510   unless deprecated_multi_args.empty?
1511     if Puppet[:strict] != :off
1512       #TRANSLATORS 'PStringType#initialize' is a class and method name and should not be translated
1513       Puppet.warn_once('deprecations', "PStringType#initialize_multi_args",
1514                        _("Passing more than one argument to PStringType#initialize is deprecated"))
1515     end
1516     size_type_or_value = deprecated_multi_args[0]
1517   end
1518   @size_type_or_value = size_type_or_value.is_a?(PIntegerType) ? size_type_or_value.to_size : size_type_or_value
1519 end
new_function(type) click to toggle source
     # File lib/puppet/pops/types/types.rb
1588 def self.new_function(type)
1589   @new_function ||= Puppet::Functions.create_loaded_function(:new_string, type.loader) do
1590     local_types do
1591       type "Format = Pattern[/#{StringConverter::Format::FMT_PATTERN_STR}/]"
1592       type 'ContainerFormat = Struct[{
1593         Optional[format]         => Format,
1594         Optional[separator]      => String,
1595         Optional[separator2]     => String,
1596         Optional[string_formats] => Hash[Type, Format]
1597       }]'
1598       type 'TypeMap = Hash[Type, Variant[Format, ContainerFormat]]'
1599       type 'Convertible = Any'
1600       type 'Formats = Variant[Default, String[1], TypeMap]'
1601     end
1602 
1603     dispatch :from_args do
1604       param           'Convertible',  :from
1605       optional_param  'Formats',      :string_formats
1606     end
1607 
1608     def from_args(from, formats = :default)
1609       StringConverter.singleton.convert(from, formats)
1610     end
1611   end
1612 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1499 def self.register_ptype(loader, ir)
1500   create_ptype(loader, ir, 'ScalarDataType',
1501     'size_type_or_value' => {
1502       KEY_TYPE => POptionalType.new(PVariantType.new([PStringType::DEFAULT, PTypeType.new(PIntegerType::DEFAULT)])),
1503     KEY_VALUE => nil
1504   })
1505 end

Public Instance Methods

accept(visitor, guard) click to toggle source
Calls superclass method Puppet::Pops::Types::PAnyType#accept
     # File lib/puppet/pops/types/types.rb
1521 def accept(visitor, guard)
1522   super
1523   @size_type_or_value.accept(visitor, guard) if @size_type_or_value.is_a?(PIntegerType)
1524 end
derived_size_type() click to toggle source
     # File lib/puppet/pops/types/types.rb
1577 def derived_size_type
1578   if @size_type_or_value.is_a?(PIntegerType)
1579     @size_type_or_value
1580   elsif @size_type_or_value.is_a?(String)
1581     sz = @size_type_or_value.size
1582     PIntegerType.new(sz, sz)
1583   else
1584     PCollectionType::DEFAULT_SIZE
1585   end
1586 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
1542 def eql?(o)
1543   self.class == o.class && @size_type_or_value == o.size_type_or_value
1544 end
from_args(from, formats = :default) click to toggle source
     # File lib/puppet/pops/types/types.rb
1608 def from_args(from, formats = :default)
1609   StringConverter.singleton.convert(from, formats)
1610 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
1526 def generalize
1527   DEFAULT
1528 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
1530 def hash
1531   @size_type_or_value.hash
1532 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1546 def instance?(o, guard = nil)
1547   # true if size compliant
1548   if o.is_a?(String)
1549     if @size_type_or_value.is_a?(PIntegerType)
1550       @size_type_or_value.instance?(o.size, guard)
1551     else
1552       @size_type_or_value.nil? ? true : o == value
1553     end
1554   else
1555     false
1556   end
1557 end
iterable?(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1534 def iterable?(guard = nil)
1535   true
1536 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1538 def iterable_type(guard = nil)
1539   ITERABLE_TYPE
1540 end
size_type() click to toggle source
     # File lib/puppet/pops/types/types.rb
1573 def size_type
1574   @size_type_or_value.is_a?(PIntegerType) ? @size_type_or_value : nil
1575 end
value() click to toggle source
     # File lib/puppet/pops/types/types.rb
1559 def value
1560   @size_type_or_value.is_a?(PIntegerType) ? nil : @size_type_or_value
1561 end
values() click to toggle source

@deprecated @api private

     # File lib/puppet/pops/types/types.rb
1565 def values
1566   if Puppet[:strict] != :off
1567     #TRANSLATORS 'PStringType#values' and '#value' are classes and method names and should not be translated
1568     Puppet.warn_once('deprecations', "PStringType#values", _("Method PStringType#values is deprecated. Use #value instead"))
1569   end
1570   @value.is_a?(String) ? [@value] : EMPTY_ARRAY
1571 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

     # File lib/puppet/pops/types/types.rb
1623 def _assignable?(o, guard)
1624   if @size_type_or_value.is_a?(PIntegerType)
1625     # A general string is assignable by any other string or pattern restricted string
1626     # if the string has a size constraint it does not match since there is no reasonable way
1627     # to compute the min/max length a pattern will match. For enum, it is possible to test that
1628     # each enumerator value is within range
1629     case o
1630     when PStringType
1631       @size_type_or_value.assignable?(o.derived_size_type, guard)
1632 
1633     when PEnumType
1634       if o.values.empty?
1635         # enum represents all enums, and thus all strings, a sized constrained string can thus not
1636         # be assigned any enum (unless it is max size).
1637         @size_type_or_value.assignable?(PCollectionType::DEFAULT_SIZE, guard)
1638       else
1639         # true if all enum values are within range
1640         orange = o.values.map(&:size).minmax
1641         srange = @size_type_or_value.range
1642         # If o min and max are within the range of t
1643         srange[0] <= orange[0] && srange[1] >= orange[1]
1644       end
1645 
1646     when PPatternType
1647       # true if size constraint is at least 0 to +Infinity (which is the same as the default)
1648       @size_type_or_value.assignable?(PCollectionType::DEFAULT_SIZE, guard)
1649     else
1650       # no other type matches string
1651       false
1652     end
1653   else
1654     case o
1655     when PStringType
1656       # Must match exactly when value is a string
1657       @size_type_or_value.nil? || @size_type_or_value == o.size_type_or_value
1658     when PEnumType
1659       @size_type_or_value.nil? ? true : o.values.size == 1 && !o.case_insensitive? && o.values[0]
1660     when PPatternType
1661       @size_type_or_value.nil?
1662     else
1663       # All others are false, since no other type describes the same set of specific strings
1664       false
1665     end
1666   end
1667 end