Method: Puppet::Pops::Types::PBinaryType.new_function
- Defined in:
- lib/puppet/pops/types/p_binary_type.rb
.new_function(type) ⇒ 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.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/puppet/pops/types/p_binary_type.rb', line 153 def self.new_function(type) @new_function ||= Puppet::Functions.create_loaded_function(:new_Binary, type.loader) do local_types do type 'ByteInteger = Integer[0,255]' type 'Base64Format = Enum["%b", "%u", "%B", "%s", "%r"]' type 'StringHash = Struct[{value => String, "format" => Optional[Base64Format]}]' type 'ArrayHash = Struct[{value => Array[ByteInteger]}]' type 'BinaryArgsHash = Variant[StringHash, ArrayHash]' end # Creates a binary from a base64 encoded string in one of the formats %b, %u, %B, %s, or %r dispatch :from_string do param 'String', :str optional_param 'Base64Format', :format end dispatch :from_array do param 'Array[ByteInteger]', :byte_array end # Same as from_string, or from_array, but value and (for string) optional format are given in the form # of a hash. # dispatch :from_hash do param 'BinaryArgsHash', :hash_args end def from_string(str, format = nil) format ||= '%B' case format when "%b" # padding must be added for older rubies to avoid truncation padding = '=' * (str.length % 3) Binary.new(Base64.decode64(str + padding)) when "%u" Binary.new(Base64.urlsafe_decode64(str)) when "%B" Binary.new(Base64.strict_decode64(str)) when "%s" Binary.from_string(str) when "%r" Binary.from_binary_string(str) else raise ArgumentError, "Unsupported Base64 format '#{format}'" end end def from_array(array) # The array is already known to have bytes in the range 0-255, or it is in error # Without this pack C would produce weird results Binary.from_binary_string(array.pack("C*")) end def from_hash(hash) case hash['value'] when Array from_array(hash['value']) when String from_string(hash['value'], hash['format']) end end end end |