Class: Puppet::Pops::Types::StringConverter Private
- Extended by:
- Concurrent::ThreadLocalSingleton
- Defined in:
- lib/puppet/pops/types/string_converter.rb
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.
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.
{ PObjectType::DEFAULT => Format.new('%p').freeze, # call with initialization hash 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
- DEFAULT_PARAMETER_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.
{ PCollectionType::DEFAULT => '%#p', PObjectType::DEFAULT => '%#p', PBinaryType::DEFAULT => '%p', PStringType::DEFAULT => '%p', PRuntimeType::DEFAULT => '%p' }.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
-
#convert(value, string_formats = :default) ⇒ Object
private
Converts the given value to a String, under the direction of formatting rules per type.
-
#initialize ⇒ StringConverter
constructor
private
A new instance of StringConverter.
- #is_a_or_h?(x) ⇒ Boolean private
- #is_container?(t) ⇒ Boolean private
- #puppet_double_quote(str) ⇒ Object private
-
#puppet_quote(str, enforce_double_quotes = false) ⇒ String
Performs a ‘%p’ formatting of the given str such that the output conforms to Puppet syntax.
-
#string_PAnyType(val_type, val, format_map, _) ⇒ Object
private
Basically string_PAnyType converts the value to a String and then formats it according to the resulting type.
- #string_PArrayType(val_type, val, format_map, indentation) ⇒ Object private
- #string_PBinaryType(val_type, val, format_map, _) ⇒ Object private
- #string_PBooleanType(val_type, val, format_map, indentation) ⇒ Object private
- #string_PDefaultType(val_type, val, format_map, _) ⇒ Object private
- #string_PFloatType(val_type, val, format_map, _) ⇒ Object private
- #string_PHashType(val_type, val, format_map, indentation) ⇒ Object private
- #string_PIntegerType(val_type, val, format_map, _) ⇒ Object private
- #string_PIteratorType(val_type, val, format_map, indentation) ⇒ Object private
- #string_PObjectType(val_type, val, format_map, indentation) ⇒ Object private
- #string_PObjectTypeExtension(val_type, val, format_map, indentation) ⇒ Object private
- #string_PRegexpType(val_type, val, format_map, _) ⇒ Object private
- #string_PRuntimeType(val_type, val, format_map, indent) ⇒ Object private
- #string_PStringType(val_type, val, format_map, _) ⇒ Object private
- #string_PStructType(val_type, val, format_map, indentation) ⇒ Object private
- #string_PTupleType(val_type, val, format_map, indentation) ⇒ Object private
- #string_PTypeType(val_type, val, format_map, _) ⇒ Object private
- #string_PUndefType(val_type, val, format_map, _) ⇒ Object private
- #string_PURIType(val_type, val, format_map, indentation) ⇒ Object private
Methods included from Concurrent::ThreadLocalSingleton
Constructor Details
#initialize ⇒ StringConverter
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.
261 262 263 |
# File 'lib/puppet/pops/types/string_converter.rb', line 261 def initialize @string_visitor = Visitor.new(self, "string", 3, 3) end |
Class Method Details
.convert(value, string_formats = :default) ⇒ Object
255 256 257 |
# File 'lib/puppet/pops/types/string_converter.rb', line 255 def self.convert(value, string_formats = :default) singleton.convert(value, string_formats) 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.
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/puppet/pops/types/string_converter.rb', line 488 def convert(value, string_formats = :default) = DEFAULT_STRING_FORMATS value_type = TypeCalculator.infer_set(value) if string_formats.is_a?(String) # For Array and Hash, the format is given as a Hash where 'format' key is the format for the collection itself if Puppet::Pops::Types::PArrayType::DEFAULT.assignable?(value_type) # add the format given for the exact type string_formats = { Puppet::Pops::Types::PArrayType::DEFAULT => { 'format' => string_formats } } elsif Puppet::Pops::Types::PHashType::DEFAULT.assignable?(value_type) # add the format given for the exact type string_formats = { Puppet::Pops::Types::PHashType::DEFAULT => { 'format' => string_formats } } else # add the format given for the exact type string_formats = { value_type => string_formats } end 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 = 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, , 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.
1019 1020 1021 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1019 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.
1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1023 def is_container?(t) case t when PArrayType, PHashType, PStructType, PTupleType, PObjectType true else false end end |
#puppet_double_quote(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.
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 |
# File 'lib/puppet/pops/types/string_converter.rb', line 896 def puppet_double_quote(str) bld = '"'.dup str.each_codepoint 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 bld << sprintf('\\u{%X}', codepoint) elsif codepoint <= 0x7f bld << codepoint else bld << [codepoint].pack('U') end end end bld << '"' bld end |
#puppet_quote(str, enforce_double_quotes = false) ⇒ 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.
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 |
# File 'lib/puppet/pops/types/string_converter.rb', line 862 def puppet_quote(str, enforce_double_quotes = false) if enforce_double_quotes return puppet_double_quote(str) end # Assume that the string can be single quoted bld = "'".dup bld.force_encoding(str.encoding) escaped = false str.each_codepoint do |codepoint| # Control characters and non-ascii characters cannot be present in a single quoted string return puppet_double_quote(str) if codepoint < 0x20 if escaped bld << 0x5c << codepoint escaped = false elsif codepoint == 0x27 bld << 0x5c << codepoint elsif codepoint == 0x5c escaped = true elsif codepoint <= 0x7f bld << codepoint else bld << [codepoint].pack('U') end end # If string ended with a backslash, then that backslash must be escaped bld << 0x5c if escaped bld << "'" bld 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
625 626 627 628 |
# File 'lib/puppet/pops/types/string_converter.rb', line 625 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.
942 943 944 945 946 947 948 949 950 951 952 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 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 |
# File 'lib/puppet/pops/types/string_converter.rb', line 942 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 = ''.dup 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.each_with_object([0]) do |v_i, memo| # array or hash breaks if is_a_or_h?(v_i[0]) memo << 0 else memo[-1] += mapped[v_i[1]].length end 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.rstrip! unless buf[-1] == "\n" buf << "\n" buf << children_indentation.padding end end # remove trailing space added by separator if followed by break buf.rstrip! if buf[-1] == ' ' && str_val[0] == "\n" 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.
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/puppet/pops/types/string_converter.rb', line 773 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 name of the type "Binary(\"#{Kernel.format(f.orig_fmt.tr('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.
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
# File 'lib/puppet/pops/types/string_converter.rb', line 667 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.
630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/puppet/pops/types/string_converter.rb', line 630 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.
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
# File 'lib/puppet/pops/types/string_converter.rb', line 754 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.
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1044 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) if format.alt? sep = sep.rstrip unless sep[-1] == "\n" sep = "#{sep}\n" end 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 = ''.dup 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.
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/puppet/pops/types/string_converter.rb', line 729 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 Kernel.format(f.orig_fmt.tr('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.
1038 1039 1040 1041 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1038 def string_PIteratorType(val_type, val, format_map, indentation) v = val.to_a _convert(TypeCalculator.infer_set(v), v, format_map, indentation) end |
#string_PObjectType(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.
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/puppet/pops/types/string_converter.rb', line 572 def string_PObjectType(val_type, val, format_map, indentation) f = get_format(val_type, format_map) case f.format when :p fmt = TypeFormatter.singleton indentation = indentation.indenting(f.alt? || indentation.is_indenting?) fmt = fmt.indented(indentation.level, 2) if indentation.is_indenting? fmt.string(val) when :s val.to_s when :q val.inspect else raise FormatError.new('Object', f.format, 'spq') end end |
#string_PObjectTypeExtension(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.
589 590 591 |
# File 'lib/puppet/pops/types/string_converter.rb', line 589 def string_PObjectTypeExtension(val_type, val, format_map, indentation) string_PObjectType(val_type.base_type, val, 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.
927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
# File 'lib/puppet/pops/types/string_converter.rb', line 927 def string_PRegexpType(val_type, val, format_map, _) f = get_format(val_type, format_map) case f.format when :p str_regexp = PRegexpType.regexp_to_s_with_delimiters(val) f.orig_fmt == '%p' ? str_regexp : Kernel.format(f.orig_fmt.tr('p', 's'), str_regexp) when :s str_regexp = PRegexpType.regexp_to_s(val) str_regexp = puppet_quote(str_regexp) if f.alt? f.orig_fmt == '%s' ? str_regexp : Kernel.format(f.orig_fmt, str_regexp) else raise FormatError.new('Regexp', f.format, 'sp') end end |
#string_PRuntimeType(val_type, val, format_map, indent) ⇒ 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.
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/puppet/pops/types/string_converter.rb', line 593 def string_PRuntimeType(val_type, val, format_map, indent) # Before giving up on this, and use a string representation of the unknown # object, a check is made to see if the object can present itself as # a hash or an array. If it can, then that representation is used instead. case val when Hash hash = val.to_hash # Ensure that the returned value isn't derived from Hash return string_PHashType(val_type, hash, format_map, indent) if hash.instance_of?(Hash) when Array array = val.to_a # Ensure that the returned value isn't derived from Array return string_PArrayType(val_type, array, format_map, indent) if array.instance_of?(Array) end f = get_format(val_type, format_map) case f.format when :s val.to_s when :p puppet_quote(val.to_s) when :q val.inspect else raise FormatError.new('Runtime', f.format, 'spq') 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.
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 |
# File 'lib/puppet/pops/types/string_converter.rb', line 819 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, f.alt?)) when :c c_val = val.capitalize f.alt? ? apply_string_flags(f, puppet_quote(c_val)) : Kernel.format(f.orig_fmt.tr('c', 's'), c_val) when :C c_val = val.split('::').map(&:capitalize).join('::') f.alt? ? apply_string_flags(f, puppet_quote(c_val)) : Kernel.format(f.orig_fmt.tr('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.tr('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.tr('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.tr('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.
1099 1100 1101 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1099 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.
1033 1034 1035 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1033 def string_PTupleType(val_type, val, format_map, indentation) string_PArrayType(val_type, val, format_map, indentation) end |
#string_PTypeType(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.
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1104 def string_PTypeType(val_type, val, format_map, _) f = get_format(val_type, format_map) case f.format when :s str_val = f.alt? ? "\"#{val}\"" : val.to_s Kernel.format(f.orig_fmt, str_val) when :p Kernel.format(f.orig_fmt.tr('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.
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
# File 'lib/puppet/pops/types/string_converter.rb', line 643 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 |
#string_PURIType(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.
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 |
# File 'lib/puppet/pops/types/string_converter.rb', line 1118 def string_PURIType(val_type, val, format_map, indentation) f = get_format(val_type, format_map) case f.format when :p fmt = TypeFormatter.singleton indentation = indentation.indenting(f.alt? || indentation.is_indenting?) fmt = fmt.indented(indentation.level, 2) if indentation.is_indenting? fmt.string(val) when :s str_val = val.to_s Kernel.format(f.orig_fmt, f.alt? ? puppet_quote(str_val) : str_val) else raise FormatError.new('URI', f.format, 'sp') end end |