class Puppet::Pops::Types::PBinaryType

A Puppet Language Type that represents binary data content (a sequence of 8-bit bytes). Instances of this data type can be created from `String` and `Array[Integer]` values. Also see the `binary_file` function for reading binary content from a file.

A `Binary` can be converted to `String` and `Array` form - see function `new` for the respective target data type for more information.

Instances of this data type serialize as base 64 encoded strings when the serialization format is textual, and as binary content when a serialization format supports this.

@api public

Constants

DEFAULT

Public Class Methods

new_function(type) click to toggle source

@api private

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

Public Instance Methods

eql?(o) click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
143 def eql?(o)
144   self.class == o.class
145 end
from_array(array) click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
206 def from_array(array)
207   # The array is already known to have bytes in the range 0-255, or it is in error
208   # Without this pack C would produce weird results
209   Binary.from_binary_string(array.pack("C*"))
210 end
from_hash(hash) click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
212 def from_hash(hash)
213   case hash['value']
214   when Array
215     from_array(hash['value'])
216   when String
217     from_string(hash['value'], hash['format'])
218   end
219 end
from_string(str, format = nil) click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
181 def from_string(str, format = nil)
182   format ||= '%B'
183   case format
184   when "%b"
185     # padding must be added for older rubies to avoid truncation
186     padding = '=' * (str.length % 3)
187     Binary.new(Base64.decode64(str + padding))
188 
189   when "%u"
190     Binary.new(Base64.urlsafe_decode64(str))
191 
192   when "%B"
193     Binary.new(Base64.strict_decode64(str))
194 
195   when "%s"
196     Binary.from_string(str)
197 
198   when "%r"
199     Binary.from_binary_string(str)
200 
201   else
202     raise ArgumentError, "Unsupported Base64 format '#{format}'"
203   end
204 end
instance?(o, guard = nil) click to toggle source

Only instances of Binary are instances of the PBinaryType

    # File lib/puppet/pops/types/p_binary_type.rb
139 def instance?(o, guard = nil)
140   o.is_a?(Binary)
141 end
roundtrip_with_string?() click to toggle source

Binary uses the strict base64 format as its string representation @return [TrueClass] true

    # File lib/puppet/pops/types/p_binary_type.rb
149 def roundtrip_with_string?
150   true
151 end

Protected Instance Methods

_assignable?(o, guard) click to toggle source
    # File lib/puppet/pops/types/p_binary_type.rb
227 def _assignable?(o, guard)
228   o.class == self.class
229 end