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

Public Instance Methods

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

Protected Instance Methods

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