Class: Puppet::Pops::Types::PBinaryType

Inherits:
PAnyType show all
Defined in:
lib/puppet/pops/types/p_binary_type.rb

Overview

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.

Defined Under Namespace

Classes: Binary

Constant Summary collapse

DEFAULT =
PBinaryType.new

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #accept, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #generalize, #hash, #iterable?, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, #normalize, #really_instance?, #resolve, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from Visitable

#accept

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s

Class Method Details

.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

.register_ptype(loader, ir) ⇒ Object



132
133
134
# File 'lib/puppet/pops/types/p_binary_type.rb', line 132

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType')
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/puppet/pops/types/p_binary_type.rb', line 142

def eql?(o)
  self.class == o.class
end

#instance?(o, guard = nil) ⇒ Boolean

Only instances of Binary are instances of the PBinaryType

Returns:

  • (Boolean)


138
139
140
# File 'lib/puppet/pops/types/p_binary_type.rb', line 138

def instance?(o, guard = nil)
  o.is_a?(Binary)
end

#roundtrip_with_string?TrueClass

Binary uses the strict base64 format as its string representation

Returns:

  • (TrueClass)

    true



148
149
150
# File 'lib/puppet/pops/types/p_binary_type.rb', line 148

def roundtrip_with_string?
  true
end