typeutil

package
v1.86.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package typeutil contains a collection of type-related utility functions.

This package provides a set of utility functions and definitions to work with generic types in Go. It also provides serialization and deserialization functions to safely transmit, store and retrieve data between different systems (e.g., databases, queues, caches, etc.).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteDecode added in v1.85.0

func ByteDecode(msg []byte, data any) error

ByteDecode decodes a byte slice message encoded with the ByteEncode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func ByteDecryptAny added in v1.86.0

func ByteDecryptAny(key, msg []byte, data any) error

ByteDecryptAny decrypts a byte-slice message produced with the ByteEncryptAny function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received. The key argument must be the same used to encrypt the data: either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func ByteDeserialize added in v1.85.0

func ByteDeserialize(msg []byte, data any) error

ByteDeserialize decodes a string message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func ByteEncode added in v1.85.0

func ByteEncode(data any) ([]byte, error)

ByteEncode encodes the input data to gob+base64 byte slice.

func ByteEncryptAny added in v1.86.0

func ByteEncryptAny(key []byte, data any) ([]byte, error)

ByteEncryptAny encrypts data with the specified key and returns a base64 byte slice. The key argument must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func ByteSerialize added in v1.85.0

func ByteSerialize(data any) ([]byte, error)

ByteSerialize encodes the input data to JSON+base64 byte slice.

func Decode

func Decode(msg string, data any) error

Decode decodes a string message encoded with the Encode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	var data TestData

	msg := "Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA=="

	err := typeutil.Decode(msg, &data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(data)

}
Output:

{abc123 -375}

func Decrypt added in v1.85.0

func Decrypt(key, msg []byte) ([]byte, error)

Decrypt decrypts a byte-slice data encrypted with the Encrypt function. The key argument must be the same used to encrypt the data: either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func DecryptAny added in v1.86.0

func DecryptAny(key []byte, msg string, data any) error

DecryptAny decrypts string message produced with the EncryptAny function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received. The key argument must be the same used to encrypt the data: either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func Deserialize added in v1.69.0

func Deserialize(msg string, data any) error

Deserialize decodes a byte slice message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	var data TestData

	msg := "eyJBbHBoYSI6ImFiYzEyMyIsIkJldGEiOi0zNzV9Cg=="

	err := typeutil.Deserialize(msg, &data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(data)

}
Output:

{abc123 -375}

func Encode

func Encode(data any) (string, error)

Encode encodes the input data to gob+base64 string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	data := &TestData{Alpha: "test_string", Beta: -9876}

	v, err := typeutil.Encode(data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(v)
}
Output:

func Encrypt added in v1.85.0

func Encrypt(key, msg []byte) ([]byte, error)

Encrypt encrypts the byte-slice input msg with the specified key. The key argument must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func EncryptAny added in v1.86.0

func EncryptAny(key []byte, data any) (string, error)

EncryptAny encrypts data with the specified key and returns a base64 string The key argument must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.

func IsNil

func IsNil(v any) bool

IsNil returns true if the input value is nil.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	var nilChan chan int

	v := typeutil.IsNil(nilChan)
	fmt.Println(v)

}
Output:

true

func IsZero

func IsZero[T any](v T) bool

IsZero returns true if the input value is equal to the zero instance (e.g. empty string, 0 int, nil pointer).

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	var zeroInt int

	v := typeutil.IsZero(zeroInt)
	fmt.Println(v)

}
Output:

true

func Pointer added in v1.75.1

func Pointer[T any](v T) *T

Pointer returns the address of v.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	v := 5

	p := typeutil.Pointer(v)
	fmt.Println(p)
}
Output:

func Serialize added in v1.68.0

func Serialize(data any) (string, error)

Serialize encodes the input data to JSON+base64 string.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	type TestData struct {
		Alpha string
		Beta  int
	}

	data := &TestData{Alpha: "test_string", Beta: -9876}

	v, err := typeutil.Serialize(data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(v)

}
Output:

eyJBbHBoYSI6InRlc3Rfc3RyaW5nIiwiQmV0YSI6LTk4NzZ9Cg==

func Value added in v1.75.1

func Value[T any](p *T) T

Value returns the value of the provided pointer or the type default (zero value) if nil.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	num := 5
	p := &num

	v := typeutil.Value(p)
	fmt.Println(v)

}
Output:

5

func Zero added in v1.75.1

func Zero[T any](_ T) T

Zero returns the zero instance (e.g. empty string, 0 int, nil pointer).

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/typeutil"
)

func main() {
	num := 5

	v := typeutil.Zero(num)
	fmt.Println(v)

}
Output:

0

Types

type Float added in v1.80.0

type Float interface {
	~float32 | ~float64
}

Float is a constraint for float types.

type Int added in v1.80.0

type Int interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Int is a constraint for signed integer types.

type Number added in v1.80.0

type Number interface {
	Int | UInt | Float
}

Number is a constraint for all integer and float numbers.

type Ordered added in v1.80.0

type Ordered interface {
	Number | ~string
}

Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >.

type UInt added in v1.80.0

type UInt interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

UInt is a constraint for unsigned integer types.

Jump to

Keyboard shortcuts

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