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
1510 def initialize(size_type_or_value, deprecated_multi_args = EMPTY_ARRAY)
1511   unless deprecated_multi_args.empty?
1512     if Puppet[:strict] != :off
1513       #TRANSLATORS 'PStringType#initialize' is a class and method name and should not be translated
1514       Puppet.warn_once('deprecations', "PStringType#initialize_multi_args",
1515                        _("Passing more than one argument to PStringType#initialize is deprecated"))
1516     end
1517     size_type_or_value = deprecated_multi_args[0]
1518   end
1519   @size_type_or_value = size_type_or_value.is_a?(PIntegerType) ? size_type_or_value.to_size : size_type_or_value
1520 end
new_function(type) click to toggle source
     # File lib/puppet/pops/types/types.rb
1589 def self.new_function(type)
1590   @new_function ||= Puppet::Functions.create_loaded_function(:new_string, type.loader) do
1591     local_types do
1592       type "Format = Pattern[/#{StringConverter::Format::FMT_PATTERN_STR}/]"
1593       type 'ContainerFormat = Struct[{
1594         Optional[format]         => Format,
1595         Optional[separator]      => String,
1596         Optional[separator2]     => String,
1597         Optional[string_formats] => Hash[Type, Format]
1598       }]'
1599       type 'TypeMap = Hash[Type, Variant[Format, ContainerFormat]]'
1600       type 'Convertible = Any'
1601       type 'Formats = Variant[Default, String[1], TypeMap]'
1602     end
1603 
1604     dispatch :from_args do
1605       param           'Convertible',  :from
1606       optional_param  'Formats',      :string_formats
1607     end
1608 
1609     def from_args(from, formats = :default)
1610       StringConverter.singleton.convert(from, formats)
1611     end
1612   end
1613 end
register_ptype(loader, ir) click to toggle source
     # File lib/puppet/pops/types/types.rb
1500 def self.register_ptype(loader, ir)
1501   create_ptype(loader, ir, 'ScalarDataType',
1502     'size_type_or_value' => {
1503       KEY_TYPE => POptionalType.new(PVariantType.new([PStringType::DEFAULT, PTypeType.new(PIntegerType::DEFAULT)])),
1504     KEY_VALUE => nil
1505   })
1506 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
1522 def accept(visitor, guard)
1523   super
1524   @size_type_or_value.accept(visitor, guard) if @size_type_or_value.is_a?(PIntegerType)
1525 end
derived_size_type() click to toggle source
     # File lib/puppet/pops/types/types.rb
1578 def derived_size_type
1579   if @size_type_or_value.is_a?(PIntegerType)
1580     @size_type_or_value
1581   elsif @size_type_or_value.is_a?(String)
1582     sz = @size_type_or_value.size
1583     PIntegerType.new(sz, sz)
1584   else
1585     PCollectionType::DEFAULT_SIZE
1586   end
1587 end
eql?(o) click to toggle source
     # File lib/puppet/pops/types/types.rb
1543 def eql?(o)
1544   self.class == o.class && @size_type_or_value == o.size_type_or_value
1545 end
from_args(from, formats = :default) click to toggle source
     # File lib/puppet/pops/types/types.rb
1609 def from_args(from, formats = :default)
1610   StringConverter.singleton.convert(from, formats)
1611 end
generalize() click to toggle source
     # File lib/puppet/pops/types/types.rb
1527 def generalize
1528   DEFAULT
1529 end
hash() click to toggle source
     # File lib/puppet/pops/types/types.rb
1531 def hash
1532   @size_type_or_value.hash
1533 end
instance?(o, guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1547 def instance?(o, guard = nil)
1548   # true if size compliant
1549   if o.is_a?(String)
1550     if @size_type_or_value.is_a?(PIntegerType)
1551       @size_type_or_value.instance?(o.size, guard)
1552     else
1553       @size_type_or_value.nil? ? true : o == value
1554     end
1555   else
1556     false
1557   end
1558 end
iterable?(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1535 def iterable?(guard = nil)
1536   true
1537 end
iterable_type(guard = nil) click to toggle source
     # File lib/puppet/pops/types/types.rb
1539 def iterable_type(guard = nil)
1540   ITERABLE_TYPE
1541 end
size_type() click to toggle source
     # File lib/puppet/pops/types/types.rb
1574 def size_type
1575   @size_type_or_value.is_a?(PIntegerType) ? @size_type_or_value : nil
1576 end
value() click to toggle source
     # File lib/puppet/pops/types/types.rb
1560 def value
1561   @size_type_or_value.is_a?(PIntegerType) ? nil : @size_type_or_value
1562 end
values() click to toggle source

@deprecated @api private

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

Protected Instance Methods

_assignable?(o, guard) click to toggle source

@api private

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