Class: Puppet::Pops::Types::StringConverter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/string_converter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Converts Puppet runtime objects to String under the control of a Format. Use from Puppet Language is via the function ‘new`.

Defined Under Namespace

Classes: Format, FormatError, Indentation

Constant Summary collapse

DEFAULT_INDENTATION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Indentation.new(0, true, false).freeze
DEFAULT_CONTAINER_FORMATS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

format used by default for values in a container (basically strings are quoted since they may contain a ‘,’))

{
  PAnyType::DEFAULT  => Format.new('%p').freeze,   # quoted string (Ruby inspect)
}.freeze
DEFAULT_ARRAY_FORMAT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Format.new('%a')
DEFAULT_HASH_FORMAT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Format.new('%h')
DEFAULT_HASH_DELIMITERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

['{', '}'].freeze
DEFAULT_ARRAY_DELIMITERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

['[', ']'].freeze
DEFAULT_STRING_FORMATS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  PFloatType::DEFAULT    => Format.new('%f').freeze,    # float
  PNumericType::DEFAULT  => Format.new('%d').freeze,    # decimal number
  PArrayType::DEFAULT    => DEFAULT_ARRAY_FORMAT.freeze,
  PHashType::DEFAULT     => DEFAULT_HASH_FORMAT.freeze,
  PBinaryType::DEFAULT   => Format.new('%B').freeze,    # strict base64 string unquoted
  PAnyType::DEFAULT      => Format.new('%s').freeze,    # unquoted string
}.freeze
FMT_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w{separator separator2 format string_formats}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringConverter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of StringConverter.



249
250
251
# File 'lib/puppet/pops/types/string_converter.rb', line 249

def initialize
  @@string_visitor   ||= Visitor.new(self, "string", 3, 3)
end

Class Method Details

.convert(value, string_formats = :default) ⇒ Object



236
237
238
# File 'lib/puppet/pops/types/string_converter.rb', line 236

def self.convert(value, string_formats = :default)
  singleton.convert(value, string_formats)
end

.singletonTypeConverter

Returns the singleton instance.

Returns:

  • (TypeConverter)

    the singleton instance



243
244
245
# File 'lib/puppet/pops/types/string_converter.rb', line 243

def self.singleton
  @tconv_instance ||= new
end

Instance Method Details

#convert(value, string_formats = :default) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the given value to a String, under the direction of formatting rules per type.

When converting to string it is possible to use a set of built in conversion rules.

A format is specified on the form:

´´´%[Flags][.Precision]Format ´´´

‘Width` is the number of characters into which the value should be fitted. This allocated space is padded if value is shorter. By default it is space padded, and the flag 0 will cause padding with 0 for numerical formats.

‘Precision` is the number of fractional digits to show for floating point, and the maximum characters included in a string format.

Note that all data type supports the formats ‘s` and `p` with the meaning “default to-string” and “default-programmatic to-string”.

### Integer

| Format | Integer Formats | —— | ————— | d | Decimal, negative values produces leading ‘-’ | x X | Hexadecimal in lower or upper case. Uses ..f/..F for negative values unless # is also used | o | Octal. Uses ..0 for negative values unless # is also used | b B | Binary with prefix ‘b’ or ‘B’. Uses ..1/..1 for negative values unless # is also used | c | numeric value representing a Unicode value, result is a one unicode character string, quoted if alternative flag # is used | s | same as d, or d in quotes if alternative flag # is used | p | same as d | eEfgGaA | converts integer to float and formats using the floating point rules

Defaults to ‘d`

### Float

| Format | Float formats | —— | ————- | f | floating point in non exponential notation | e E | exponential notation with ‘e’ or ‘E’ | g G | conditional exponential with ‘e’ or ‘E’ if exponent < -4 or >= the precision | a A | hexadecimal exponential form, using ‘x’/‘X’ as prefix and ‘p’/‘P’ before exponent | s | converted to string using format p, then applying string formatting rule, alternate form # quotes result | p | f format with minimum significant number of fractional digits, prec has no effect | dxXobBc | converts float to integer and formats using the integer rules

Defaults to ‘p`

### String

| Format | String | —— | —— | s | unquoted string, verbatim output of control chars | p | programmatic representation - strings are quoted, interior quotes and control chars are escaped

| C | each

name segment capitalized, quoted if alternative flag # is used

| c | capitalized string, quoted if alternative flag # is used | d | downcased string, quoted if alternative flag # is used | u | upcased string, quoted if alternative flag # is used | t | trims leading and trailing whitespace from the string, quoted if alternative flag # is used

Defaults to ‘s` at top level and `p` inside array or hash.

### Boolean

| Format | Boolean Formats | —- | ——————-

| t T | ‘true’/‘false’ or ‘True’/‘False’ , first char if alternate form is used (i.e. ‘t’/‘f’ or ‘T’/‘F’). | y Y | ‘yes’/‘no’, ‘Yes’/‘No’, ‘y’/‘n’ or ‘Y’/‘N’ if alternative flag # is used | dxXobB | numeric value 0/1 in accordance with the given format which must be valid integer format | eEfgGaA | numeric value 0.0/1.0 in accordance with the given float format and flags | s | ‘true’ / ‘false’ | p | ‘true’ / ‘false’

### Regexp

| Format | Regexp Formats (%/) | —- | —————— | s | / / delimiters, alternate flag replaces / delimiters with quotes | p | / / delimiters

### Undef

| Format | Undef formats | —— | ————- | s | empty string, or quoted empty string if alternative flag # is used | p | ‘undef’, or quoted ‘“undef”’ if alternative flag # is used | n | ‘nil’, or ‘null’ if alternative flag # is used | dxXobB | ‘NaN’ | eEfgGaA | ‘NaN’ | v | ‘n/a’ | V | ‘N/A’ | u | ‘undef’, or ‘undefined’ if alternative # flag is used

### Default (value)

| Format | Default formats | —— | ————— | d D | ‘default’ or ‘Default’, alternative form # causes value to be quoted | s | same as d | p | same as d

### Binary (value)

| Format | Default formats | —— | ————— | s | binary as unquoted characters | p | ‘Binary(“<base64strict>”)’ | b | ‘<base64>’ - base64 string with newlines inserted | B | ‘<base64strict>’ - base64 strict string (without newlines inserted) | u | ‘<base64urlsafe>’ - base64 urlsafe string | t | ‘Binary’ - outputs the name of the type only | T | ‘BINARY’ - output the name of the type in all caps only

The alternate form flag ‘#` will quote the binary or base64 text output The width and precision values are applied to the text part only in `%p` format.

### Array & Tuple

| Format | Array/Tuple Formats | —— | ————- | a | formats with ‘[ ]` delimiters and `,`, alternate form `#` indents nested arrays/hashes | s | same as a | p | same as a

See “Flags” ‘<[({|` for formatting of delimiters, and “Additional parameters for containers; Array and Hash” for more information about options.

The alternate form flag ‘#` will cause indentation of nested array or hash containers. If width is also set it is taken as the maximum allowed length of a sequence of elements (not including delimiters). If this max length is exceeded, each element will be indented.

### Hash & Struct

| Format | Hash/Struct Formats | —— | ————- | h | formats with ‘{ }` delimiters, `,` element separator and ` => ` inner element separator unless overridden by flags | s | same as h | p | same as h | a | converts the hash to an array of [k,v] tuples and formats it using array rule(s)

See “Flags” ‘<[({|` for formatting of delimiters, and “Additional parameters for containers; Array and Hash” for more information about options.

The alternate form flag ‘#` will format each hash key/value entry indented on a separate line.

### Type

| Format | Array/Tuple Formats | —— | ————- | s | The same as p, quoted if alternative flag # is used | p | Outputs the type in string form as specified by the Puppet Language

### Flags

| Flag | Effect | —— | —— | (space) | space instead of + for numeric output (- is shown), for containers skips delimiters | # | alternate format; prefix 0x/0x, 0 (octal) and 0b/0B for binary, Floats force decimal ‘.’. For g/G keep trailing 0. | + | show sign +/- depending on value’s sign, changes x,X, o,b, B format to not use 2’s complement form | - | left justify the value in the given width | 0 | pad with 0 instead of space for widths larger than value | <[({| | defines an enclosing pair <> [] () {} or | | when used with a container type

### Additional parameters for containers; Array and Hash

For containers (Array and Hash), the format is specified by a hash where the following keys can be set:

  • ‘’format’‘ - the format specifier for the container itself

  • ‘’separator’‘ - the separator string to use between elements, should not contain padding space at the end

  • ‘’separator2’‘ - the separator string to use between association of hash entries key/value

  • ‘’string_formats’´ - a map of type to format for elements contained in the container

Note that the top level format applies to Array and Hash objects contained/nested in an Array or a Hash.

Given format mappings are merged with (default) formats and a format specified for a narrower type wins over a broader.

Parameters:

  • mode (String, Symbol)

    :strict or :extended (or :default which is the same as :strict)

  • string_formats (String, Hash) (defaults to: :default)

    format tring, or a hash mapping type to a format string, and for Array and Hash types map to hash of details



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/puppet/pops/types/string_converter.rb', line 468

def convert(value, string_formats = :default)
  options = DEFAULT_STRING_FORMATS

  value_type = TypeCalculator.infer_set(value)
  if string_formats.is_a?(String)
    # add the format given for the exact type
    string_formats = { value_type => string_formats }
  end

  case string_formats
  when :default
   # do nothing, use default formats

  when Hash
    # Convert and validate user input
    string_formats = validate_input(string_formats)
    # Merge user given with defaults such that user options wins, merge is deep and format specific
    options = Format.merge_string_formats(DEFAULT_STRING_FORMATS, string_formats)
  else
    raise ArgumentError, "string conversion expects a Default value or a Hash of type to format mappings, got a '#{string_formats.class}'"
  end

  _convert(value_type, value, options, DEFAULT_INDENTATION)
end

#is_a_or_h?(x) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


928
929
930
# File 'lib/puppet/pops/types/string_converter.rb', line 928

def is_a_or_h?(x)
  x.is_a?(Array) || x.is_a?(Hash)
end

#is_container?(t) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


932
933
934
935
936
937
938
939
# File 'lib/puppet/pops/types/string_converter.rb', line 932

def is_container?(t)
  case t
  when PArrayType, PHashType, PStructType, PTupleType
    true
  else
    false
  end
end

#puppet_quote(str) ⇒ String

Performs a ‘%p’ formatting of the given str such that the output conforms to Puppet syntax. An ascii string without control characters, dollar, single-qoute, or backslash, will be quoted using single quotes. All other strings will be quoted using double quotes.

Parameters:

  • str (String)

    the string that should be formatted

Returns:

  • (String)

    the formatted string



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/puppet/pops/types/string_converter.rb', line 804

def puppet_quote(str)
  if str.ascii_only? && (str =~ /(?:'|\$|\p{Cntrl}|\\)/).nil?
    "'#{str}'"
  else
    bld = '"'
    str.codepoints do |codepoint|
      case codepoint
      when 0x09
        bld << '\\t'
      when 0x0a
        bld << '\\n'
      when 0x0d
        bld << '\\r'
      when 0x22
        bld << '\\"'
      when 0x24
        bld << '\\$'
      when 0x5c
        bld << '\\\\'
      else
        if codepoint < 0x20 || codepoint > 0x7f
          bld << sprintf('\\u{%X}', codepoint)
        else
          bld.concat(codepoint)
        end
      end
    end
    bld << '"'
    bld
  end
end

#puppet_safe(str) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Given an unsafe string make it safe for puppet



558
559
560
561
562
563
# File 'lib/puppet/pops/types/string_converter.rb', line 558

def puppet_safe(str)
  str = str.inspect # all specials are now quoted
  # all $ variables must be quoted
  str.gsub!("\$", "\\\$")
  str
end

#string_PAnyType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Basically string_PAnyType converts the value to a String and then formats it according to the resulting type



569
570
571
572
# File 'lib/puppet/pops/types/string_converter.rb', line 569

def string_PAnyType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  Kernel.format(f.orig_fmt, val)
end

#string_PArrayType(val_type, val, format_map, indentation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/puppet/pops/types/string_converter.rb', line 851

def string_PArrayType(val_type, val, format_map, indentation)
  format         = get_format(val_type, format_map)
  sep            = format.separator || DEFAULT_ARRAY_FORMAT.separator
  string_formats = format.container_string_formats || DEFAULT_CONTAINER_FORMATS
  delims         = format.delimiter_pair(DEFAULT_ARRAY_DELIMITERS)

  # Make indentation active, if array is in alternative format, or if nested in indenting
  indentation = indentation.indenting(format.alt? || indentation.is_indenting?)

  case format.format
  when :a, :s, :p
    buf = ''
    if indentation.breaks?
      buf << "\n"
      buf << indentation.padding
    end
    buf << delims[0]

    # Make a first pass to format each element
    children_indentation = indentation.increase(format.alt?) # tell children they are expected to indent
    mapped = val.map do |v|
      if children_indentation.first?
        children_indentation = children_indentation.subsequent
      end
      val_t = TypeCalculator.infer_set(v)
      _convert(val_t, v, is_container?(val_t) ? format_map : string_formats, children_indentation)
    end

    # compute widest run in the array, skip nested arrays and hashes
    # then if size > width, set flag if a break on each element should be performed
    if format.alt? && format.width
      widest = val.each_with_index.reduce([0]) do | memo, v_i |
        # array or hash breaks
        if is_a_or_h?(v_i[0])
          memo << 0
        else
          memo[-1] += mapped[v_i[1]].length
        end
        memo
      end
      widest = widest.max
      sz_break = widest > (format.width || Float::INFINITY)
    else
      sz_break = false
    end

    # output each element with breaks and padding
    children_indentation = indentation.increase(format.alt?)
    val.each_with_index do |v, i|
      str_val = mapped[i]
      if children_indentation.first?
        children_indentation = children_indentation.subsequent
        # if breaking, indent first element by one
        if sz_break && !is_a_or_h?(v)
          buf << ' '
        end
      else
        buf << sep
        # if break on each (and breaking will not occur because next is an array or hash)
        # or, if indenting, and previous was an array or hash, then break and continue on next line
        # indented.
        if (sz_break && !is_a_or_h?(v)) || (format.alt? && i > 0 && is_a_or_h?(val[i-1]) && !is_a_or_h?(v))
          buf << "\n"
          buf << children_indentation.padding
        elsif !(format.alt? && is_a_or_h?(v))
          buf << ' '
        end
      end
      buf << str_val
    end
    buf << delims[1]
    buf
  else
    raise FormatError.new('Array', format.format, 'asp')
  end
end

#string_PBinaryType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'lib/puppet/pops/types/string_converter.rb', line 716

def string_PBinaryType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  substitute = f.alt? ? 'p' : 's'
  case f.format
  when :s
    val_to_convert = val.binary_buffer
    if !f.alt?
      # Assume it is valid UTF-8
      val_to_convert = val_to_convert.dup.force_encoding('UTF-8')
      # If it isn't
      unless val_to_convert.valid_encoding?
        # try to convert and fail with details about what is wrong
        val_to_convert = val.binary_buffer.encode('UTF-8')
      end
    else
      val_to_convert = val.binary_buffer
    end
    Kernel.format(f.orig_fmt.gsub('s', substitute), val_to_convert)

  when :p
    # width & precision applied to string, not the the name of the type
    "Binary(\"#{Kernel.format(f.orig_fmt.gsub('p', 's'), val.to_s)}\")"

  when :b
    Kernel.format(f.orig_fmt.gsub('b', substitute), val.relaxed_to_s)

  when :B
    Kernel.format(f.orig_fmt.gsub('B', substitute), val.to_s)

  when :u
    Kernel.format(f.orig_fmt.gsub('u', substitute), val.urlsafe_to_s)

  when :t
    # Output as the type without any data
    Kernel.format(f.orig_fmt.gsub('t', substitute), 'Binary')

  when :T
    # Output as the type without any data in all caps
    Kernel.format(f.orig_fmt.gsub('T', substitute), 'BINARY')

  else
    raise FormatError.new('Binary', f.format, 'bButTsp')
  end
end

#string_PBooleanType(val_type, val, format_map, indentation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/puppet/pops/types/string_converter.rb', line 610

def string_PBooleanType(val_type, val, format_map, indentation)
  f = get_format(val_type, format_map)
  case f.format
  when :t
    # 'true'/'false' or 't'/'f' if in alt mode
    str_bool = val.to_s
    apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

  when :T
    # 'True'/'False' or 'T'/'F' if in alt mode
    str_bool = val.to_s.capitalize
    apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

  when :y
    # 'yes'/'no' or 'y'/'n' if in alt mode
    str_bool = val ? 'yes' : 'no'
    apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

  when :Y
    # 'Yes'/'No' or 'Y'/'N' if in alt mode
    str_bool = val ? 'Yes' : 'No'
    apply_string_flags(f, f.alt? ? str_bool[0] : str_bool)

  when :d, :x, :X, :o, :b, :B
    # Boolean in numeric form, formated by integer rule
    numeric_bool = val ? 1 : 0
    string_formats = { Puppet::Pops::Types::PIntegerType::DEFAULT => f}
    _convert(TypeCalculator.infer_set(numeric_bool), numeric_bool, string_formats, indentation)

  when :e, :E, :f, :g, :G, :a, :A
    # Boolean in numeric form, formated by float rule
    numeric_bool = val ? 1.0 : 0.0
    string_formats = { Puppet::Pops::Types::PFloatType::DEFAULT => f}
    _convert(TypeCalculator.infer_set(numeric_bool), numeric_bool, string_formats, indentation)

  when :s
    apply_string_flags(f, val.to_s)

  when :p
    apply_string_flags(f, val.inspect)

  else
    raise FormatError.new('Boolean', f.format, 'tTyYdxXobBeEfgGaAsp')
  end
end

#string_PDefaultType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



574
575
576
577
578
579
580
581
582
583
584
# File 'lib/puppet/pops/types/string_converter.rb', line 574

def string_PDefaultType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  apply_string_flags(f, case f.format
  when :d, :s, :p
    f.alt? ? '"default"' : 'default'
  when :D
    f.alt? ? '"Default"' : 'Default'
  else
    raise FormatError.new('Default', f.format, 'dDsp')
  end)
end

#string_PFloatType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/puppet/pops/types/string_converter.rb', line 697

def string_PFloatType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  case f.format
  when :d, :x, :X, :o, :b, :B
    Kernel.format(f.orig_fmt, val.to_i)

  when :e, :E, :f, :g, :G, :a, :A, :p
    Kernel.format(f.orig_fmt, val)

  when :s
    float_str = f.alt? ? "\"#{Kernel.format('%p', val)}\"" : Kernel.format('%p', val)
    Kernel.format(f.orig_fmt, float_str)

  else
    raise FormatError.new('Float', f.format, 'dxXobBeEfgGaAsp')
  end
end

#string_PHashType(val_type, val, format_map, indentation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/puppet/pops/types/string_converter.rb', line 953

def string_PHashType(val_type, val, format_map, indentation)
  format         = get_format(val_type, format_map)
  sep            = format.separator  || DEFAULT_HASH_FORMAT.separator
  assoc          = format.separator2 || DEFAULT_HASH_FORMAT.separator2
  string_formats = format.container_string_formats || DEFAULT_CONTAINER_FORMATS
  delims         = format.delimiter_pair(DEFAULT_HASH_DELIMITERS)

  sep = format.alt? ? "#{sep}\n" : "#{sep} "

  cond_break     = ''
  padding        = ''

  case format.format
  when :a
    # Convert to array and use array rules
    array_hash = val.to_a
    _convert(TypeCalculator.infer_set(array_hash), array_hash, format_map, indentation)

  when :h, :s, :p
    indentation = indentation.indenting(format.alt? || indentation.is_indenting?)
    buf = ''
    if indentation.breaks?
      buf << "\n"
      buf << indentation.padding
    end

    children_indentation = indentation.increase
    if format.alt?
      cond_break = "\n"
      padding = children_indentation.padding
    end
    buf << delims[0]
    buf << cond_break  # break after opening delimiter if pretty printing
    buf << val.map do |k,v|
      key_type = TypeCalculator.infer_set(k)
      val_type = TypeCalculator.infer_set(v)
      key = _convert(key_type, k, is_container?(key_type) ? format_map : string_formats, children_indentation)
      val = _convert(val_type, v, is_container?(val_type) ? format_map : string_formats, children_indentation)
      "#{padding}#{key}#{assoc}#{val}"
    end.join(sep)
    if format.alt?
      buf << cond_break
      buf << indentation.padding
    end
    buf << delims[1]
    buf
  else
    raise FormatError.new('Hash', format.format, 'hasp')
  end
end

#string_PIntegerType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/puppet/pops/types/string_converter.rb', line 672

def string_PIntegerType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  case f.format
  when :d, :x, :X, :o, :b, :B, :p
    Kernel.format(f.orig_fmt, val)

  when :e, :E, :f, :g, :G, :a, :A
    Kernel.format(f.orig_fmt, val.to_f)

  when :c
    char = [val].pack("U")
    char = f.alt? ? "\"#{char}\"" : char
    char = Kernel.format(f.orig_fmt.gsub('c','s'), char)

  when :s
    fmt = f.alt? ? 'p' : 's'
    int_str = Kernel.format('%d', val)
    Kernel.format(f.orig_fmt.gsub('s', fmt), int_str)

  else
    raise FormatError.new('Integer', f.format, 'dxXobBeEfgGaAspc')
  end
end

#string_PIteratorType(val_type, val, format_map, indentation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



947
948
949
950
# File 'lib/puppet/pops/types/string_converter.rb', line 947

def string_PIteratorType(val_type, val, format_map, indentation)
  v = val.to_a
  _convert(TypeCalculator.infer_set(v), v, format_map, indentation)
end

#string_PRegexpType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'lib/puppet/pops/types/string_converter.rb', line 837

def string_PRegexpType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  case f.format
  when :p
    Kernel.format(f.orig_fmt, val)
  when :s
    str_regexp = val.inspect
    str_regexp = f.alt? ? "\"#{str_regexp[1..-2]}\"" : str_regexp
    Kernel.format(f.orig_fmt, str_regexp)
  else
    raise FormatError.new('Regexp', f.format, 'rsp')
  end
end

#string_PRuntimeType(val_type, val, format_map) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/puppet/pops/types/string_converter.rb', line 541

def string_PRuntimeType(val_type, val, format_map)
  case (f = get_format(val_type, format_map))
  when :s
    val.to_s
  when :q
    val.to_s.inspect
  when :puppet
    puppet_safe(val.to_s)
  when :i, :d, :x, :o, :f, :puppet
    converted = convert(o, PNumericType) # rest is default
    "%#{f}" % converted
  else
    throw(:failed_conversion, [o, PStringType::DEFAULT, f])
  end
end

#string_PStringType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/puppet/pops/types/string_converter.rb', line 762

def string_PStringType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  case f.format
  when :s
    Kernel.format(f.orig_fmt, val)

  when :p
    apply_string_flags(f, puppet_quote(val))

  when :c
    c_val = val.capitalize
    f.alt? ? apply_string_flags(f, puppet_quote(c_val)) :  Kernel.format(f.orig_fmt.gsub('c', 's'), c_val)

  when :C
    c_val = val.split('::').map {|s| s.capitalize }.join('::')
    f.alt? ? apply_string_flags(f, puppet_quote(c_val)) :  Kernel.format(f.orig_fmt.gsub('C', 's'), c_val)

  when :u
    c_val = val.upcase
    f.alt? ? apply_string_flags(f, puppet_quote(c_val)) :  Kernel.format(f.orig_fmt.gsub('u', 's'), c_val)

  when :d
    c_val = val.downcase
    f.alt? ? apply_string_flags(f, puppet_quote(c_val)) :  Kernel.format(f.orig_fmt.gsub('d', 's'), c_val)

  when :t  # trim
    c_val = val.strip
    f.alt? ? apply_string_flags(f, puppet_quote(c_val)) :  Kernel.format(f.orig_fmt.gsub('t', 's'), c_val)

  else
    raise FormatError.new('String', f.format, 'cCudspt')
  end
end

#string_PStructType(val_type, val, format_map, indentation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1005
1006
1007
# File 'lib/puppet/pops/types/string_converter.rb', line 1005

def string_PStructType(val_type, val, format_map, indentation)
  string_PHashType(val_type, val, format_map, indentation)
end

#string_PTupleType(val_type, val, format_map, indentation) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



942
943
944
# File 'lib/puppet/pops/types/string_converter.rb', line 942

def string_PTupleType(val_type, val, format_map, indentation)
  string_PArrayType(val_type, val, format_map, indentation)
end

#string_PType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/puppet/pops/types/string_converter.rb', line 1010

def string_PType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  case f.format
  when :s
    str_val = f.alt? ? "\"#{val.to_s}\"" : val.to_s
    Kernel.format(f.orig_fmt, str_val)
  when :p
    Kernel.format(f.orig_fmt.gsub('p', 's'), val.to_s)
  else
    raise FormatError.new('Type', f.format, 'sp')
  end
end

#string_PUndefType(val_type, val, format_map, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/puppet/pops/types/string_converter.rb', line 587

def string_PUndefType(val_type, val, format_map, _)
  f = get_format(val_type, format_map)
  apply_string_flags(f, case f.format
  when :n
    f.alt? ? 'null' : 'nil'
  when :u
    f.alt? ? 'undefined' : 'undef'
  when :d, :x, :X, :o, :b, :B, :e, :E, :f, :g, :G, :a, :A
    'NaN'
  when :v
    'n/a'
  when :V
    'N/A'
  when :s
    f.alt? ? '""' : ''
  when :p
    f.alt? ? '"undef"' : 'undef'
  else
    raise FormatError.new('Undef', f.format, 'nudxXobBeEfgGaAvVsp')
  end)
end