compressor

package module
v0.0.0-...-7122652 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2024 License: MIT Imports: 9 Imported by: 0

README

compressor

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToDeflate

func ToDeflate(a any) []byte

ToDeflate compresses any given value into the Deflate format. This function internally uses the ToDeflateWithErr function to perform the compression and checks for any errors that may arise during the compression process.

If an error is encountered during the compression, this function will panic and throw the error.

Parameters:

  • a: an interface value to be compressed into Deflate format.

Returns:

  • []byte: A byte slice containing Deflate compressed form of the input value.

Panic:

This function will panic if an error is encountered during compression.

Example:

str := "Hello World"
deflateBytes := ToDeflate(str)
//deflateBytes now contains the Deflate compressed form of "Hello World"

arr := []int{1, 2, 3}
deflateBytes = ToDeflate(arr)
// deflateBytes now contains the Deflate compressed form of the array [1,2,3]

func ToDeflateBase64

func ToDeflateBase64(a any) string

ToDeflateBase64 compresses a given value using Deflate method and then encodes the compressed data into a Base64 string. It uses the ToDeflateBase64WithErr function to perform the compression and encoding. If any error occurs during the process, the function will panic and terminate the program.

Parameters:

  • a: An interface value. This can be any data type and holds the data to be compressed and encoded.

Returns:

  • string: A string in Base64 format that represents the Deflate compressed form of the input data.

Example:

// Example 1: Simple String
s := "Hello, I am AI Assistant!"
b64 := ToDeflateBase64(s)
fmt.Println(b64) // Prints the Base64-encoded, Deflate compressed form of the string

// Example 2: Struct
type user struct {
    Name string
    Age  int
}
u := user{"Bob", 25}
b64 = ToDeflateBase64(u)
fmt.Println(b64) // Prints the Base64-encoded, Deflate compressed form of the struct

Panics:

  • If there is an error during the compression or encoding process, the function will panic.

func ToDeflateBase64WithErr

func ToDeflateBase64WithErr(a any) (string, error)

ToDeflateBase64WithErr compresses any given value into the Deflate format and encodes the result into a Base64 string. This function leverages the ToDeflateWithErr function for the compression and then uses the base64.StdEncoding.EncodeToString method for the encoding of the compressed data into Base64. Consequently, it can propagate any errors encountered in the compression or encoding.

Parameters:

  • a: Any value that will be compressed into the Deflate format and encoded into Base64. The actual value can be of any data type.

Returns:

  • string: A Base64-encoded string that represents the Deflate-compressed form of the input data.
  • error: An error object that indicates any errors that occurred during the compression or encoding operations. If no errors occurred, it returns nil.

Example:

// Example 1: Simple String
s := "Hello, I am AI Assistant!"
b64, err := ToDeflateBase64WithErr(s)
if err != nil {
     log.Fatal(err)
}
fmt.Println(b64) // Prints the Base64-encoded, Deflate compressed form of the string

// Example 2: Struct
type user struct {
    Name string
    Age  int
}
u := user{"Bob", 25}
b64, err = ToDeflateBase64WithErr(u)
if err != nil {
     log.Fatal(err)
}
fmt.Println(b64) // Prints the Base64-encoded, Deflate compressed form of the struct

func ToDeflateWithErr

func ToDeflateWithErr(a any) ([]byte, error)

ToDeflateWithErr function converts and compresses any given value into the Deflate format. This function delegates the conversion process to the 'toBytes' function. Also, it enables error propagation which gives the ability to handle the error in parent functions.

Parameters:

  • a: Any data type, provides the interface value to be compressed into Deflate format.

Returns:

  • [ ]byte: A byte slice exhibiting the Deflate compressed form of the value given as the 'a' parameter.
  • error: An error object indicating any error encountered during the operation. Returns 'nil' if the operation is successful.

Example:

// Example 1: Simple String
s := "Hello, I am AI Assistant!"
bs, err := ToDeflateWithErr(s)
if err != nil {
     log.Fatal(err)
}
fmt.Println(bs) // Prints the Deflate compressed form of the string

// Example 2: Struct
type user struct {
    Name string
    Age  int
}
u := user{"Bob", 25}
bs, err = ToDeflateWithErr(u)
if err != nil {
     log.Fatal(err)
}
fmt.Println(bs) // Prints the Deflate compressed form of the struct

func ToGzip

func ToGzip(a any) []byte

ToGzip converts any given value into a byte array using GZIP compression. It first converts the given value into a byte array using the toBytes function and then applies compression using gzip. If the conversion to byte array fails, it will cause a panic.

Parameters:

  • a: Any value to be converted into a GZIP compressed byte array.

Returns:

  • []byte: The GZIP compressed byte array representation of the given value.

Panic:

  • If the conversion to byte array fails, it will cause a panic.

Example:

 var x string = "Hello, world!"
 var y int = 12345
	fmt.Println(ToGzip(x))  // returns gzip compressed byte array of the string
	fmt.Println(ToGzip(y))  // returns gzip compressed byte array of the integer

Note:

This function is a variation of ToGzipWithErr where it suppresses any occurring error by causing a panic.

func ToGzipBase64

func ToGzipBase64(a any) string

ToGzipBase64 converts any given value into a GZIP compressed Base64 string. It first compresses the given value into a GZIP byte array using the ToGzipBase64WithErr function and then converts it to a Base64 string. If any error occurs during the conversion or compression, it would panic and throw the error.

Parameters:

  • a: Any value to be converted into a GZIP compressed Base64 string.

Returns:

  • string: The GZIP compressed Base64 string representation of the given value.

Panics:

  • error: Any errors that might occur during the conversion or compression.

Example:

var x string = "Hello, world!"
b64 := ToGzipBase64(x)
fmt.Println(b64)  // Prints the gzip compressed Base64 string of the string

var y map[string]int = map[string]int{"one": 1, "two": 2}
b64 = ToGzipBase64(y)
fmt.Println(b64)  // Prints the gzip compressed Base64 string of the map

func ToGzipBase64WithErr

func ToGzipBase64WithErr(a any) (string, error)

ToGzipBase64WithErr converts any given value into a GZIP compressed Base64 string. It first compresses the given value into a GZIP byte array using the ToGzipWithErr function and then converts it into a Base64 string. Any errors that occur during the conversion or compression are returned alongside with the Base64 string.

Parameters:

  • a: Any value to be converted into a GZIP compressed Base64 string.

Returns:

  • string: The GZIP compressed Base64 string representation of the given value.
  • error: Any errors that occur during the conversion or compression.

Example:

var x string = "Hello, world!"
var y map[string]int = map[string]int{"one": 1, "two": 2}
b64, err := ToGzipBase64WithErr(x)
if err != nil {
	log.Fatal(err)
}
fmt.Println(b64)  // Prints the gzip compressed Base64 string of the string

b64, err = ToGzipBase64WithErr(y)
if err != nil {
	log.Fatal(err)
}
fmt.Println(b64)  // Prints the gzip compressed Base64 string of the map

func ToGzipWithErr

func ToGzipWithErr(a any) ([]byte, error)

ToGzipWithErr converts any given value into a GZIP compressed byte array. It first converts the given value into a byte array using the toBytes function and then applies GZIP compression. Any errors that occur during the conversion or compression are returned alongside the byte array.

Parameters:

  • a: Any value to be converted into a GZIP compressed byte array.

Returns:

  • []byte: The GZIP compressed byte array representation of the given value.
  • error: Any errors that might occur during the conversion or compression.

Example:

var x string = "Hello, world!"
var y map[string]int = map[string]int{"one": 1, "two": 2}
bs, err := ToGzipWithErr(x)
if err != nil {
	log.Fatal(err)
}
fmt.Println(bs)  // Prints the gzip compressed byte array of the string

bs, err = ToGzipWithErr(y)
if err != nil {
	log.Fatal(err)
}
fmt.Println(bs)  // Prints the gzip compressed byte array of the map

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL