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 ¶
- Variables
- func ByteDecode(msg []byte, data any) error
- func ByteDecryptAny(key, msg []byte, data any) error
- func ByteDecryptSerializeAny(key, msg []byte, data any) error
- func ByteDeserialize(msg []byte, data any) error
- func ByteEncode(data any) ([]byte, error)
- func ByteEncryptAny(key []byte, data any) ([]byte, error)
- func ByteEncryptSerializeAny(key []byte, data any) ([]byte, error)
- func ByteSerialize(data any) ([]byte, error)
- func Decode(msg string, data any) error
- func Decrypt(key, msg []byte) ([]byte, error)
- func DecryptAny(key []byte, msg string, data any) error
- func DecryptSerializeAny(key []byte, msg string, data any) error
- func Deserialize(msg string, data any) error
- func Encode(data any) (string, error)
- func Encrypt(key, msg []byte) ([]byte, error)
- func EncryptAny(key []byte, data any) (string, error)
- func EncryptSerializeAny(key []byte, data any) (string, error)
- func IsNil(v any) bool
- func IsZero[T any](v T) bool
- func Pointer[T any](v T) *T
- func RandUint32() uint32
- func RandUint64() uint64
- func RandomBytes(r io.Reader, n int) ([]byte, error)
- func Serialize(data any) (string, error)
- func Value[T any](p *T) T
- func Zero[T any](_ T) T
- type Float
- type Int
- type Number
- type Ordered
- type UInt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var RandReader = rand.Reader //nolint:gochecknoglobals
RandReader is the default random number generator.
Functions ¶
func ByteDecode ¶ added in v1.85.0
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
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 ByteDecryptSerializeAny ¶ added in v1.87.0
ByteDecryptSerializeAny decrypts a byte-slice message produced with the ByteEncryptSerializeAny 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
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
ByteEncode encodes the input data to gob+base64 byte slice.
func ByteEncryptAny ¶ added in v1.86.0
ByteEncryptAny encrypts the input data with the specified key and returns a base64 byte slice. The input data is serialized using gob, encrypted with the Encrypt method and encoded as base64. The key argument must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256.
func ByteEncryptSerializeAny ¶ added in v1.87.0
ByteEncryptSerializeAny encrypts the input data with the specified key and returns a base64 byte slice. The input data is serialized using json, encrypted with the Encrypt method and encoded as base64. 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
ByteSerialize encodes the input data to JSON+base64 byte slice.
func Decode ¶
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
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
DecryptAny wraps the ByteDecryptAny function to accept a msg string instead of a byte slice.
func DecryptSerializeAny ¶ added in v1.87.0
DecryptSerializeAny wraps the ByteDecryptSerializeAny function to accept a msg string instead of a byte slice.
func Deserialize ¶ added in v1.69.0
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 ¶
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)
}
func Encrypt ¶ added in v1.85.0
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
EncryptAny wraps the ByteEncryptAny function to return a string instead of a byte slice.
func EncryptSerializeAny ¶ added in v1.87.0
EncryptSerializeAny wraps the ByteEncrypSerializetAny function to return a string instead of a byte slice.
func IsNil ¶
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 ¶
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)
}
func RandUint32 ¶ added in v1.87.0
func RandUint32() uint32
RandUint32 returns a pseudo-random 32-bit value as a uint32 from the default Source. It try to use crypto/rand.Reader, if it fails, it falls back to math/rand/v2.Uint32.
func RandUint64 ¶ added in v1.87.0
func RandUint64() uint64
RandUint64 returns a pseudo-random 64-bit value as a uint64 from the default Source. It try to use crypto/rand.Reader, if it fails, it falls back to math/rand/v2.Uint64.
func RandomBytes ¶ added in v1.87.0
RandomBytes generates a slice of random bytes with the specified length. The r argument must be a cryptographically secure random number generator (i.e. crypto/rand.Read).
func Serialize ¶ added in v1.68.0
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